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=789f75c5178db2da3579c10cd14f1fd96c193932;hpb=b2375f35a2f897e1417e8b5ec5b19b3257a11586;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 789f75c..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,14 +15,17 @@ */ 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.ConfigException; +import org.wamblee.xmlrouter.config.Identifiable; /** * Default implementation of the {@link Config} interface. @@ -31,17 +34,24 @@ import org.wamblee.xmlrouter.config.Config; * * @param */ -public abstract class ConfigImpl implements Config { +public abstract class ConfigImpl> implements + ExtendedConfig { - private AtomicLong next; + private Id id; private Map, T> registered; /** * Constructs the object. */ - public ConfigImpl() { - next = new AtomicLong(1); - registered = new LinkedHashMap, T>(); + public ConfigImpl(Id aId) { + notNull("id", aId); + id = aId; + registered = new HashMap, T>(); + } + + @Override + public Id getId() { + return id; } /* @@ -50,15 +60,22 @@ public abstract class ConfigImpl implements Config { * @see org.wamblee.xmlrouter.config.Config#add(T) */ @Override - public Id add(T aT) { - notNull(aT); - long seqno = next.incrementAndGet(); - Id id = new Id(seqno); - registered.put(id, wrap(id, aT)); - 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(id.getId() + ".", aT)); } - public abstract T wrap(Id aId, T aT); + /** + * This is called to wrap the given object by a safer version. + * + * @param aT + * Object to wrap. + * @return Wrapped object. + */ + public abstract T wrap(String aPrefix, T aT); /* * (non-Javadoc) @@ -68,42 +85,35 @@ public abstract class ConfigImpl implements Config { * .Id) */ @Override - public boolean remove(Id aId) { - notNull(aId); - return registered.remove(aId) != null; + public synchronized boolean remove(Id aId) { + notNull("aId", aId); + T value = registered.get(aId); + if (value != null) { + registered.remove(aId); + return true; + } + return false; } - /* - * (non-Javadoc) - * - * @see org.wamblee.xmlrouter.config.Config#ids() - */ @Override - public Collection> ids() { - return Collections.unmodifiableCollection(registered.keySet()); + public Collection values() { + return Collections.unmodifiableCollection(registered.values()); } - /* - * (non-Javadoc) - * - * @see - * org.wamblee.xmlrouter.config.Config#get(org.wamblee.xmlrouter.common.Id) - */ @Override - public T get(Id aId) { - notNull(aId); - return registered.get(aId); - } - - private void notNull(T aT) { - if (aT == null) { - throw new NullPointerException("Object is null"); + 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(); } }