X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FConfigImpl.java;h=ae2351f97d37c2e4ae08c5dd0d7a7d5ea090352a;hb=3b2c669b25bfcb5a3c3f06ff9180d85143bebb2a;hp=0324e78f8353b1dd58598e49b66ecca786b6552d;hpb=03a6b404471945aed9d48fc1e5b8447b4a9d9413;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 0324e78..ae2351f 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java @@ -17,18 +17,14 @@ package org.wamblee.xmlrouter.impl; import static org.wamblee.xmlrouter.impl.MessageUtil.*; -import java.util.ArrayList; +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.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. * @@ -36,26 +32,47 @@ 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 ExtendedConfig { - private Id id; - private List registered; + private Class type; + private String prefix; + private Map, T> registered; /** * Constructs the object. */ - public ConfigImpl(Id aId) { - // TODO test for null. - id = aId; - registered = new ArrayList(); + public ConfigImpl(Class aType, String aPrefix) { + notNull("type", aType); + notNull("id", aPrefix); + type = aType; + prefix = aPrefix; + registered = new HashMap, T>(); + } + + @Override + public Class getType() { + return type; + } + + /** + * Copies the config object. + * + * @param aConfig + * Config to copy. + */ + public ConfigImpl(ConfigImpl aConfig) { + notNull("config", aConfig); + prefix = aConfig.prefix; + registered = new HashMap, T>(); + for (Map.Entry, T> entry : aConfig.registered.entrySet()) { + registered.put(entry.getKey(), entry.getValue()); + } } @Override - public Id getId() { - return id; + public String getPrefix() { + return prefix; } /* @@ -66,7 +83,10 @@ public abstract class ConfigImpl> implements @Override public synchronized void add(T aT) { notNull("aT", aT); - registered.add(wrap(id.getId() + ".", aT)); + if (registered.containsKey(aT.getId())) { + throw new ConfigException("Duplicate id '" + aT.getId() + "'"); + } + registered.put(aT.getId(), wrap(aT)); } /** @@ -76,7 +96,7 @@ public abstract class ConfigImpl> implements * Object to wrap. * @return Wrapped object. */ - public abstract T wrap(String aPrefix, T aT); + public abstract T wrap(T aT); /* * (non-Javadoc) @@ -88,19 +108,33 @@ public abstract class ConfigImpl> implements @Override public synchronized boolean remove(Id aId) { notNull("aId", aId); - Iterator i = registered.iterator(); - while (i.hasNext()) { - T t = i.next(); - if (t.getId().equals(aId)) { - i.remove(); - return true; - } + 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()); + } + + @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()); + } + + @Override + public int hashCode() { + return registered.keySet().hashCode(); } }