0c6100dc3fc82c2c9a4484bc2af21e258e84d7a0
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / CompositeConfig.java
1 /*
2  * Copyright 2005-2011 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.xmlrouter.impl;
17
18 import static org.wamblee.xmlrouter.impl.MessageUtil.*;
19
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24
25 import org.wamblee.xmlrouter.common.Id;
26 import org.wamblee.xmlrouter.config.Config;
27 import org.wamblee.xmlrouter.config.ConfigException;
28 import org.wamblee.xmlrouter.config.Identifiable;
29
30 /**
31  * Composite config. The composite config
32  * 
33  * @author Erik Brakkee
34  * 
35  * @param <T>
36  */
37 public class CompositeConfig<T extends Identifiable<T>> implements
38     ExtendedConfig<T> {
39
40     private static final Id<Config> ID = new Id<Config>("compositeconfig");
41     private static final String READ_ONLY_INSTANCE = "read only instance";
42
43     private Class<T> type;
44     private Set<Id<Config>> ids;
45     private List<Id<T>> valueIds;
46     private List<T> values;
47
48     public CompositeConfig(Class<T> aType) {
49         type = aType;
50         ids = new HashSet<Id<Config>>();
51         valueIds = new ArrayList<Id<T>>();
52         values = new ArrayList<T>();
53     }
54
55     @Override
56     public Class<T> getType() {
57         return type;
58     }
59
60     @Override
61     public Id<Config> getId() {
62         return ID;
63     }
64
65     public void addConfig(Config<T> aConfig) {
66         notNull("aConfig", aConfig);
67         if (ids.contains(aConfig.getId())) {
68             throw new ConfigException("duplicate id '" +
69                 aConfig.getId().toString() + "'");
70         }
71         String prefix = aConfig.getId().getId() + ".";
72         for (T item : aConfig.values()) {
73             Id<T> newId = new Id<T>(prefix + item.getId());
74             if (valueIds.contains(newId)) {
75                 throw new ConfigException("duplicate id '" +
76                     item.getId().toString() + "'");
77             }
78         }
79
80         ids.add(aConfig.getId());
81
82         for (T item : aConfig.values()) {
83             Id<T> newId = new Id<T>(prefix + item.getId());
84             valueIds.add(newId);
85             values.add(IdentifiablePrefixProxyFactory.getProxy(prefix, item,
86                 Identifiable.class, aConfig.getType()));
87         }
88     }
89
90     @Override
91     public List<T> values() {
92         return values;
93     }
94
95     @Override
96     public boolean remove(Id<T> aId) {
97         notSupported(READ_ONLY_INSTANCE);
98         return false;
99     }
100
101     @Override
102     public void add(T aT) {
103         notSupported(READ_ONLY_INSTANCE);
104     }
105 }