X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FConfigImpl.java;h=75b8222c04363038f3ed289eb5386a9c7843aded;hb=06368da424915190303bcb501f0cc4cd1b74a2f2;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..75b8222 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,16 @@ */ package org.wamblee.xmlrouter.impl; -import java.util.Collection; +import static org.wamblee.xmlrouter.impl.MessageUtil.*; + +import java.util.ArrayList; import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.concurrent.atomic.AtomicLong; +import java.util.Iterator; +import java.util.List; 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. @@ -31,79 +33,71 @@ import org.wamblee.xmlrouter.config.Config; * * @param */ -public abstract class ConfigImpl implements Config { +// TODO make sure that each item inside this config is prefixed with the id of +// the config. +public abstract class ConfigImpl> implements + ExtendedConfig { - private AtomicLong next; - private Map, T> registered; + private Id id; + private List 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 ArrayList(); } - /* - * (non-Javadoc) - * - * @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)); + public Id getId() { return id; } - public abstract T wrap(Id aId, T aT); - /* * (non-Javadoc) * - * @see - * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common - * .Id) + * @see org.wamblee.xmlrouter.config.Config#add(T) */ @Override - public boolean remove(Id aId) { - notNull(aId); - return registered.remove(aId) != null; + public synchronized void add(T aT) { + notNull("aT", aT); + registered.add(wrap(id.getId() + ".", aT)); } - /* - * (non-Javadoc) + /** + * This is called to wrap the given object by a safer version. * - * @see org.wamblee.xmlrouter.config.Config#ids() + * @param aT + * Object to wrap. + * @return Wrapped object. */ - @Override - public Collection> ids() { - return Collections.unmodifiableCollection(registered.keySet()); - } + public abstract T wrap(String aPrefix, T aT); /* * (non-Javadoc) * * @see - * org.wamblee.xmlrouter.config.Config#get(org.wamblee.xmlrouter.common.Id) + * org.wamblee.xmlrouter.config.Config#remove(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 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; + } } + return false; } - private void notNull(Id aId) { - if (aId == null) { - throw new NullPointerException("Id is null"); - } + @Override + public List values() { + return Collections.unmodifiableList(registered); } }