-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathComposedMethodsFactory.java
63 lines (47 loc) · 1.72 KB
/
ComposedMethodsFactory.java
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
package openperipheral.adapter.composed;
import com.google.common.base.Predicate;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import openperipheral.adapter.AdapterRegistry;
import openperipheral.adapter.IMethodExecutor;
public abstract class ComposedMethodsFactory<T extends IMethodMap> {
public static class InvalidClassException extends RuntimeException {
private static final long serialVersionUID = 5722017683388067641L;
private InvalidClassException() {
super();
}
private InvalidClassException(Throwable cause) {
super(cause);
}
}
private final Map<Class<?>, T> classes = Maps.newHashMap();
private final Set<Class<?>> invalidClasses = Sets.newHashSet();
private final AdapterRegistry adapters;
private final ClassMethodsComposer composer;
public ComposedMethodsFactory(AdapterRegistry adapters, Predicate<IMethodExecutor> selector) {
this.adapters = adapters;
this.composer = new ClassMethodsComposer(selector);
}
public Map<Class<?>, T> listCollectedClasses() {
return Collections.unmodifiableMap(classes);
}
public T getAdaptedClass(Class<?> targetCls) {
if (invalidClasses.contains(targetCls)) throw new InvalidClassException();
T value = classes.get(targetCls);
if (value == null) {
try {
Map<String, IMethodExecutor> methods = composer.createMethodsList(targetCls, adapters);
value = wrapMethods(targetCls, methods);
} catch (Throwable t) {
invalidClasses.add(targetCls);
throw new InvalidClassException(t);
}
classes.put(targetCls, value);
}
return value;
}
protected abstract T wrapMethods(Class<?> targetCls, Map<String, IMethodExecutor> methods);
}