1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package jp.liq.container;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import jp.liq.container.ContainerException.Reason;
22
23
24
25
26
27 public abstract class Module {
28 private final Map<Class<?>, ComponentMetadata> components;
29
30
31
32
33 public Module() {
34 this.components = new HashMap<Class<?>, ComponentMetadata>();
35 }
36
37 synchronized <T> ComponentMetadata getComponentMetadata(Class<T> type) {
38 ComponentMetadata cm = components.get(type);
39 if(cm == null) {
40 Component<?> component = createComponent(type);
41 if(component == null) {
42 return null;
43 }
44 cm = new ComponentMetadata(component, this);
45 components.put(type, cm);
46 }
47 return cm;
48 }
49
50
51
52
53
54 protected <T> void addComponent(Component<T> component) {
55 Class<T> type = component.getType();
56 if(components.containsKey(type)) {
57 throw new ContainerException(Reason.DUPLICATE_REGISTRATION, type, component,
58 components.get(type).getComponent());
59 }
60 components.put(type, new ComponentMetadata(component, this));
61 }
62
63
64
65
66
67 protected <T> Component<T> createComponent(Class<T> type) {
68 return null;
69 }
70 }