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.ArrayList;
19 import java.util.List;
20
21 import jp.liq.container.ContainerException.Reason;
22
23
24
25
26
27
28
29
30
31 public final class Container {
32 private final List<Module> modules;
33
34
35
36
37 public Container() {
38 this.modules = new ArrayList<Module>();
39 }
40
41
42
43
44
45 public void include(Module m) {
46 modules.add(m);
47 }
48
49
50
51
52
53
54 public void include(Container container) {
55 modules.addAll(container.modules);
56 }
57
58
59
60
61
62
63
64
65
66
67
68 public <T> T get(Class<T> type) throws ContainerException {
69
70 ComponentMetadata cm = getComponentMetadata(type);
71 if (cm == null) {
72 throw new ContainerException(Reason.COMPONENT_NOT_FOUND, type);
73 }
74 ContainerResolver resolver = new ContainerResolver(cm);
75 Object o = resolver.resolve();
76 return type.cast(o);
77 }
78
79 private <T> ComponentMetadata getComponentMetadata(Class<T> type) {
80 for (Module m : modules) {
81 ComponentMetadata cm = m.getComponentMetadata(type);
82 if (cm != null) {
83 return cm;
84 }
85 }
86 return null;
87 }
88
89 private class ContainerResolver extends Resolver {
90
91 ContainerResolver(ComponentMetadata info) {
92 super(info);
93 }
94
95 @Override
96 protected ComponentMetadata findComponentMetadata(Class<?> type) {
97 return getComponentMetadata(type);
98 }
99
100 }
101 }