First version after introduction of meaningful ids and Identifiable interface.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / RobustIdentifiable.java
diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustIdentifiable.java
new file mode 100644 (file)
index 0000000..f3da5f4
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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.logging.Level;
+import java.util.logging.Logger;
+
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Identifiable;
+
+/**
+ * Robust identifiable provides robustness for identifiable objects.
+ * 
+ * @author Erik Brakkee
+ * 
+ * @param <T>
+ */
+public class RobustIdentifiable<T> implements Identifiable<T> {
+    private static final Logger LOGGER = Logger
+        .getLogger(RobustIdentifiable.class.getName());
+
+    private Id<T> id;
+
+    // TODO test this class.
+    // TODO test that id is constant even though delegated changes its id.
+
+    public RobustIdentifiable(Identifiable<T> aIdentifiable) {
+        // TODO test id is null
+        // TODO getId() throws exception
+        try {
+            id = aIdentifiable.getId();
+            if (id == null) {
+                id = new Id<T>(Constants.UNKNOWN_ID.toString());
+                throw new RuntimeException(
+                    "Temporary to catch nulls during refactoring");
+            }
+        } catch (Exception e) {
+            LOGGER
+                .log(Level.WARNING, "Identifiable getId() threw exception", e);
+        }
+
+    }
+
+    @Override
+    public Id<T> getId() {
+        return id;
+    }
+
+    // TODO test equals, hashcode.
+
+    @Override
+    public int hashCode() {
+        return id.hashCode();
+    }
+
+    @Override
+    public boolean equals(Object aObj) {
+        if (aObj == null) {
+            return false;
+        }
+        if (!getClass().isInstance(aObj)) {
+            return false;
+        }
+        RobustIdentifiable<T> obj = (RobustIdentifiable<T>) aObj;
+        return id.equals(obj.getId());
+    }
+}