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=e29725e8655d2be5ad4fab667d4152dca68e37e8;hpb=f70baadfd579f4d3aa2e8c9ee7d758fb37d7872f;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 e29725e..ae2351f 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java @@ -15,13 +15,15 @@ */ package org.wamblee.xmlrouter.impl; +import static org.wamblee.xmlrouter.impl.MessageUtil.*; + +import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; import org.wamblee.xmlrouter.common.Id; -import org.wamblee.xmlrouter.config.Config; +import org.wamblee.xmlrouter.config.Identifiable; /** * Default implementation of the {@link Config} interface. @@ -30,19 +32,47 @@ import org.wamblee.xmlrouter.config.Config; * * @param */ -public abstract class ConfigImpl implements ExtendedConfig { +public abstract class ConfigImpl> implements + ExtendedConfig { - private boolean dirty; - private AtomicLong next; + private Class type; + private String prefix; private Map, T> registered; /** * Constructs the object. */ - public ConfigImpl(AtomicLong aNext) { - dirty = false; - next = aNext; - registered = new LinkedHashMap, T>(); + 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 String getPrefix() { + return prefix; } /* @@ -51,25 +81,22 @@ public abstract class ConfigImpl implements ExtendedConfig { * @see org.wamblee.xmlrouter.config.Config#add(T) */ @Override - public synchronized Id add(T aT) { - notNull(aT); - long seqno = next.incrementAndGet(); - Id id = new Id(seqno); - registered.put(id, wrap(id, aT)); - dirty = true; - return id; + public synchronized void add(T aT) { + notNull("aT", aT); + if (registered.containsKey(aT.getId())) { + throw new ConfigException("Duplicate id '" + aT.getId() + "'"); + } + registered.put(aT.getId(), wrap(aT)); } /** * This is called to wrap the given object by a safer version. * - * @param aId - * Id. * @param aT * Object to wrap. * @return Wrapped object. */ - public abstract T wrap(Id aId, T aT); + public abstract T wrap(T aT); /* * (non-Javadoc) @@ -80,25 +107,34 @@ public abstract class ConfigImpl implements ExtendedConfig { */ @Override public synchronized boolean remove(Id aId) { - notNull(aId); - dirty = true; - return registered.remove(aId) != null; + notNull("aId", aId); + T value = registered.get(aId); + if (value != null) { + registered.remove(aId); + return true; + } + return false; } @Override - public Map, T> map() { - return Collections.unmodifiableMap(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(); } }