id prefixes are now done at the very last moment instead of when an item is added.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / CompositeConfig.java
index 2523ef8d652b76752251f340c05e60b88ceca7a4..0c6100dc3fc82c2c9a4484bc2af21e258e84d7a0 100644 (file)
@@ -24,7 +24,7 @@ import java.util.Set;
 
 import org.wamblee.xmlrouter.common.Id;
 import org.wamblee.xmlrouter.config.Config;
-import org.wamblee.xmlrouter.config.DuplicateException;
+import org.wamblee.xmlrouter.config.ConfigException;
 import org.wamblee.xmlrouter.config.Identifiable;
 
 /**
@@ -37,40 +37,53 @@ import org.wamblee.xmlrouter.config.Identifiable;
 public class CompositeConfig<T extends Identifiable<T>> implements
     ExtendedConfig<T> {
 
+    private static final Id<Config> ID = new Id<Config>("compositeconfig");
     private static final String READ_ONLY_INSTANCE = "read only instance";
+
+    private Class<T> type;
     private Set<Id<Config>> ids;
-    private Id<Config> id;
     private List<Id<T>> valueIds;
     private List<T> values;
 
-    public CompositeConfig(Id<Config> aId) {
-        notNull("aId", aId);
+    public CompositeConfig(Class<T> aType) {
+        type = aType;
         ids = new HashSet<Id<Config>>();
-        id = aId;
         valueIds = new ArrayList<Id<T>>();
         values = new ArrayList<T>();
     }
 
+    @Override
+    public Class<T> getType() {
+        return type;
+    }
+
     @Override
     public Id<Config> getId() {
-        return id;
+        return ID;
     }
 
     public void addConfig(Config<T> aConfig) {
         notNull("aConfig", aConfig);
         if (ids.contains(aConfig.getId())) {
-            throw new DuplicateException(aConfig.getId().toString());
+            throw new ConfigException("duplicate id '" +
+                aConfig.getId().toString() + "'");
         }
+        String prefix = aConfig.getId().getId() + ".";
         for (T item : aConfig.values()) {
-            if (valueIds.contains(item.getId())) {
-                throw new DuplicateException(item.getId().toString());
+            Id<T> newId = new Id<T>(prefix + item.getId());
+            if (valueIds.contains(newId)) {
+                throw new ConfigException("duplicate id '" +
+                    item.getId().toString() + "'");
             }
         }
 
         ids.add(aConfig.getId());
+
         for (T item : aConfig.values()) {
-            valueIds.add(item.getId());
-            values.add(item);
+            Id<T> newId = new Id<T>(prefix + item.getId());
+            valueIds.add(newId);
+            values.add(IdentifiablePrefixProxyFactory.getProxy(prefix, item,
+                Identifiable.class, aConfig.getType()));
         }
     }