777ecd0ac2c45864328605eeff9955419197673a
[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 Class<T> type;
41     private Id<Config> id;
42     private Map<Id<T>, T> registered;
43
44     /**
45      * Constructs the object.
46      */
47     public ConfigImpl(Class<T> aType, Id<Config> aId) {
48         notNull("type", aType);
49         notNull("id", aId);
50         type = aType;
51         id = aId;
52         registered = new HashMap<Id<T>, T>();
53     }
54
55     @Override
56     public Class<T> getType() {
57         return type;
58     }
59
60     /**
61      * Copies the config object.
62      * 
63      * @param aConfig
64      *            Config to copy.
65      */
66     public ConfigImpl(ConfigImpl<T> aConfig) {
67         notNull("config", aConfig);
68         id = aConfig.id;
69         registered = new HashMap<Id<T>, T>();
70         for (Map.Entry<Id<T>, T> entry : aConfig.registered.entrySet()) {
71             registered.put(entry.getKey(), entry.getValue());
72         }
73     }
74
75     @Override
76     public Id<Config> getId() {
77         return id;
78     }
79
80     /*
81      * (non-Javadoc)
82      * 
83      * @see org.wamblee.xmlrouter.config.Config#add(T)
84      */
85     @Override
86     public synchronized void add(T aT) {
87         notNull("aT", aT);
88         if (registered.containsKey(aT.getId())) {
89             throw new ConfigException("Duplicate id '" + aT.getId() + "'");
90         }
91         registered.put(aT.getId(), wrap(aT));
92     }
93
94     /**
95      * This is called to wrap the given object by a safer version.
96      * 
97      * @param aT
98      *            Object to wrap.
99      * @return Wrapped object.
100      */
101     public abstract T wrap(T aT);
102
103     /*
104      * (non-Javadoc)
105      * 
106      * @see
107      * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
108      * .Id)
109      */
110     @Override
111     public synchronized boolean remove(Id<T> aId) {
112         notNull("aId", aId);
113         T value = registered.get(aId);
114         if (value != null) {
115             registered.remove(aId);
116             return true;
117         }
118         return false;
119     }
120
121     @Override
122     public Collection<T> values() {
123         return Collections.unmodifiableCollection(registered.values());
124     }
125
126     @Override
127     public boolean equals(Object aObj) {
128         if (aObj == null) {
129             return false;
130         }
131         if (!(aObj instanceof ConfigImpl)) {
132             return false;
133         }
134         ConfigImpl obj = (ConfigImpl) aObj;
135         return registered.keySet().equals(obj.registered.keySet());
136     }
137
138     @Override
139     public int hashCode() {
140         return registered.keySet().hashCode();
141     }
142 }