a686f88cec94cd57f187e3c3ca26eed80fa93a22
[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 java.util.ArrayList;
19 import java.util.List;
20
21 import org.wamblee.system.core.DefaultProvidedInterface;
22 import org.wamblee.system.core.ProvidedInterface;
23 import org.wamblee.system.core.RequiredInterface;
24 import org.wamblee.system.core.Scope;
25
26 /**
27  * The class configuration encapsulates the knowledge of how to wrap a class as a component.
28  * 
29  * @author Erik Brakkee
30  *
31  */
32 public class ClassConfiguration {
33
34         private Class _class; 
35         private ConstructorConfiguration constructorConfig;
36         private ObjectConfiguration objectConfig; 
37         
38         /**
39          * Constructs the configuration. By default no constructor is selected and 
40          * one of {@link #select(Class...)} or 
41          * {@link #greedy()} must be called.  
42          * @param aClass Class to construct. 
43          */
44         public ClassConfiguration(Class aClass) {
45                 _class = aClass; 
46                 constructorConfig = new ConstructorConfiguration(aClass);
47                 objectConfig = new ObjectConfiguration(aClass);
48         }
49         
50         public ConstructorConfiguration getConstructorConfig() {
51                 return constructorConfig;
52         }
53         
54         public ObjectConfiguration getObjectConfig() { 
55             return objectConfig; 
56         }
57
58         /**
59          * Creates the object in the given scope. 
60          * @param aScope Scope containing required interfaces for this object. 
61          * @return object. 
62          */
63         public Object create(Scope aScope) {
64                 return constructorConfig.create(aScope);
65         }
66         
67         /**
68          * Injects required interfaces through the setters 
69          * @param aObject Object to inject into. 
70          * @param aScope Scope in which injection takes place. 
71          */
72         public void inject(Scope aScope, Object aObject) { 
73             objectConfig.inject(aScope, aObject);
74         }
75         
76         public List<ProvidedInterface> getProvidedInterfaces() {
77             List<ProvidedInterface> result = new ArrayList<ProvidedInterface>();
78             result.add(new DefaultProvidedInterface("provided", _class));       
79             return result;
80         }
81         
82         public List<RequiredInterface> getRequiredInterfaces() { 
83             List<RequiredInterface> result = new ArrayList<RequiredInterface>();
84             result.addAll(constructorConfig.getRequiredInterfaces());
85             result.addAll(objectConfig.getRequiredInterfaces());
86             return result; 
87         }
88 }