X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FConfigImpl.java;h=a1733ab41bf84a9b84cb3986f460586871bd763e;hb=f8027d76e1c3e517a8b80a3476f51adee845fc5b;hp=9442db674eb027575182da4f9675badbe76e07f4;hpb=7b7444ecb86a1af3eb11013e94776c2860205f2a;p=xmlrouter diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java index 9442db6..a1733ab 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java @@ -15,18 +15,18 @@ */ 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; -// TODO think real hard about the prefixing. We want a consistent view for clients. -// perhaps only provide a method to add items and hide all access to the ids. - /** * Default implementation of the {@link Config} interface. * @@ -34,21 +34,19 @@ import org.wamblee.xmlrouter.config.Identifiable; * * @param */ -// TODO make sure that each item inside this config is prefixed with the id of -// the config. -public abstract class ConfigImpl implements +public abstract class ConfigImpl> implements ExtendedConfig { private Id id; - private List registered; + private Map, T> registered; /** * Constructs the object. */ public ConfigImpl(Id aId) { - // TODO test for null. + notNull("id", aId); id = aId; - registered = new ArrayList(); + registered = new HashMap, T>(); } @Override @@ -63,9 +61,11 @@ public abstract class ConfigImpl implements */ @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)); } /** @@ -86,32 +86,34 @@ public abstract class ConfigImpl implements */ @Override public synchronized boolean remove(Id aId) { - notNull(aId); - Iterator 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 values() { - return Collections.unmodifiableList(registered); + public Collection 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 aId) { - if (aId == null) { - throw new NullPointerException("Id is null"); - } + @Override + public int hashCode() { + return registered.keySet().hashCode(); } }