1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.rribbit.retrieval;
17
18 import java.util.Collection;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21
22 import org.rribbit.ListenerObject;
23 import org.rribbit.creation.ListenerObjectCreator;
24 import org.rribbit.creation.notification.ListenerObjectCreationObserver;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class CachedListenerObjectRetriever extends DefaultListenerObjectRetriever implements ListenerObjectCreationObserver {
41
42 private static final Logger log = LoggerFactory.getLogger(CachedListenerObjectRetriever.class);
43
44
45
46
47 protected Map<RetrievalRequest, Collection<ListenerObject>> cache;
48
49
50
51
52
53 public CachedListenerObjectRetriever() {
54 this.init();
55 }
56
57
58
59
60
61
62
63 public CachedListenerObjectRetriever(ListenerObjectCreator... listenerObjectCreators) {
64 super(listenerObjectCreators);
65 this.init();
66 }
67
68
69
70
71 protected void init() {
72 cache = new ConcurrentHashMap<>();
73 }
74
75
76
77
78
79
80 @Override
81 public void onClassAdded(Class<?> addedClass) {
82
83 log.debug("Class was added by ListenerObjectCreator, clearing cache...");
84 this.clearCache();
85 }
86
87 @Override
88 public Collection<ListenerObject> getListenerObjects(Class<?> returnType) {
89
90 this.checkReturnType(returnType);
91
92 log.debug("Inspecting cache for matches");
93 RetrievalRequest request = new RetrievalRequest(null, returnType);
94 Collection<ListenerObject> listenerObjects = cache.get(request);
95 if(listenerObjects == null) {
96 log.debug("No match found, retrieving ListenerObject from DefaultRetriever and storing in cache");
97 listenerObjects = super.getListenerObjects(returnType);
98 cache.put(request, listenerObjects);
99 }
100 log.debug("Found {} ListenerObjects", listenerObjects.size());
101 return listenerObjects;
102 }
103
104 @Override
105 public Collection<ListenerObject> getListenerObjects(String hint) {
106
107 this.checkHint(hint);
108
109 log.debug("Inspecting cache for matches");
110 RetrievalRequest request = new RetrievalRequest(hint, null);
111 Collection<ListenerObject> listenerObjects = cache.get(request);
112 if(listenerObjects == null) {
113 log.debug("No match found, retrieving ListenerObject from DefaultRetriever and storing in cache");
114 listenerObjects = super.getListenerObjects(hint);
115 cache.put(request, listenerObjects);
116 }
117 log.debug("Found {} ListenerObjects", listenerObjects.size());
118 return listenerObjects;
119 }
120
121 @Override
122 public Collection<ListenerObject> getListenerObjects(Class<?> returnType, String hint) {
123
124 this.checkReturnType(returnType);
125 this.checkHint(hint);
126
127 log.debug("Inspecting cache for matches");
128 RetrievalRequest request = new RetrievalRequest(hint, returnType);
129 Collection<ListenerObject> listenerObjects = cache.get(request);
130 if(listenerObjects == null) {
131 log.debug("No match found, retrieving ListenerObject from DefaultRetriever and storing in cache");
132 listenerObjects = super.getListenerObjects(returnType, hint);
133 cache.put(request, listenerObjects);
134 }
135 log.debug("Found {} ListenerObjects", listenerObjects.size());
136 return listenerObjects;
137 }
138
139 @Override
140 public void addListenerObjectCreator(ListenerObjectCreator listenerObjectCreator) {
141 super.addListenerObjectCreator(listenerObjectCreator);
142 listenerObjectCreator.registerObserver(this);
143 this.clearCache();
144 }
145
146 @Override
147 public void setListenerObjectCreator(ListenerObjectCreator listenerObjectCreator) {
148 super.setListenerObjectCreator(listenerObjectCreator);
149 listenerObjectCreator.registerObserver(this);
150 this.clearCache();
151 }
152
153
154
155
156
157
158 protected void checkHint(String hint) {
159
160 if(hint == null) {
161 throw new IllegalArgumentException("hint cannot be null!");
162 }
163 }
164
165
166
167
168
169
170 protected void checkReturnType(Class<?> returnType) {
171
172 if(returnType == null) {
173 throw new IllegalArgumentException("returnType cannot be null!");
174 }
175 }
176
177 protected void clearCache() {
178
179 if(cache != null) {
180 cache.clear();
181 }
182 }
183
184
185
186
187
188
189
190
191 protected static class RetrievalRequest {
192
193 private String hint;
194 private Class<?> returnType;
195
196 public RetrievalRequest(String hint, Class<?> returnType) {
197 this.hint = hint;
198 this.returnType = returnType;
199 }
200
201 @Override
202 public int hashCode() {
203
204 int prime = 31;
205 int result = 1;
206 result = prime * result + ((hint == null) ? 0 : hint.hashCode());
207 result = prime * result + ((returnType == null) ? 0 : returnType.hashCode());
208 return result;
209 }
210
211 @Override
212 public boolean equals(Object obj) {
213
214 if(this == obj) {
215 return true;
216 }
217 if(obj == null) {
218 return false;
219 }
220 if(this.getClass() != obj.getClass()) {
221 return false;
222 }
223
224 RetrievalRequest other = (RetrievalRequest) obj;
225 if(hint == null) {
226 if(other.hint != null) {
227 return false;
228 }
229 } else if(!hint.equals(other.hint)) {
230 return false;
231 }
232 if(returnType == null) {
233 if(other.returnType != null) {
234 return false;
235 }
236 } else if(!returnType.equals(other.returnType)) {
237 return false;
238 }
239 return true;
240 }
241 }
242 }