2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xmlrouter.impl;
18 import static org.wamblee.xmlrouter.impl.MessageUtil.*;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
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;
31 * Default implementation of the {@link Config} interface.
33 * @author Erik Brakkee
37 public abstract class ConfigImpl<T extends Identifiable<T>> implements
40 private Class<T> type;
41 private Id<Config> id;
42 private Map<Id<T>, T> registered;
45 * Constructs the object.
47 public ConfigImpl(Class<T> aType, Id<Config> aId) {
48 notNull("type", aType);
52 registered = new HashMap<Id<T>, T>();
56 public Class<T> getType() {
61 * Copies the config object.
66 public ConfigImpl(ConfigImpl<T> aConfig) {
67 notNull("config", aConfig);
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());
76 public Id<Config> getId() {
83 * @see org.wamblee.xmlrouter.config.Config#add(T)
86 public synchronized void add(T aT) {
88 if (registered.containsKey(aT.getId())) {
89 throw new ConfigException("Duplicate id '" + aT.getId() + "'");
91 registered.put(aT.getId(), wrap(aT));
95 * This is called to wrap the given object by a safer version.
99 * @return Wrapped object.
101 public abstract T wrap(T aT);
107 * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
111 public synchronized boolean remove(Id<T> aId) {
113 T value = registered.get(aId);
115 registered.remove(aId);
122 public Collection<T> values() {
123 return Collections.unmodifiableCollection(registered.values());
127 public boolean equals(Object aObj) {
131 if (!(aObj instanceof ConfigImpl)) {
134 ConfigImpl obj = (ConfigImpl) aObj;
135 return registered.keySet().equals(obj.registered.keySet());
139 public int hashCode() {
140 return registered.keySet().hashCode();