You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jdo-commits@db.apache.org by cl...@apache.org on 2008/06/29 21:17:52 UTC

svn commit: r672662 - in /db/jdo/trunk: api2-legacy/src/java/javax/jdo/identity/ObjectIdentity.java api2/src/java/javax/jdo/identity/ObjectIdentity.java api2/test/java/javax/jdo/identity/ObjectIdentityTest.java

Author: clr
Date: Sun Jun 29 12:17:52 2008
New Revision: 672662

URL: http://svn.apache.org/viewvc?rev=672662&view=rev
Log:
JDO-598 Updated ObjectIdentity to allow Comparable classes

Modified:
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ObjectIdentity.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/ObjectIdentity.java
    db/jdo/trunk/api2/test/java/javax/jdo/identity/ObjectIdentityTest.java

Modified: db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ObjectIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ObjectIdentity.java?rev=672662&r1=672661&r2=672662&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ObjectIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ObjectIdentity.java Sun Jun 29 12:17:52 2008
@@ -141,18 +141,28 @@
     public int compareTo(Object o) {
         if (o instanceof ObjectIdentity) {
         	ObjectIdentity other = (ObjectIdentity)o;
-        	if (other.keyAsObject instanceof Comparable && keyAsObject instanceof Comparable) {
-        		return ((Comparable)keyAsObject).compareTo((Comparable)other.keyAsObject);
-        	}
-        	else
-        	{
-        		throw new ClassCastException("The key class (" + 
-        				keyAsObject.getClass().getName() + 
-        				") does not implement Comparable");
-        	}
+            int result = super.compare(other);
+            if (result == 0) {
+                if (other.keyAsObject instanceof Comparable && 
+                        keyAsObject instanceof Comparable) {
+                    return ((Comparable)keyAsObject).compareTo(
+                            (Comparable)other.keyAsObject);
+                }
+                else
+                {
+                    throw new ClassCastException("The key class (" + 
+                            keyAsObject.getClass().getName() + 
+                            ") does not implement Comparable");
+                }
+            } else {
+                return result;
+            }
         }
-    	// Just disallow comparison. Could make some assumptions about being Date, Locale etc
-        throw new ClassCastException("ObjectIdentity cannot be used for comparator ordering");
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + 
+                " != " + o.getClass().getName());
     }
 
     /** Write this object. Write the superclass first.

Modified: db/jdo/trunk/api2/src/java/javax/jdo/identity/ObjectIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/identity/ObjectIdentity.java?rev=672662&r1=672661&r2=672662&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/identity/ObjectIdentity.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/identity/ObjectIdentity.java Sun Jun 29 12:17:52 2008
@@ -141,18 +141,28 @@
     public int compareTo(Object o) {
         if (o instanceof ObjectIdentity) {
         	ObjectIdentity other = (ObjectIdentity)o;
-        	if (other.keyAsObject instanceof Comparable && keyAsObject instanceof Comparable) {
-        		return ((Comparable)keyAsObject).compareTo((Comparable)other.keyAsObject);
-        	}
-        	else
-        	{
-        		throw new ClassCastException("The key class (" + 
-        				keyAsObject.getClass().getName() + 
-        				") does not implement Comparable");
-        	}
+            int result = super.compare(other);
+            if (result == 0) {
+                if (other.keyAsObject instanceof Comparable && 
+                        keyAsObject instanceof Comparable) {
+                    return ((Comparable)keyAsObject).compareTo(
+                            (Comparable)other.keyAsObject);
+                }
+                else
+                {
+                    throw new ClassCastException("The key class (" + 
+                            keyAsObject.getClass().getName() + 
+                            ") does not implement Comparable");
+                }
+            } else {
+                return result;
+            }
         }
-    	// Just disallow comparison. Could make some assumptions about being Date, Locale etc
-        throw new ClassCastException("ObjectIdentity cannot be used for comparator ordering");
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + 
+                " != " + o.getClass().getName());
     }
 
     /** Write this object. Write the superclass first.

Modified: db/jdo/trunk/api2/test/java/javax/jdo/identity/ObjectIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/identity/ObjectIdentityTest.java?rev=672662&r1=672661&r2=672662&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/identity/ObjectIdentityTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/identity/ObjectIdentityTest.java Sun Jun 29 12:17:52 2008
@@ -142,6 +142,17 @@
         assertEquals ("Equal ObjectIdentity instances compare not equal.", c1, c2);
     }
 
+    public void testDateCompareTo() {
+    	ObjectIdentity c1 = new ObjectIdentity(Object.class, new Date(1));
+    	ObjectIdentity c2 = new ObjectIdentity(Object.class, new Date(1));
+    	ObjectIdentity c3 = new ObjectIdentity(Object.class, new Date(2));
+        ObjectIdentity c4 = new ObjectIdentity(Class.class, new Date(1));
+        assertEquals("Equal ObjectIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal ObjectIdentity instances have wrong compareTo result", c1.compareTo(c3) < 0);
+        assertTrue("Not equal ObjectIdentity instances have wrong compareTo result", c3.compareTo(c1) > 0); 
+        assertTrue("Not equal ObjectIdentity instances have wrong compareTo result", c1.compareTo(c4) > 0);
+    }
+
     public void testBadStringConstructorNullClass() {
         try {
             ObjectIdentity c1 = new ObjectIdentity(null, "1");