From jdo-commits-return-2381-apmail-db-jdo-commits-archive=www.apache.org@db.apache.org Fri Jun 27 06:04:50 2008 Return-Path: Delivered-To: apmail-db-jdo-commits-archive@www.apache.org Received: (qmail 3026 invoked from network); 27 Jun 2008 06:04:50 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 27 Jun 2008 06:04:50 -0000 Received: (qmail 98565 invoked by uid 500); 27 Jun 2008 06:04:52 -0000 Mailing-List: contact jdo-commits-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jdo-dev@db.apache.org Delivered-To: mailing list jdo-commits@db.apache.org Received: (qmail 98556 invoked by uid 99); 27 Jun 2008 06:04:52 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 26 Jun 2008 23:04:52 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 27 Jun 2008 06:04:10 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id D0EBB238896B; Thu, 26 Jun 2008 23:04:29 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: jdo-commits@db.apache.org From: andyj@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20080627060429.D0EBB238896B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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); + } }