equality based on the ids of the contents of SingleRouterConfig.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / ConfigImpl.java
index 980bd07601277b1c52b1580df5e44a8a2a7f135c..a1733ab41bf84a9b84cb3986f460586871bd763e 100644 (file)
@@ -17,13 +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.ConfigException;
 import org.wamblee.xmlrouter.config.Identifiable;
 
 /**
@@ -37,7 +38,7 @@ public abstract class ConfigImpl<T extends Identifiable<T>> implements
     ExtendedConfig<T> {
 
     private Id<Config> id;
-    private List<T> registered;
+    private Map<Id<T>, T> registered;
 
     /**
      * Constructs the object.
@@ -45,7 +46,7 @@ public abstract class ConfigImpl<T extends Identifiable<T>> implements
     public ConfigImpl(Id<Config> aId) {
         notNull("id", aId);
         id = aId;
-        registered = new ArrayList<T>();
+        registered = new HashMap<Id<T>, T>();
     }
 
     @Override
@@ -61,7 +62,10 @@ public abstract class ConfigImpl<T extends Identifiable<T>> 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(id.getId() + ".", aT));
     }
 
     /**
@@ -83,19 +87,33 @@ public abstract class ConfigImpl<T extends Identifiable<T>> implements
     @Override
     public synchronized boolean remove(Id<T> aId) {
         notNull("aId", aId);
-        Iterator<T> 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<T> values() {
-        return Collections.unmodifiableList(registered);
+    public Collection<T> 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();
     }
 }