1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.inject.impl; |
18 | |
|
19 | |
import java.lang.annotation.Annotation; |
20 | |
import java.util.ArrayList; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.List; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
import org.apache.commons.inject.api.IBinding; |
26 | |
import org.apache.commons.inject.api.IKey; |
27 | |
import org.apache.commons.inject.api.Key; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class AbstractBindingSet { |
35 | |
|
36 | |
|
37 | |
|
38 | |
public static class MappedKey<T> extends Key<T> { |
39 | |
private final Class<? extends Annotation> annotationType; |
40 | |
public MappedKey(Class<T> pType, String pName, |
41 | |
Annotation[] pAnnotations, |
42 | |
Class<? extends Annotation> pAnnotationType) { |
43 | 183 | super(pType, pName, pAnnotations); |
44 | 183 | annotationType = pAnnotationType; |
45 | 183 | } |
46 | |
|
47 | |
public Class<? extends Annotation> getAnnotationType() { |
48 | 162 | return annotationType; |
49 | |
} |
50 | |
} |
51 | |
|
52 | |
|
53 | |
|
54 | |
protected static class BindingAndKey<T> { |
55 | |
private IBinding<T> binding; |
56 | |
private final MappedKey<T> key; |
57 | |
|
58 | 59 | BindingAndKey(IBinding<T> pBinding, MappedKey<T> pKey) { |
59 | 59 | binding = pBinding; |
60 | 59 | key = pKey; |
61 | 59 | } |
62 | |
|
63 | |
public IBinding<T> getBinding() { |
64 | 280 | return binding; |
65 | |
} |
66 | |
|
67 | |
public void setBinding(IBinding<T> pBinding) { |
68 | 0 | binding = pBinding; |
69 | 0 | } |
70 | |
|
71 | |
public MappedKey<T> getKey() { |
72 | 205 | return key; |
73 | |
} |
74 | |
} |
75 | |
protected static class ReducedKey<T> { |
76 | |
private final Class<T> type; |
77 | |
private final String name; |
78 | 210 | ReducedKey(Class<T> pType, String pName) { |
79 | 210 | type = pType; |
80 | 210 | name = pName; |
81 | 210 | } |
82 | |
public Class<T> getType() { |
83 | 326 | return type; |
84 | |
} |
85 | |
public String getName() { |
86 | 326 | return name; |
87 | |
} |
88 | |
@Override |
89 | |
public int hashCode() { |
90 | 255 | return 31 * (31 + name.hashCode()) + type.hashCode(); |
91 | |
} |
92 | |
|
93 | |
@Override |
94 | |
public boolean equals(Object obj) { |
95 | 163 | if (this == obj) |
96 | 0 | return true; |
97 | 163 | if (obj == null) |
98 | 0 | return false; |
99 | 163 | if (getClass() != obj.getClass()) |
100 | 0 | return false; |
101 | 163 | ReducedKey<?> other = (ReducedKey<?>) obj; |
102 | 163 | return getType() == other.getType() && getName().equals(other.getName()); |
103 | |
} |
104 | |
|
105 | |
|
106 | |
} |
107 | |
|
108 | |
protected final Map<ReducedKey<?>, List<BindingAndKey<?>>> map; |
109 | |
|
110 | 27 | protected AbstractBindingSet(Map<ReducedKey<?>, List<BindingAndKey<?>>> pMap) { |
111 | 27 | map = pMap; |
112 | 27 | } |
113 | |
|
114 | |
protected AbstractBindingSet() { |
115 | 9 | this(new HashMap<ReducedKey<?>, List<BindingAndKey<?>>>()); |
116 | 9 | } |
117 | |
|
118 | |
|
119 | |
protected <T> ReducedKey<T> newReducedKey(IKey<T> pKey) { |
120 | 183 | return new ReducedKey<T>(pKey.getType(), pKey.getName()); |
121 | |
} |
122 | |
|
123 | |
protected List<BindingAndKey<?>> findOrCreateList(ReducedKey<?> pKey) { |
124 | 183 | List<BindingAndKey<?>> list = map.get(pKey); |
125 | 183 | if (list == null) { |
126 | 45 | list = new ArrayList<BindingAndKey<?>>(); |
127 | 45 | map.put(pKey, list); |
128 | |
} |
129 | 183 | return list; |
130 | |
} |
131 | |
|
132 | |
protected boolean isMatching(IKey<?> pSearchKey, MappedKey<?> pMapKey) { |
133 | |
|
134 | |
|
135 | |
|
136 | 163 | if (!hasAnnotations(pMapKey.getAnnotations(), pSearchKey) |
137 | |
|| !hasAnnotations(pSearchKey.getAnnotations(), pMapKey)) { |
138 | 1 | return false; |
139 | |
} |
140 | 162 | final Class<? extends Annotation> mappedAnnotationType = pMapKey.getAnnotationType(); |
141 | 162 | if (mappedAnnotationType != null) { |
142 | 0 | boolean found = false; |
143 | 0 | for (Annotation searchAnnotation : pSearchKey.getAnnotations()) { |
144 | 0 | if (searchAnnotation != null && mappedAnnotationType == searchAnnotation.getClass()) { |
145 | 0 | found = true; |
146 | 0 | break; |
147 | |
} |
148 | |
} |
149 | 0 | if (!found) { |
150 | 0 | return false; |
151 | |
} |
152 | |
|
153 | |
} |
154 | 162 | return true; |
155 | |
} |
156 | |
|
157 | |
private boolean hasAnnotations(Annotation[] pAnnotations, IKey<?> pKey) { |
158 | 326 | for (Annotation annotation : pAnnotations) { |
159 | 1 | boolean found = false; |
160 | 1 | for (Annotation ann : pKey.getAnnotations()) { |
161 | 0 | if (annotation.equals(ann)) { |
162 | 0 | found = true; |
163 | 0 | break; |
164 | |
} |
165 | |
} |
166 | 1 | if (!found) { |
167 | 1 | return false; |
168 | |
} |
169 | |
} |
170 | 325 | return true; |
171 | |
} |
172 | |
} |