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;
-// TODO think real hard about the prefixing. We want a consistent view for clients.
-// perhaps only provide a method to add items and hide all access to the ids.
-
/**
* Default implementation of the {@link Config} interface.
*
*
* @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<T>> implements
ExtendedConfig<T> {
private Id<Config> id;
- private List<T> registered;
+ private Map<Id<T>, T> registered;
/**
* Constructs the object.
*/
public ConfigImpl(Id<Config> aId) {
- // TODO test for null.
+ notNull("id", aId);
id = aId;
- registered = new ArrayList<T>();
+ registered = new HashMap<Id<T>, T>();
}
@Override
@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));
}
/**
@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();
}
}