db-jdo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an...@apache.org
Subject svn commit: r672121 - in /db/jdo/trunk: api2-legacy/src/java/javax/jdo/identity/ api2-legacy/test/java/javax/jdo/identity/ api2/src/java/javax/jdo/identity/ api2/test/java/javax/jdo/identity/
Date Fri, 27 Jun 2008 06:04:28 GMT
Author: andyj
Date: Thu Jun 26 23:04:27 2008
New Revision: 672121

URL: http://svn.apache.org/viewvc?rev=672121&view=rev
Log:
JDO-598 Change javax.jdo.identity classes to implement Comparable. Simple tests for ByteIdentity,
CharIdentity, LongIdentity, IntIdentity, ShortIdentity, StringIdentity

Modified:
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ByteIdentity.java
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/CharIdentity.java
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/IntIdentity.java
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/LongIdentity.java
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ObjectIdentity.java
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ShortIdentity.java
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/SingleFieldIdentity.java
    db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/StringIdentity.java
    db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ByteIdentityTest.java
    db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/CharIdentityTest.java
    db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ConcreteTestIdentity.java
    db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/IntIdentityTest.java
    db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/LongIdentityTest.java
    db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ShortIdentityTest.java
    db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/StringIdentityTest.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/ByteIdentity.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/CharIdentity.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/IntIdentity.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/LongIdentity.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/ObjectIdentity.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/ShortIdentity.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/SingleFieldIdentity.java
    db/jdo/trunk/api2/src/java/javax/jdo/identity/StringIdentity.java
    db/jdo/trunk/api2/test/java/javax/jdo/identity/ByteIdentityTest.java
    db/jdo/trunk/api2/test/java/javax/jdo/identity/CharIdentityTest.java
    db/jdo/trunk/api2/test/java/javax/jdo/identity/ConcreteTestIdentity.java
    db/jdo/trunk/api2/test/java/javax/jdo/identity/IntIdentityTest.java
    db/jdo/trunk/api2/test/java/javax/jdo/identity/LongIdentityTest.java
    db/jdo/trunk/api2/test/java/javax/jdo/identity/ShortIdentityTest.java
    db/jdo/trunk/api2/test/java/javax/jdo/identity/StringIdentityTest.java

Modified: db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ByteIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ByteIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ByteIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ByteIdentity.java Thu Jun 26 23:04:27
2008
@@ -105,6 +105,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof ByteIdentity) {
+        	ByteIdentity other = (ByteIdentity)o;
+            return (int)(key - other.key);
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

Modified: db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/CharIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/CharIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/CharIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/CharIdentity.java Thu Jun 26 23:04:27
2008
@@ -113,6 +113,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof CharIdentity) {
+        	CharIdentity other = (CharIdentity)o;
+            return (int)(key - other.key);
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

Modified: db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/IntIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/IntIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/IntIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/IntIdentity.java Thu Jun 26 23:04:27
2008
@@ -102,6 +102,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof IntIdentity) {
+        	IntIdentity other = (IntIdentity)o;
+            return key - other.key;
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

Modified: db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/LongIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/LongIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/LongIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/LongIdentity.java Thu Jun 26 23:04:27
2008
@@ -103,6 +103,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof LongIdentity) {
+        	LongIdentity other = (LongIdentity)o;
+            return (int)(key - other.key);
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

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=672121&r1=672120&r2=672121&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 Thu Jun 26 23:04:27
2008
@@ -133,6 +133,28 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    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");
+        	}
+        }
+    	// Just disallow comparison. Could make some assumptions about being Date, Locale etc
+        throw new ClassCastException("ObjectIdentity cannot be used for comparator ordering");
+    }
+
     /** Write this object. Write the superclass first.
      * @param out the output
      */

