*/
package org.wamblee.xmlrouter.impl;
-import java.util.ArrayList;
+import static org.wamblee.xmlrouter.impl.MessageUtil.*;
+
+import java.util.Collection;
import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.Config;
+import org.wamblee.xmlrouter.config.ConfigException;
import org.wamblee.xmlrouter.config.Identifiable;
/**
*
* @param <T>
*/
-// TODO make sure that each item inside this config is prefixed with the id of
-// the config.
-public abstract class ConfigImpl<T extends Identifiable> implements
+public abstract class ConfigImpl<T extends Identifiable<T>> implements
ExtendedConfig<T> {
private Id<Config> id;
- private List<T> registered;
+ private Map<Id<T>, T> registered;
/**
* Constructs the object.
*/
public ConfigImpl(Id<Config> aId) {
- // TODO test for null.
+ notNull("id", aId);
id = aId;
- registered = new ArrayList<T>();
+ registered = new HashMap<Id<T>, T>();
}
@Override
*/
@Override
public synchronized void add(T aT) {
- // TODO test duplicate ids.
- notNull(aT);
- registered.add(wrap(id.getId() + ".", aT));
+ notNull("aT", aT);
+ if (registered.containsKey(aT.getId())) {
+ throw new ConfigException("Duplicate id '" + aT.getId() + "'");
+ }
+ registered.put(aT.getId(), wrap(id.getId() + ".", aT));
}
/**
*/
@Override
public synchronized boolean remove(Id<T> aId) {
- notNull(aId);
- Iterator<T> i = registered.iterator();
- while (i.hasNext()) {
- T t = i.next();
- if (t.getId().equals(aId)) {
- i.remove();
- return true;
- }
+ notNull("aId", aId);
+ T value = registered.get(aId);
+ if (value != null) {
+ registered.remove(aId);
+ return true;
}
return false;
}
@Override
- public List<T> values() {
- return Collections.unmodifiableList(registered);
+ public Collection<T> values() {
+ return Collections.unmodifiableCollection(registered.values());
}
- private void notNull(T aT) {
- if (aT == null) {
- throw new NullPointerException("Object is null");
+ @Override
+ public boolean equals(Object aObj) {
+ if (aObj == null) {
+ return false;
}
+ if (!(aObj instanceof ConfigImpl)) {
+ return false;
+ }
+ ConfigImpl obj = (ConfigImpl) aObj;
+ return registered.keySet().equals(obj.registered.keySet());
}
- private void notNull(Id<T> aId) {
- if (aId == null) {
- throw new NullPointerException("Id is null");
- }
+ @Override
+ public int hashCode() {
+ return registered.keySet().hashCode();
}
}