*/
package org.wamblee.xmlrouter.impl;
+import static org.wamblee.xmlrouter.impl.MessageUtil.*;
+
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedHashMap;
+import java.util.HashMap;
import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.Config;
+import org.wamblee.xmlrouter.config.ConfigException;
+import org.wamblee.xmlrouter.config.Identifiable;
/**
* Default implementation of the {@link Config} interface.
*
* @param <T>
*/
-public abstract class ConfigImpl<T> implements Config<T> {
+public abstract class ConfigImpl<T extends Identifiable<T>> implements
+ ExtendedConfig<T> {
- private AtomicLong next;
+ private Id<Config> id;
private Map<Id<T>, T> registered;
/**
* Constructs the object.
*/
- public ConfigImpl() {
- next = new AtomicLong(1);
- registered = new LinkedHashMap<Id<T>, T>();
+ public ConfigImpl(Id<Config> aId) {
+ notNull("id", aId);
+ id = aId;
+ registered = new HashMap<Id<T>, T>();
+ }
+
+ @Override
+ public Id<Config> getId() {
+ return id;
}
/*
* @see org.wamblee.xmlrouter.config.Config#add(T)
*/
@Override
- public Id<T> add(T aT) {
- notNull(aT);
- long seqno = next.incrementAndGet();
- Id<T> id = new Id<T>(seqno);
- registered.put(id, wrap(id, aT));
- return id;
+ public synchronized void add(T aT) {
+ notNull("aT", aT);
+ if (registered.containsKey(aT.getId())) {
+ throw new ConfigException("Duplicate id '" + aT.getId() + "'");
+ }
+ registered.put(aT.getId(), wrap(id.getId() + ".", aT));
}
- public abstract T wrap(Id<T> aId, T aT);
+ /**
+ * This is called to wrap the given object by a safer version.
+ *
+ * @param aT
+ * Object to wrap.
+ * @return Wrapped object.
+ */
+ public abstract T wrap(String aPrefix, T aT);
/*
* (non-Javadoc)
* .Id)
*/
@Override
- public boolean remove(Id<T> aId) {
- notNull(aId);
- return registered.remove(aId) != null;
+ public synchronized boolean remove(Id<T> aId) {
+ notNull("aId", aId);
+ T value = registered.get(aId);
+ if (value != null) {
+ registered.remove(aId);
+ return true;
+ }
+ return false;
}
- /*
- * (non-Javadoc)
- *
- * @see org.wamblee.xmlrouter.config.Config#ids()
- */
@Override
- public Collection<Id<T>> ids() {
- return Collections.unmodifiableCollection(registered.keySet());
+ public Collection<T> values() {
+ return Collections.unmodifiableCollection(registered.values());
}
- /*
- * (non-Javadoc)
- *
- * @see
- * org.wamblee.xmlrouter.config.Config#get(org.wamblee.xmlrouter.common.Id)
- */
@Override
- public T get(Id<T> aId) {
- notNull(aId);
- return registered.get(aId);
- }
-
- private void notNull(T aT) {
- if (aT == null) {
- throw new NullPointerException("Object is null");
+ 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();
}
}