Modified: db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ShortIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ShortIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ShortIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/ShortIdentity.java Thu Jun 26 23:04:27
2008
@@ -102,6 +102,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof ShortIdentity) {
+        	ShortIdentity other = (ShortIdentity)o;
+            return key - other.key;
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

Modified: db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/SingleFieldIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/SingleFieldIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/SingleFieldIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/SingleFieldIdentity.java Thu Jun
26 23:04:27 2008
@@ -40,7 +40,7 @@
  * @version 2.0
  */
 public abstract class SingleFieldIdentity
-    implements Externalizable {
+    implements Externalizable, Comparable {
     
     /** The Internationalization message helper.
      */

Modified: db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/StringIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/StringIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/StringIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/src/java/javax/jdo/identity/StringIdentity.java Thu Jun 26 23:04:27
2008
@@ -78,6 +78,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof StringIdentity) {
+            StringIdentity other = (StringIdentity)o;
+            return ((String)this.keyAsObject).compareTo((String)other.keyAsObject);
+        }
+        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.
      * @param out the output
      */

Modified: db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ByteIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ByteIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ByteIdentityTest.java (original)
+++ db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ByteIdentityTest.java Thu Jun 26
23:04:27 2008
@@ -127,4 +127,13 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	ByteIdentity c1 = new ByteIdentity(Object.class, (byte)1);
+        ByteIdentity c2 = new ByteIdentity(Object.class, (byte)1);
+        ByteIdentity c3 = new ByteIdentity(Object.class, (byte)2);
+        assertEquals("Equal ByteIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal ByteIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal ByteIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
+
 }

Modified: db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/CharIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/CharIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/CharIdentityTest.java (original)
+++ db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/CharIdentityTest.java Thu Jun 26
23:04:27 2008
@@ -136,4 +136,13 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	CharIdentity c1 = new CharIdentity(Object.class, '1');
+    	CharIdentity c2 = new CharIdentity(Object.class, '1');
+    	CharIdentity c3 = new CharIdentity(Object.class, '2');
+        assertEquals("Equal CharIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal CharIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal CharIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
+
 }

Modified: db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ConcreteTestIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ConcreteTestIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ConcreteTestIdentity.java (original)
+++ db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ConcreteTestIdentity.java Thu Jun
26 23:04:27 2008
@@ -22,7 +22,6 @@
 
 package javax.jdo.identity;
 
-import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
@@ -42,7 +41,16 @@
         public ConcreteTestIdentity() {
             super();
         }
-        
+
+        /** Determine the ordering of identity objects.
+         * @param o Other identity
+         * @return The relative ordering between the objects
+         * @since 2.2
+         */
+        public int compareTo(Object o) {
+            throw new ClassCastException("Not implemented");
+        }
+
         public void writeExternal(ObjectOutput out) throws IOException {
             super.writeExternal(out);
         } 

Modified: db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/IntIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/IntIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/IntIdentityTest.java (original)
+++ db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/IntIdentityTest.java Thu Jun 26
23:04:27 2008
@@ -126,4 +126,13 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	IntIdentity c1 = new IntIdentity(Object.class, 1);
+    	IntIdentity c2 = new IntIdentity(Object.class, 1);
+    	IntIdentity c3 = new IntIdentity(Object.class, 2);
+        assertEquals("Equal IntIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal IntIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal IntIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
+
 }

Modified: db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/LongIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/LongIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/LongIdentityTest.java (original)
+++ db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/LongIdentityTest.java Thu Jun 26
23:04:27 2008
@@ -127,4 +127,13 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	LongIdentity c1 = new LongIdentity(Object.class, 1);
+    	LongIdentity c2 = new LongIdentity(Object.class, 1);
+    	LongIdentity c3 = new LongIdentity(Object.class, 2);
+        assertEquals("Equal LongIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal LongIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal LongIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
+
 }

Modified: db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ShortIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ShortIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ShortIdentityTest.java (original)
+++ db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/ShortIdentityTest.java Thu Jun 26
23:04:27 2008
@@ -126,4 +126,13 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	ShortIdentity c1 = new ShortIdentity(Object.class, (short)1);
+    	ShortIdentity c2 = new ShortIdentity(Object.class, (short)1);
+    	ShortIdentity c3 = new ShortIdentity(Object.class, (short)2);
+        assertEquals("Equal IntIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal IntIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal IntIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
+
 }

Modified: db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/StringIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/StringIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/StringIdentityTest.java (original)
+++ db/jdo/trunk/api2-legacy/test/java/javax/jdo/identity/StringIdentityTest.java Thu Jun
26 23:04:27 2008
@@ -89,4 +89,13 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	StringIdentity c1 = new StringIdentity(Object.class, "1");
+    	StringIdentity c2 = new StringIdentity(Object.class, "1");
+    	StringIdentity c3 = new StringIdentity(Object.class, "2");
+        assertEquals("Equal StringIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal StringIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal StringIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
+
 }

Modified: db/jdo/trunk/api2/src/java/javax/jdo/identity/ByteIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/identity/ByteIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/identity/ByteIdentity.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/identity/ByteIdentity.java Thu Jun 26 23:04:27 2008
@@ -105,6 +105,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof ByteIdentity) {
+        	ByteIdentity other = (ByteIdentity)o;
+            return (int)(key - other.key);
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

Modified: db/jdo/trunk/api2/src/java/javax/jdo/identity/CharIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/identity/CharIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/identity/CharIdentity.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/identity/CharIdentity.java Thu Jun 26 23:04:27 2008
@@ -113,6 +113,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof CharIdentity) {
+        	CharIdentity other = (CharIdentity)o;
+            return (int)(key - other.key);
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

Modified: db/jdo/trunk/api2/src/java/javax/jdo/identity/IntIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/identity/IntIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/identity/IntIdentity.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/identity/IntIdentity.java Thu Jun 26 23:04:27 2008
@@ -102,6 +102,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof IntIdentity) {
+        	IntIdentity other = (IntIdentity)o;
+            return key - other.key;
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

Modified: db/jdo/trunk/api2/src/java/javax/jdo/identity/LongIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/identity/LongIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/identity/LongIdentity.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/identity/LongIdentity.java Thu Jun 26 23:04:27 2008
@@ -103,6 +103,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof LongIdentity) {
+        	LongIdentity other = (LongIdentity)o;
+            return (int)(key - other.key);
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

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=672121&r1=672120&r2=672121&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 Thu Jun 26 23:04:27
2008
@@ -133,6 +133,28 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    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");
+        	}
+        }
+    	// Just disallow comparison. Could make some assumptions about being Date, Locale etc
+        throw new ClassCastException("ObjectIdentity cannot be used for comparator ordering");
+    }
+
     /** Write this object. Write the superclass first.
      * @param out the output
      */

