-
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathServiceKey.java
More file actions
131 lines (114 loc) · 2.76 KB
/
ServiceKey.java
File metadata and controls
131 lines (114 loc) · 2.76 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* 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 org.jspecify.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 Type getType() {
return type;
}
/**
* Raw type in case of parameterized type.
*
* @return Raw type in case of parameterized type.
*/
public 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 <T> ServiceKey<T> key(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 <T> ServiceKey<T> key(Class<T> type, String name) {
return new ServiceKey<>(type, type, name);
}
/**
* Creates a named resource key.
*
* @param type Resource type.
* @param name Resource name.
* @param <T> Type.
* @return A new resource key.
*/
@SuppressWarnings("unchecked")
public static <T> ServiceKey<T> key(Reified<T> type, String name) {
return new ServiceKey<>(type.getType(), (Class<T>) type.getRawType(), name);
}
/**
* Creates a resource key.
*
* @param type Resource type.
* @param <T> Type.
* @return A new resource key.
*/
@SuppressWarnings("unchecked")
public static <T> ServiceKey<T> key(Reified<T> type) {
return new ServiceKey<>(type.getType(), (Class<T>) type.getRawType(), null);
}
}