Config no longer implements Identifiable because this was in violation of the contrac...
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / ConfigImpl.java
index 5beeed5c12da92ece9bb9833d39bbe0c0bc2241e..ae2351f97d37c2e4ae08c5dd0d7a7d5ea090352a 100644 (file)
  */
 package org.wamblee.xmlrouter.impl;
 
-import java.util.ArrayList;
+import static org.wamblee.xmlrouter.impl.MessageUtil.*;
+
+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.Identifiable;
 
 /**
@@ -31,26 +32,47 @@ import org.wamblee.xmlrouter.config.Identifiable;
  * 
  * @param <T>
  */
-// TODO make sure that each item inside this config is prefixed with the id of
-// the config.
-public abstract class ConfigImpl<T extends Identifiable> implements
+public abstract class ConfigImpl<T extends Identifiable<T>> implements
     ExtendedConfig<T> {
 
-    private Id<Config> id;
-    private List<T> registered;
+    private Class<T> type;
+    private String prefix;
+    private Map<Id<T>, T> registered;
 
     /**
      * Constructs the object.
      */
-    public ConfigImpl(Id<Config> aId) {
-        // TODO test for null.
-        id = aId;
-        registered = new ArrayList<T>();
+    public ConfigImpl(Class<T> aType, String aPrefix) {
+        notNull("type", aType);
+        notNull("id", aPrefix);
+        type = aType;
+        prefix = aPrefix;
+        registered = new HashMap<Id<T>, T>();
     }
 
     @Override
-    public Id<Config> getId() {
-        return id;
+    public Class<T> getType() {
+        return type;
+    }
+
+    /**
+     * Copies the config object.
+     * 
+     * @param aConfig
+     *            Config to copy.
+     */
+    public ConfigImpl(ConfigImpl<T> aConfig) {
+        notNull("config", aConfig);
+        prefix = aConfig.prefix;
+        registered = new HashMap<Id<T>, T>();
+        for (Map.Entry<Id<T>, T> entry : aConfig.registered.entrySet()) {
+            registered.put(entry.getKey(), entry.getValue());
+        }
+    }
+
+    @Override
+    public String getPrefix() {
+        return prefix;
     }
 
     /*
@@ -60,9 +82,11 @@ public abstract class ConfigImpl<T extends Identifiable> implements
      */
     @Override
     public synchronized void add(T aT) {
-        // TODO test duplicate ids.
-        notNull(aT);
-        registered.add(wrap(id.getId() + ".", aT));
+        notNull("aT", aT);
+        if (registered.containsKey(aT.getId())) {
+            throw new ConfigException("Duplicate id '" + aT.getId() + "'");
+        }
+        registered.put(aT.getId(), wrap(aT));
     }
 
     /**
@@ -72,7 +96,7 @@ public abstract class ConfigImpl<T extends Identifiable> implements
      *            Object to wrap.
      * @return Wrapped object.
      */
-    public abstract T wrap(String aPrefix, T aT);
+    public abstract T wrap(T aT);
 
     /*
      * (non-Javadoc)
@@ -83,32 +107,34 @@ public abstract class ConfigImpl<T extends Identifiable> implements
      */
     @Override
     public synchronized boolean remove(Id<T> aId) {
-        notNull(aId);
-        Iterator<T> i = registered.iterator();
-        while (i.hasNext()) {
-            T t = i.next();
-            if (t.getId().equals(aId)) {
-                i.remove();
-                return true;
-            }
+        notNull("aId", aId);
+        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());
     }
 
-    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<T> aId) {
-        if (aId == null) {
-            throw new NullPointerException("Id is null");
-        }
+    @Override
+    public int hashCode() {
+        return registered.keySet().hashCode();
     }
 }