Modified: db/jdo/trunk/api2/src/java/javax/jdo/identity/ShortIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/identity/ShortIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/identity/ShortIdentity.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/identity/ShortIdentity.java Thu Jun 26 23:04:27 2008
@@ -102,6 +102,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof ShortIdentity) {
+        	ShortIdentity other = (ShortIdentity)o;
+            return key - other.key;
+        }
+        else if (o == null) {
+            throw new ClassCastException("object is null");
+        }
+        throw new ClassCastException(this.getClass().getName() + " != " + o.getClass().getName());
+    }
+
     /** Create the key as an Object.
      * @return the key as an Object
      * @since 2.0

Modified: db/jdo/trunk/api2/src/java/javax/jdo/identity/SingleFieldIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/identity/SingleFieldIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/identity/SingleFieldIdentity.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/identity/SingleFieldIdentity.java Thu Jun 26 23:04:27
2008
@@ -40,7 +40,7 @@
  * @version 2.0
  */
 public abstract class SingleFieldIdentity
-    implements Externalizable {
+    implements Externalizable, Comparable {
     
     /** The Internationalization message helper.
      */

Modified: db/jdo/trunk/api2/src/java/javax/jdo/identity/StringIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/src/java/javax/jdo/identity/StringIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/src/java/javax/jdo/identity/StringIdentity.java (original)
+++ db/jdo/trunk/api2/src/java/javax/jdo/identity/StringIdentity.java Thu Jun 26 23:04:27
2008
@@ -78,6 +78,22 @@
         }
     }
 
