Author: ryman
Date: Tue Nov 14 10:34:17 2006
New Revision: 474912

URL: http://svn.apache.org/viewvc?view=rev&rev=474912
Log:
[WODEN-82] Created ObjectIdTable class and junit. Updated build scripts to run 
junit.

Added:
    incubator/woden/trunk/java/src/org/apache/woden/ant/ObjectIdTable.java
    incubator/woden/trunk/java/test/org/apache/woden/ant/
    incubator/woden/trunk/java/test/org/apache/woden/ant/ObjectIdTableTest.java

Added: incubator/woden/trunk/java/src/org/apache/woden/ant/ObjectIdTable.java
URL: 
http://svn.apache.org/viewvc/incubator/woden/trunk/java/src/org/apache/woden/ant/ObjectIdTable.java?view=auto&rev=474912
==============================================================================
--- incubator/woden/trunk/java/src/org/apache/woden/ant/ObjectIdTable.java 
(added)
+++ incubator/woden/trunk/java/src/org/apache/woden/ant/ObjectIdTable.java Tue 
Nov 14 10:34:17 2006
@@ -0,0 +1,60 @@
+/**
+ * Copyright 2006 Apache Software Foundation 
+ *
+ * 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.apache.woden.ant;
+
+import java.util.HashMap;
+
+/**
+ * Provides a completely safe way to generate unique ids for equivalence 
classes of objects.
+ * Equivalent objects are assigned the same ids.
+ * Inequivalent objects are assigned different ids.
+ * Here equivalance is defined by the equals() method which is used by HashMap.
+ * Experience has shown that hashCode() occasionally produces the same hash 
for different objects.
+ * Note that the objects are permanently stored, so only use this class when 
serializing
+ * objects in an XML file.
+ * 
+ * A count is kept and assigned to each object equivalence class as it is 
added.
+ * 
+ * @author Arthur Ryman ([EMAIL PROTECTED], [EMAIL PROTECTED])
+ *
+ */
+
+public class ObjectIdTable {
+
+    private HashMap components = new HashMap();
+
+    private int nextId = 1;
+
+    /**
+     * Returns a unique integer for the equivalence class of the object.
+     * 
+     * @param o An object.
+     * @return A unique id for the object.
+     */
+    public int id(Object o) {
+
+        Integer id = (Integer) components.get(o);
+        if (id == null) {
+
+            id = new Integer(nextId++);
+            components.put(o, id);
+        }
+
+        return id.intValue();
+    }
+
+}

Added: 
incubator/woden/trunk/java/test/org/apache/woden/ant/ObjectIdTableTest.java
URL: 
http://svn.apache.org/viewvc/incubator/woden/trunk/java/test/org/apache/woden/ant/ObjectIdTableTest.java?view=auto&rev=474912
==============================================================================
--- incubator/woden/trunk/java/test/org/apache/woden/ant/ObjectIdTableTest.java 
(added)
+++ incubator/woden/trunk/java/test/org/apache/woden/ant/ObjectIdTableTest.java 
Tue Nov 14 10:34:17 2006
@@ -0,0 +1,65 @@
+/**
+ * Copyright 2006 Apache Software Foundation 
+ *
+ * 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.apache.woden.ant;
+
+import org.apache.woden.ant.ObjectIdTable;
+import org.apache.woden.types.NCNameTest;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Tests ObjectIdTable.
+ * 
+ * @author Arthur Ryman ([EMAIL PROTECTED], [EMAIL PROTECTED])
+ *
+ */
+public class ObjectIdTableTest extends TestCase {
+
+    /**
+     * Creates a test suite for this class.
+     * 
+     * @return the test suite
+     */
+    public static Test suite()
+    {
+        return new TestSuite(ObjectIdTableTest.class);
+    }
+    
+    /**
+     * Tests the id() method.
+     * Same objects have the same id.
+     * Different objects have different ids.
+     */
+    public void testId() {
+        
+        String s1 = "s1";
+        String s2 = "s2";
+        
+        ObjectIdTable oit = new ObjectIdTable();
+        
+        int id1 = oit.id(s1);
+        assertTrue("Same object, same id", id1 == oit.id(s1));
+        
+        int id2 = oit.id(s2);
+        assertTrue("Different object, different id", id1 != id2);
+        
+        assertTrue("Same object, same id, again", id2 == oit.id(s2));
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to