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