+    /** Determine the ordering of identity objects.
+     * @param o Other identity
+     * @return The relative ordering between the objects
+     * @since 2.2
+     */
+    public int compareTo(Object o) {
+        if (o instanceof StringIdentity) {
+            StringIdentity other = (StringIdentity)o;
+            return ((String)this.keyAsObject).compareTo((String)other.keyAsObject);
+        }
+        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.
      * @param out the output
      */

Modified: db/jdo/trunk/api2/test/java/javax/jdo/identity/ByteIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/identity/ByteIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/identity/ByteIdentityTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/identity/ByteIdentityTest.java Thu Jun 26 23:04:27
2008
@@ -127,4 +127,12 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	ByteIdentity c1 = new ByteIdentity(Object.class, (byte)1);
+        ByteIdentity c2 = new ByteIdentity(Object.class, (byte)1);
+        ByteIdentity c3 = new ByteIdentity(Object.class, (byte)2);
+        assertEquals("Equal ByteIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal ByteIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal ByteIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
 }

Modified: db/jdo/trunk/api2/test/java/javax/jdo/identity/CharIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/identity/CharIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/identity/CharIdentityTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/identity/CharIdentityTest.java Thu Jun 26 23:04:27
2008
@@ -136,4 +136,12 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	CharIdentity c1 = new CharIdentity(Object.class, '1');
+    	CharIdentity c2 = new CharIdentity(Object.class, '1');
+    	CharIdentity c3 = new CharIdentity(Object.class, '2');
+        assertEquals("Equal CharIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal CharIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal CharIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
 }

Modified: db/jdo/trunk/api2/test/java/javax/jdo/identity/ConcreteTestIdentity.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/identity/ConcreteTestIdentity.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/identity/ConcreteTestIdentity.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/identity/ConcreteTestIdentity.java Thu Jun 26 23:04:27
2008
@@ -22,7 +22,6 @@
 
 package javax.jdo.identity;
 
-import java.io.Externalizable;
 import java.io.IOException;
 import java.io.ObjectInput;
 import java.io.ObjectOutput;
@@ -42,7 +41,16 @@
         public ConcreteTestIdentity() {
             super();
         }
-        
+
+        /** Determine the ordering of identity objects.
+         * @param o Other identity
+         * @return The relative ordering between the objects
+         * @since 2.2
+         */
+        public int compareTo(Object o) {
+            throw new ClassCastException("Not implemented");
+        }
+
         public void writeExternal(ObjectOutput out) throws IOException {
             super.writeExternal(out);
         } 

Modified: db/jdo/trunk/api2/test/java/javax/jdo/identity/IntIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/identity/IntIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/identity/IntIdentityTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/identity/IntIdentityTest.java Thu Jun 26 23:04:27
2008
@@ -126,4 +126,12 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	IntIdentity c1 = new IntIdentity(Object.class, 1);
+    	IntIdentity c2 = new IntIdentity(Object.class, 1);
+    	IntIdentity c3 = new IntIdentity(Object.class, 2);
+        assertEquals("Equal IntIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal IntIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal IntIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
 }

Modified: db/jdo/trunk/api2/test/java/javax/jdo/identity/LongIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/identity/LongIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/identity/LongIdentityTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/identity/LongIdentityTest.java Thu Jun 26 23:04:27
2008
@@ -127,4 +127,12 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	LongIdentity c1 = new LongIdentity(Object.class, 1);
+    	LongIdentity c2 = new LongIdentity(Object.class, 1);
+    	LongIdentity c3 = new LongIdentity(Object.class, 2);
+        assertEquals("Equal LongIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal LongIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal LongIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
 }

Modified: db/jdo/trunk/api2/test/java/javax/jdo/identity/ShortIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/identity/ShortIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/identity/ShortIdentityTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/identity/ShortIdentityTest.java Thu Jun 26 23:04:27
2008
@@ -126,4 +126,12 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	ShortIdentity c1 = new ShortIdentity(Object.class, (short)1);
+    	ShortIdentity c2 = new ShortIdentity(Object.class, (short)1);
+    	ShortIdentity c3 = new ShortIdentity(Object.class, (short)2);
+        assertEquals("Equal IntIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal IntIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal IntIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
 }

Modified: db/jdo/trunk/api2/test/java/javax/jdo/identity/StringIdentityTest.java
URL: http://svn.apache.org/viewvc/db/jdo/trunk/api2/test/java/javax/jdo/identity/StringIdentityTest.java?rev=672121&r1=672120&r2=672121&view=diff
==============================================================================
--- db/jdo/trunk/api2/test/java/javax/jdo/identity/StringIdentityTest.java (original)
+++ db/jdo/trunk/api2/test/java/javax/jdo/identity/StringIdentityTest.java Thu Jun 26 23:04:27
2008
@@ -89,4 +89,12 @@
         fail ("Failed to catch expected exception.");
     }
 
+    public void testCompareTo() {
+    	StringIdentity c1 = new StringIdentity(Object.class, "1");
+    	StringIdentity c2 = new StringIdentity(Object.class, "1");
+    	StringIdentity c3 = new StringIdentity(Object.class, "2");
+        assertEquals("Equal StringIdentity instances compare not equal.", 0, c1.compareTo(c2));
+        assertTrue("Not equal StringIdentity instances have wrong compareTo result", c1.compareTo(c3)
< 0);
+        assertTrue("Not equal StringIdentity instances have wrong compareTo result", c3.compareTo(c1)
> 0); 
+    }
 }



Mime
View raw message