git://wamblee.org
/
xmlrouter
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
equals and hashcode for robust identifiable.
[xmlrouter]
/
impl
/
src
/
main
/
java
/
org
/
wamblee
/
xmlrouter
/
impl
/
ConfigImpl.java
diff --git
a/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java
b/impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java
index 789f75c5178db2da3579c10cd14f1fd96c193932..0324e78f8353b1dd58598e49b66ecca786b6552d 100644
(file)
--- a/
impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java
+++ b/
impl/src/main/java/org/wamblee/xmlrouter/impl/ConfigImpl.java
@@
-15,14
+15,19
@@
*/
package org.wamblee.xmlrouter.impl;
*/
package org.wamblee.xmlrouter.impl;
-import java.util.Collection;
+import static org.wamblee.xmlrouter.impl.MessageUtil.*;
+
+import java.util.ArrayList;
import java.util.Collections;
import java.util.Collections;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
+import java.util.Iterator;
+import java.util.List;
import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.Config;
import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.Config;
+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.
/**
* Default implementation of the {@link Config} interface.
@@
-31,79
+36,71
@@
import org.wamblee.xmlrouter.config.Config;
*
* @param <T>
*/
*
* @param <T>
*/
-public abstract class ConfigImpl<T> implements Config<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
AtomicLong next
;
- private
Map<Id<T>,
T> registered;
+ private
Id<Config> id
;
+ private
List<
T> registered;
/**
* Constructs the object.
*/
/**
* Constructs the object.
*/
- public ConfigImpl() {
- next = new AtomicLong(1);
- registered = new LinkedHashMap<Id<T>, T>();
+ public ConfigImpl(Id<Config> aId) {
+ // TODO test for null.
+ id = aId;
+ registered = new ArrayList<T>();
}
}
- /*
- * (non-Javadoc)
- *
- * @see org.wamblee.xmlrouter.config.Config#add(T)
- */
@Override
@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));
+ public Id<Config> getId() {
return id;
}
return id;
}
- public abstract T wrap(Id<T> aId, T aT);
-
/*
* (non-Javadoc)
*
/*
* (non-Javadoc)
*
- * @see
- * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
- * .Id)
+ * @see org.wamblee.xmlrouter.config.Config#add(T)
*/
@Override
*/
@Override
- public
boolean remove(Id<T> aId
) {
- notNull(
aId
);
- re
turn registered.remove(aId) != null
;
+ public
synchronized void add(T aT
) {
+ notNull(
"aT", aT
);
+ re
gistered.add(wrap(id.getId() + ".", aT))
;
}
}
- /*
- *
(non-Javadoc)
+ /*
*
+ *
This is called to wrap the given object by a safer version.
*
*
- * @see org.wamblee.xmlrouter.config.Config#ids()
+ * @param aT
+ * Object to wrap.
+ * @return Wrapped object.
*/
*/
- @Override
- public Collection<Id<T>> ids() {
- return Collections.unmodifiableCollection(registered.keySet());
- }
+ public abstract T wrap(String aPrefix, T aT);
/*
* (non-Javadoc)
*
* @see
/*
* (non-Javadoc)
*
* @see
- * org.wamblee.xmlrouter.config.Config#get(org.wamblee.xmlrouter.common.Id)
+ * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
+ * .Id)
*/
@Override
*/
@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 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;
+ }
}
}
+ return false;
}
}
- private void notNull(Id<T> aId) {
- if (aId == null) {
- throw new NullPointerException("Id is null");
- }
+ @Override
+ public List<T> values() {
+ return Collections.unmodifiableList(registered);
}
}
}
}