RobustIdentifiable implemented and tested + test impacts.
[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 String READ_ONLY_INSTANCE = "read only instance";
41     private Set<Id<Config>> ids;
42     private Id<Config> id;
43     private List<Id<T>> valueIds;
44     private List<T> values;
45
46     public CompositeConfig(Id<Config> aId) {
47         notNull("aId", aId);
48         ids = new HashSet<Id<Config>>();
49         id = aId;
50         valueIds = new ArrayList<Id<T>>();
51         values = new ArrayList<T>();
52     }
53
54     @Override
55     public Id<Config> getId() {
56         return id;
57     }
58
59     public void addConfig(Config<T> aConfig) {
60         notNull("aConfig", aConfig);
61         if (ids.contains(aConfig.getId())) {
62             throw new ConfigException("duplicate id '" +
63                 aConfig.getId().toString() + "'");
64         }
65         for (T item : aConfig.values()) {
66             if (valueIds.contains(item.getId())) {
67                 throw new ConfigException("duplicate id '" +
68                     item.getId().toString() + "'");
69             }
70         }
71
72         ids.add(aConfig.getId());
73         for (T item : aConfig.values()) {
74             valueIds.add(item.getId());
75             values.add(item);
76         }
77     }
78
79     @Override
80     public List<T> values() {
81         return values;
82     }
83
84     @Override
85     public boolean remove(Id<T> aId) {
86         notSupported(READ_ONLY_INSTANCE);
87         return false;
88     }
89
90     @Override
91     public void add(T aT) {
92         notSupported(READ_ONLY_INSTANCE);
93     }
94 }