added copying of SingleRouterConfig.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / ConfigImpl.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.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
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  * Default implementation of the {@link Config} interface.
32  * 
33  * @author Erik Brakkee
34  * 
35  * @param <T>
36  */
37 public abstract class ConfigImpl<T extends Identifiable<T>> implements
38     ExtendedConfig<T> {
39
40     private Id<Config> id;
41     private Map<Id<T>, T> registered;
42
43     /**
44      * Constructs the object.
45      */
46     public ConfigImpl(Id<Config> aId) {
47         notNull("id", aId);
48         id = aId;
49         registered = new HashMap<Id<T>, T>();
50     }
51
52     /**
53      * Copies the config object.
54      * 
55      * @param aConfig
56      *            Config to copy.
57      */
58     public ConfigImpl(ConfigImpl<T> aConfig) {
59         notNull("config", aConfig);
60         id = aConfig.id;
61         registered = new HashMap<Id<T>, T>();
62         for (Map.Entry<Id<T>, T> entry : aConfig.registered.entrySet()) {
63             registered.put(entry.getKey(), entry.getValue());
64         }
65     }
66
67     @Override
68     public Id<Config> getId() {
69         return id;
70     }
71
72     /*
73      * (non-Javadoc)
74      * 
75      * @see org.wamblee.xmlrouter.config.Config#add(T)
76      */
77     @Override
78     public synchronized void add(T aT) {
79         notNull("aT", aT);
80         if (registered.containsKey(aT.getId())) {
81             throw new ConfigException("Duplicate id '" + aT.getId() + "'");
82         }
83         registered.put(aT.getId(), wrap(id.getId() + ".", aT));
84     }
85
86     /**
87      * This is called to wrap the given object by a safer version.
88      * 
89      * @param aT
90      *            Object to wrap.
91      * @return Wrapped object.
92      */
93     public abstract T wrap(String aPrefix, T aT);
94
95     /*
96      * (non-Javadoc)
97      * 
98      * @see
99      * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
100      * .Id)
101      */
102     @Override
103     public synchronized boolean remove(Id<T> aId) {
104         notNull("aId", aId);
105         T value = registered.get(aId);
106         if (value != null) {
107             registered.remove(aId);
108             return true;
109         }
110         return false;
111     }
112
113     @Override
114     public Collection<T> values() {
115         return Collections.unmodifiableCollection(registered.values());
116     }
117
118     @Override
119     public boolean equals(Object aObj) {
120         if (aObj == null) {
121             return false;
122         }
123         if (!(aObj instanceof ConfigImpl)) {
124             return false;
125         }
126         ConfigImpl obj = (ConfigImpl) aObj;
127         return registered.keySet().equals(obj.registered.keySet());
128     }
129
130     @Override
131     public int hashCode() {
132         return registered.keySet().hashCode();
133     }
134 }