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