forked from jooby-project/jooby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceKey.java
More file actions
89 lines (77 loc) · 1.86 KB
/
ServiceKey.java
File metadata and controls
89 lines (77 loc) · 1.86 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
/**
* Jooby https://jooby.io
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
* Copyright 2014 Edgar Espina
*/
package io.jooby;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
/**
* Utility class to access application services.
*
* @param <T> Service type.
*/
public final class ServiceKey<T> {
private final Class<T> type;
private final int hashCode;
private final String name;
private ServiceKey(Class<T> type, String name) {
this.type = type;
this.name = name;
this.hashCode = Objects.hash(type, name);
}
/**
* Resource type.
*
* @return Resource type.
*/
public @Nonnull Class<T> getType() {
return type;
}
/**
* 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) {
ServiceKey that = (ServiceKey) obj;
return this.type == that.type && Objects.equals(this.name, that.name);
}
return false;
}
@Override public int hashCode() {
return hashCode;
}
@Override public String toString() {
if (name == null) {
return type.getName();
}
return type.getName() + "(" + 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, 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, name);
}
}