1c8760bb8ae77fd08c54bfbdbb433b6ef4c72693
[utils] / system / general / src / main / java / org / wamblee / system / adapters / ClassConfiguration.java
1 /*
2  * Copyright 2008 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.system.adapters;
17
18 import org.wamblee.system.core.DefaultProvidedInterface;
19 import org.wamblee.system.core.ProvidedInterface;
20 import org.wamblee.system.core.RequiredInterface;
21 import org.wamblee.system.core.Scope;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26
27 /**
28  * The class configuration encapsulates the knowledge of how to wrap a
29  * class as a component.
30  *
31  * @author Erik Brakkee
32  */
33 public class ClassConfiguration {
34     /**
35      * DOCUMENT ME!
36      */
37     private Class _class;
38
39     /**
40      * DOCUMENT ME!
41      */
42     private ConstructorConfiguration constructorConfig;
43
44     /**
45      * DOCUMENT ME!
46      */
47     private ObjectConfiguration objectConfig;
48
49 /**
50          * Constructs the configuration. By default no constructor is selected and 
51          * one of {@link #select(Class...)} or 
52          * {@link #greedy()} must be called.  
53          * @param aClass Class to construct. 
54          */
55     public ClassConfiguration(Class aClass) {
56         _class                = aClass;
57         constructorConfig     = new ConstructorConfiguration(aClass);
58         objectConfig          = new ObjectConfiguration(aClass);
59     }
60
61     /**
62      * DOCUMENT ME!
63      *
64      * @return DOCUMENT ME!
65      */
66     public ConstructorConfiguration getConstructorConfig() {
67         return constructorConfig;
68     }
69
70     /**
71      * DOCUMENT ME!
72      *
73      * @return DOCUMENT ME!
74      */
75     public ObjectConfiguration getObjectConfig() {
76         return objectConfig;
77     }
78
79     /**
80      * Creates the object in the given scope.
81      *
82      * @param aScope Scope containing required interfaces for this object.
83      *
84      * @return object.
85      */
86     public Object create(Scope aScope) {
87         return constructorConfig.create(aScope);
88     }
89
90     /**
91      * Injects required interfaces through the setters
92      *
93      * @param aScope Scope in which injection takes place.
94      * @param aObject Object to inject into.
95      */
96     public void inject(Scope aScope, Object aObject) {
97         objectConfig.inject(aScope, aObject);
98     }
99
100     /**
101      * DOCUMENT ME!
102      *
103      * @return DOCUMENT ME!
104      */
105     public List<ProvidedInterface> getProvidedInterfaces() {
106         List<ProvidedInterface> result = new ArrayList<ProvidedInterface>();
107         result.add(new DefaultProvidedInterface("provided", _class));
108
109         return result;
110     }
111
112     /**
113      * DOCUMENT ME!
114      *
115      * @return DOCUMENT ME!
116      */
117     public List<RequiredInterface> getRequiredInterfaces() {
118         List<RequiredInterface> result = new ArrayList<RequiredInterface>();
119         result.addAll(constructorConfig.getRequiredInterfaces());
120         result.addAll(objectConfig.getRequiredInterfaces());
121
122         return result;
123     }
124 }