First version after introduction of meaningful ids and Identifiable interface.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / ConfigImpl.java
index e29725e8655d2be5ad4fab667d4152dca68e37e8..c561e364b287958a169a90eb611baa58769ed3fc 100644 (file)
  */
 package org.wamblee.xmlrouter.impl;
 
+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.
@@ -30,19 +31,24 @@ import org.wamblee.xmlrouter.config.Config;
  * 
  * @param <T>
  */
-public abstract class ConfigImpl<T> implements ExtendedConfig<T> {
+public abstract class ConfigImpl<T extends Identifiable> implements
+    ExtendedConfig<T> {
 
-    private boolean dirty;
-    private AtomicLong next;
-    private Map<Id<T>, T> registered;
+    private Id<Config> id;
+    private List<T> registered;
 
     /**
      * Constructs the object.
      */
-    public ConfigImpl(AtomicLong aNext) {
-        dirty = false;
-        next = aNext;
-        registered = new LinkedHashMap<Id<T>, T>();
+    public ConfigImpl(Id<Config> aId) {
+        // TODO test for null.
+        id = aId;
+        registered = new ArrayList<T>();
+    }
+
+    @Override
+    public Id<Config> getId() {
+        return id;
     }
 
     /*
@@ -51,25 +57,20 @@ public abstract class ConfigImpl<T> implements ExtendedConfig<T> {
      * @see org.wamblee.xmlrouter.config.Config#add(T)
      */
     @Override
-    public synchronized Id<T> add(T aT) {
+    public synchronized void add(T aT) {
+        // TODO test duplicate ids.
         notNull(aT);
-        long seqno = next.incrementAndGet();
-        Id<T> id = new Id<T>(seqno);
-        registered.put(id, wrap(id, aT));
-        dirty = true;
-        return id;
+        registered.add(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<T> aId, T aT);
+    public abstract T wrap(T aT);
 
     /*
      * (non-Javadoc)
@@ -81,13 +82,20 @@ public abstract class ConfigImpl<T> implements ExtendedConfig<T> {
     @Override
     public synchronized boolean remove(Id<T> aId) {
         notNull(aId);
-        dirty = true;
-        return registered.remove(aId) != null;
+        Iterator<T> i = registered.iterator();
+        while (i.hasNext()) {
+            T t = i.next();
+            if (t.getId().equals(aId)) {
+                i.remove();
+                return true;
+            }
+        }
+        return false;
     }
 
     @Override
-    public Map<Id<T>, T> map() {
-        return Collections.unmodifiableMap(registered);
+    public List<T> values() {
+        return Collections.unmodifiableList(registered);
     }
 
     private void notNull(T aT) {