1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.inject.api; |
18 | |
|
19 | |
import java.lang.annotation.Annotation; |
20 | |
import java.lang.reflect.Array; |
21 | |
|
22 | |
public class Key<T> implements IKey<T> { |
23 | |
public static final String NO_NAME = ""; |
24 | 1 | public static final Annotation[] NO_ANNOTATIONS = (Annotation[]) Array.newInstance(Annotation.class, 0); |
25 | |
private final Class<T> type; |
26 | |
private final String name; |
27 | |
private final Annotation[] annotations; |
28 | |
|
29 | 393 | public Key(Class<T> pType, String pName, Annotation[] pAnnotations) { |
30 | 393 | if (pType == null) { |
31 | 0 | throw new NullPointerException("The keys type must not be null."); |
32 | |
} |
33 | 393 | this.type = pType; |
34 | 393 | if (pName == null) { |
35 | 0 | name = NO_NAME; |
36 | |
} else { |
37 | 393 | name = pName; |
38 | |
} |
39 | 393 | if (pAnnotations == null) { |
40 | 0 | annotations = NO_ANNOTATIONS; |
41 | |
} else { |
42 | 393 | annotations = pAnnotations; |
43 | |
} |
44 | 393 | } |
45 | |
|
46 | |
public Key(Class<T> pType, String pName) { |
47 | 193 | this(pType, pName, NO_ANNOTATIONS); |
48 | 193 | } |
49 | |
|
50 | |
public Key(Class<T> pType) { |
51 | 16 | this(pType, NO_NAME, NO_ANNOTATIONS); |
52 | 16 | } |
53 | |
|
54 | |
@Override |
55 | |
public Class<T> getType() { |
56 | 396 | return type; |
57 | |
} |
58 | |
|
59 | |
@Override |
60 | |
public String getName() { |
61 | 396 | return name; |
62 | |
} |
63 | |
|
64 | |
@Override |
65 | |
public Annotation[] getAnnotations() { |
66 | 477 | return annotations; |
67 | |
} |
68 | |
|
69 | |
@Override |
70 | |
public String toString() { |
71 | 3 | return toString(this); |
72 | |
} |
73 | |
|
74 | |
public static String toString(IKey<?> pKey) { |
75 | 3 | final StringBuilder sb = new StringBuilder(); |
76 | 3 | sb.append("Type="); |
77 | 3 | sb.append(pKey.getType().getName()); |
78 | 3 | final String name = pKey.getName(); |
79 | 3 | if (name.length() > 0) { |
80 | 2 | sb.append(", Name="); |
81 | 2 | sb.append(name); |
82 | |
} |
83 | 3 | final Annotation[] annotations = pKey.getAnnotations(); |
84 | 3 | if (annotations.length > 0) { |
85 | 1 | sb.append(", Annotations=["); |
86 | 2 | for (int i = 0; i < annotations.length; i++) { |
87 | 1 | if (i > 0) { |
88 | 0 | sb.append(","); |
89 | |
} |
90 | 1 | sb.append(annotations[i]); |
91 | |
} |
92 | 1 | sb.append("]"); |
93 | |
} |
94 | 3 | return sb.toString(); |
95 | |
} |
96 | |
} |