Removed DOCUMENT ME comments that were generated and applied source code
[utils] / system / general / src / main / java / org / wamblee / system / core / DefaultScope.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.core;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 /**
26  * 
27  * @author $author$
28  * @version $Revision$
29  */
30 public class DefaultScope implements Scope {
31     private List<Scope> parents;
32
33     private Map<String, Object> properties;
34
35     private Map<String, Object> runtimes;
36
37     private Map<ProvidedInterface, ProvidedInterfaceImplementation> provided;
38
39     private List<ProvidedInterface> externallyProvided;
40
41     /**
42      * Creates a new DefaultScope object.
43      * 
44      */
45     public DefaultScope(List<ProvidedInterface> aExternallyProvided) {
46         this(aExternallyProvided.toArray(new ProvidedInterface[0]));
47     }
48
49     /**
50      * Creates a new DefaultScope object.
51      * 
52      */
53     public DefaultScope(ProvidedInterface[] aExternallyProvided) {
54         this(aExternallyProvided, new ArrayList<Scope>());
55     }
56
57     /**
58      * Creates a new DefaultScope object.
59      * 
60      */
61     public DefaultScope(ProvidedInterface[] aExternallyProvided, Scope aParent) {
62         this(aExternallyProvided, Arrays.asList(new Scope[] { aParent }));
63     }
64
65     /**
66      * Creates a new DefaultScope object.
67      * 
68      */
69     public DefaultScope(ProvidedInterface[] aExternallyProvided,
70         List<Scope> aParent) {
71         parents = new ArrayList<Scope>(aParent);
72         properties = new HashMap<String, Object>();
73         runtimes = new HashMap<String, Object>();
74         provided = new HashMap<ProvidedInterface, ProvidedInterfaceImplementation>();
75         externallyProvided = new ArrayList<ProvidedInterface>();
76         externallyProvided.addAll(Arrays.asList(aExternallyProvided));
77     }
78
79     @Override
80     public List<ProvidedInterface> getProvidedInterfaces() {
81         return Collections.unmodifiableList(externallyProvided);
82     }
83
84     @Override
85     public Object get(String aKey) {
86         return properties.get(aKey);
87     }
88
89     @Override
90     public void put(String aKey, Object aValue) {
91         properties.put(aKey, aValue);
92     }
93
94     @Override
95     public void addRuntime(Component aComponent, Object aRuntime) {
96         runtimes.put(aComponent.getName(), aRuntime);
97     }
98
99     @Override
100     public Object getRuntime(Component aComponent) {
101         return runtimes.get(aComponent.getName());
102     }
103
104     @Override
105     public Object getRuntime(String aName) {
106         return runtimes.get(aName);
107     }
108
109     @Override
110     synchronized public void publishInterface(ProvidedInterface aInterface,
111         Object aImplementation) {
112         provided.put(aInterface, new ProvidedInterfaceImplementation(
113             aInterface, aImplementation));
114     }
115
116     @Override
117     public <T> T getInterfaceImplementation(ProvidedInterface aInterface,
118         Class<T> aType) {
119         if (aInterface == null) {
120             return null;
121         }
122
123         ProvidedInterfaceImplementation providedIntf = provided.get(aInterface);
124
125         if (providedIntf == null) {
126             for (Scope parent : parents) {
127                 T impl = parent.getInterfaceImplementation(aInterface, aType);
128
129                 if (impl != null) {
130                     return impl;
131                 }
132             }
133
134             return null;
135         } else {
136             return providedIntf.getImplementation(aType);
137         }
138     }
139 }