refactoring of the config interface towards more reuse in the implementation and...
[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
new file mode 100644 (file)
index 0000000..789f75c
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.xmlrouter.impl;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Config;
+
+/**
+ * Default implementation of the {@link Config} interface.
+ * 
+ * @author Erik Brakkee
+ * 
+ * @param <T>
+ */
+public abstract class ConfigImpl<T> implements Config<T> {
+
+    private AtomicLong next;
+    private Map<Id<T>, T> registered;
+
+    /**
+     * Constructs the object.
+     */
+    public ConfigImpl() {
+        next = new AtomicLong(1);
+        registered = new LinkedHashMap<Id<T>, T>();
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @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 abstract T wrap(Id<T> aId, T aT);
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
+     * .Id)
+     */
+    @Override
+    public boolean remove(Id<T> aId) {
+        notNull(aId);
+        return registered.remove(aId) != null;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.wamblee.xmlrouter.config.Config#ids()
+     */
+    @Override
+    public Collection<Id<T>> ids() {
+        return Collections.unmodifiableCollection(registered.keySet());
+    }
+
+    /*
+     * (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");
+        }
+    }
+
+    private void notNull(Id<T> aId) {
+        if (aId == null) {
+            throw new NullPointerException("Id is null");
+        }
+    }
+}