-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathServiceKey.java
More file actions
111 lines (95 loc) · 2.51 KB
/
Copy pathServiceKey.java
File metadata and controls
111 lines (95 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import java.lang.reflect.Type;
import java.util.Objects;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.jooby.internal.reflect.$Types;
/**
* Utility class to access application services.
*
* @param <T> Service type.
*/
public final class ServiceKey<T> {
private final Type type;
private final Class<T> rawType;
private final int hashCode;
private final String name;
private ServiceKey(Type type, Class<T> rawType, String name) {
this.type = type;
this.rawType = rawType;
this.name = name;
this.hashCode = Objects.hash(type, name);
}
/**
* Resource type.
*
* @return Resource type.
*/
public @NonNull Type getType() {
return type;
}
public @NonNull Class<T> getRawType() {
return rawType;
}
/**
* Resource name or <code>null</code>.
*
* @return Resource name or <code>null</code>.
*/
public @Nullable String getName() {
return name;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ServiceKey<?> key) {
return this.type.equals(key.type) && Objects.equals(this.name, key.name);
}
return false;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public String toString() {
var typeName = $Types.typeToString(type);
if (name == null) {
return typeName;
}
return typeName + "(" + name + ")";
}
/**
* Creates a resource key.
*
* @param type Resource type.
* @param <T> Type.
* @return A new resource key.
*/
public static @NonNull <T> ServiceKey<T> key(@NonNull Class<T> type) {
return new ServiceKey<>(type, type, null);
}
/**
* Creates a named resource key.
*
* @param type Resource type.
* @param name Resource name.
* @param <T> Type.
* @return A new resource key.
*/
public static @NonNull <T> ServiceKey<T> key(@NonNull Class<T> type, @NonNull String name) {
return new ServiceKey<>(type, type, name);
}
@SuppressWarnings("unchecked")
public static @NonNull <T> ServiceKey<T> key(@NonNull Reified<T> type, @NonNull String name) {
return new ServiceKey<>(type.getType(), (Class<T>) type.getRawType(), name);
}
@SuppressWarnings("unchecked")
public static @NonNull <T> ServiceKey<T> key(@NonNull Reified<T> type) {
return new ServiceKey<>(type.getType(), (Class<T>) type.getRawType(), null);
}
}