Index: /luni/src/main/java/java/lang/Byte.java =================================================================== --- /luni/src/main/java/java/lang/Byte.java (revision 393999) +++ /luni/src/main/java/java/lang/Byte.java (working copy) @@ -17,10 +17,9 @@ /** *
Byte is the wrapper for the primitive type byte.
+ * Compares this Byte to the Byte passed. If
+ * this instance's value is equal to the value of the instance passed, then
+ * 0 is returned. If this instance's value is less than the value of the
+ * instance passed, then a negative value is returned. If this instance's
+ * value is greater than the value of the instance passed, then a positive
+ * value is returned.
+ *
object is
+ * null.
+ * @since 1.2
+ */
+ public int compareTo(Byte object) {
+ return value > object.value ? 1 : (value < object.value ? -1 : 0);
+ }
/**
* Parses the string argument as if it was a byte value and returns the
Index: /luni/src/main/java/java/lang/Character.java
===================================================================
--- /luni/src/main/java/java/lang/Character.java (revision 393999)
+++ /luni/src/main/java/java/lang/Character.java (working copy)
@@ -52,8 +52,7 @@
*
* @since 1.0
*/
-public final class Character implements Serializable, Comparable {
- //TODO Add Comparablec is null.
+ * @since 1.2
*/
public int compareTo(Character c) {
return value - c.value;
@@ -1118,23 +1119,6 @@
public int compareTo(Character c) {
return value - c.value;
}
-
- /**
- * Compare the receiver to the specified Object to determine the relative
- * ordering.
- *
- * @param object
- * an Object
- * @return an int < 0 if this Character is less than the specified
- * Character, 0 if they are equal, and > 0 if this Character is
- * greater
- *
- * @throws ClassCastException
- * when object is not a Character
- */
- public int compareTo(Object object) {
- return value - ((Character) object).value;
- }
/*
* Provides a cache for the 'valueOf' method. A size of 512 should cache the
Index: /luni/src/main/java/java/lang/Double.java
===================================================================
--- /luni/src/main/java/java/lang/Double.java (revision 393999)
+++ /luni/src/main/java/java/lang/Double.java (working copy)
@@ -23,7 +23,7 @@
* @see java.lang.Number
* @since 1.0
*/
-public final class Double extends Number implements Comparable {
+public final class Double extends Number implements Comparableobject is null.
+ * @since 1.2
*/
public int compareTo(Double object) {
long d1, d2;
@@ -147,26 +150,6 @@
}
/**
- * Compares the receiver with a Double parameter.
- *
- * @param object
- * the Double to compare to the receiver
- *
- * @return Returns greater than zero when this.doubleValue() is greater than
- * object.doubleValue(), zero when this.doubleValue() equals
- * object.doubleValue(), and less than zero when this.doubleValue()
- * is less than object.doubleValue()
- *
- * @throws ClassCastException
- * when object is not a Double
- *
- * @see #compareTo(Double)
- */
- public int compareTo(Object object) {
- return compareTo((Double) object);
- }
-
- /**
* Answers the byte value which the receiver represents
*
* @return byte the value of the receiver.
Index: /luni/src/main/java/java/lang/Float.java
===================================================================
--- /luni/src/main/java/java/lang/Float.java (revision 393999)
+++ /luni/src/main/java/java/lang/Float.java (working copy)
@@ -24,8 +24,8 @@
* @see java.lang.Number
* @since 1.0
*/
-public final class Float extends Number implements Comparable {
- //TODO Add Comparableobject is null.
* @since 1.2
*/
public int compareTo(Float object) {
@@ -157,26 +158,6 @@
}
/**
- * Compares the receiver with a Float parameter.
- *
- * @param object
- * the Float to compare to the receiver
- *
- * @return Returns greater than zero when this.floatValue() is greater than
- * object.floatValue(), zero when this.floatValue() equals
- * object.floatValue(), and less than zero when this.floatValue() is
- * less than object.floatValue()
- *
- * @throws ClassCastException
- * when object is not a Float
- *
- * @see #compareTo(Float)
- */
- public int compareTo(Object object) {
- return compareTo((Float) object);
- }
-
- /**
* Answers the byte value which the receiver represents
*
* @return byte the value of the receiver.
Index: /luni/src/main/java/java/lang/Integer.java
===================================================================
--- /luni/src/main/java/java/lang/Integer.java (revision 393999)
+++ /luni/src/main/java/java/lang/Integer.java (working copy)
@@ -30,8 +30,8 @@
* @see java.lang.Number
* @since 1.1
*/
-public final class Integer extends Number implements Comparable {
- //TODO Add Comparable
+ * Compares this Integer to the Integer
+ * passed. If this instance's value is equal to the value of the instance
+ * passed, then 0 is returned. If this instance's value is less than the
+ * value of the instance passed, then a negative value is returned. If this
+ * instance's value is greater than the value of the instance passed, then a
+ * positive value is returned.
+ *
object is
+ * null.
+ * @since 1.2
+ */
public int compareTo(Integer object) {
return value > object.value ? 1 : (value < object.value ? -1 : 0);
}
@@ -114,10 +129,6 @@
return value > object.value ? 1 : (value < object.value ? -1 : 0);
}
- public int compareTo(Object object) {
- return compareTo((Integer) object);
- }
-
/**
* Parses the string argument as if it was an int value and returns the
* result. Throws NumberFormatException if the string does not represent an
Index: /luni/src/main/java/java/lang/Long.java
===================================================================
--- /luni/src/main/java/java/lang/Long.java (revision 393999)
+++ /luni/src/main/java/java/lang/Long.java (working copy)
@@ -30,7 +30,7 @@
* @see java.lang.Number
* @since 1.0
*/
-public final class Long extends Number implements Comparable {
+public final class Long extends Number implements Comparable
+ * Compares this Long to the Long
+ * passed. If this instance's value is equal to the value of the instance
+ * passed, then 0 is returned. If this instance's value is less than the
+ * value of the instance passed, then a negative value is returned. If this
+ * instance's value is greater than the value of the instance passed, then a
+ * positive value is returned.
+ *
object is
+ * null.
+ * @since 1.2
+ */
public int compareTo(Long object) {
return value > object.value ? 1 : (value < object.value ? -1 : 0);
}
@@ -114,10 +129,6 @@
return value > object.value ? 1 : (value < object.value ? -1 : 0);
}
- public int compareTo(Object object) {
- return compareTo((Long) object);
- }
-
/**
* Parses the string argument as if it was a long value and returns the
* result. Throws NumberFormatException if the string does not represent a
Index: /luni/src/main/java/java/lang/Short.java
===================================================================
--- /luni/src/main/java/java/lang/Short.java (revision 393999)
+++ /luni/src/main/java/java/lang/Short.java (working copy)
@@ -23,8 +23,8 @@
* @see java.lang.Number
* @since 1.1
*/
-public final class Short extends Number implements Comparable {
- //TODO Add Comparable
+ * Compares this Short to the Short
+ * passed. If this instance's value is equal to the value of the instance
+ * passed, then 0 is returned. If this instance's value is less than the
+ * value of the instance passed, then a negative value is returned. If this
+ * instance's value is greater than the value of the instance passed, then a
+ * positive value is returned.
+ *
object is
+ * null.
+ * @since 1.2
+ */
public int compareTo(Short object) {
return value > object.value ? 1 : (value < object.value ? -1 : 0);
}
Index: /luni/src/test/java/org/apache/harmony/tests/java/lang/ByteTest.java
===================================================================
--- /luni/src/test/java/org/apache/harmony/tests/java/lang/ByteTest.java (revision 392827)
+++ /luni/src/test/java/org/apache/harmony/tests/java/lang/ByteTest.java (working copy)
@@ -325,4 +325,32 @@
assertEquals(0, new Byte((byte)0).shortValue());
assertEquals(1, new Byte((byte)1).shortValue());
}
+
+ /**
+ * @tests java.lang.Byte#compareTo(Byte)
+ */
+ public void test_compareToLjava_lang_Byte() {
+ final Byte min = new Byte(Byte.MIN_VALUE);
+ final Byte zero = new Byte((byte)0);
+ final Byte max = new Byte(Byte.MAX_VALUE);
+
+ assertTrue(max.compareTo(max) == 0);
+ assertTrue(min.compareTo(min) == 0);
+ assertTrue(zero.compareTo(zero) == 0);
+
+ assertTrue(max.compareTo(zero) > 0);
+ assertTrue(max.compareTo(min) > 0);
+
+ assertTrue(zero.compareTo(max) < 0);
+ assertTrue(zero.compareTo(min) > 0);
+
+ assertTrue(min.compareTo(zero) < 0);
+ assertTrue(min.compareTo(max) < 0);
+
+ try {
+ min.compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
+ }
}
Index: /luni/src/test/java/org/apache/harmony/tests/java/lang/CharacterTest.java
===================================================================
--- /luni/src/test/java/org/apache/harmony/tests/java/lang/CharacterTest.java (revision 392827)
+++ /luni/src/test/java/org/apache/harmony/tests/java/lang/CharacterTest.java (working copy)
@@ -573,4 +573,32 @@
} catch (IndexOutOfBoundsException e) {
}
}
+
+ /**
+ * @tests java.lang.Character#compareTo(Character)
+ */
+ public void test_compareToLjava_lang_Byte() {
+ final Character min = new Character(Character.MIN_VALUE);
+ final Character mid = new Character((char)(Character.MAX_VALUE/2));
+ final Character max = new Character(Character.MAX_VALUE);
+
+ assertTrue(max.compareTo(max) == 0);
+ assertTrue(min.compareTo(min) == 0);
+ assertTrue(mid.compareTo(mid) == 0);
+
+ assertTrue(max.compareTo(mid) > 0);
+ assertTrue(max.compareTo(min) > 0);
+
+ assertTrue(mid.compareTo(max) < 0);
+ assertTrue(mid.compareTo(min) > 0);
+
+ assertTrue(min.compareTo(mid) < 0);
+ assertTrue(min.compareTo(max) < 0);
+
+ try {
+ min.compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
+ }
}
Index: /luni/src/test/java/org/apache/harmony/tests/java/lang/DoubleTest.java
===================================================================
--- /luni/src/test/java/org/apache/harmony/tests/java/lang/DoubleTest.java (revision 392827)
+++ /luni/src/test/java/org/apache/harmony/tests/java/lang/DoubleTest.java (working copy)
@@ -54,6 +54,12 @@
D2.compareTo(objDouble) == 1);
}
}
+
+ try {
+ new Double(0.0D).compareTo(null);
+ fail("No NPE");
+ } catch(NullPointerException e) {
+ }
}
/**
Index: /luni/src/test/java/tests/api/java/lang/ByteTest.java
===================================================================
--- /luni/src/test/java/tests/api/java/lang/ByteTest.java (revision 392827)
+++ /luni/src/test/java/tests/api/java/lang/ByteTest.java (working copy)
@@ -63,26 +63,6 @@
}
/**
- * @tests java.lang.Byte#compareTo(java.lang.Object)
- */
- public void test_compareToLjava_lang_Object() {
- // Test for method int java.lang.Byte.compareTo(java.lang.Object)
- assertTrue("Comparison failed", new Byte((byte) 1)
- .compareTo((Object) new Byte((byte) 2)) < 0);
- assertTrue("Comparison failed", new Byte((byte) 1)
- .compareTo((Object) new Byte((byte) -2)) > 0);
- assertTrue("Comparison failed", new Byte((byte) 1)
- .compareTo((Object) new Byte((byte) 1)) == 0);
- try {
- new Byte((byte) 1).compareTo(new Object());
- } catch (ClassCastException e) {
- // correct
- return;
- }
- fail("Failed to throw ClassCastException");
- }
-
- /**
* @tests java.lang.Byte#decode(java.lang.String)
*/
public void test_decodeLjava_lang_String() {
Index: /luni/src/test/java/tests/api/java/lang/CharacterTest.java
===================================================================
--- /luni/src/test/java/tests/api/java/lang/CharacterTest.java (revision 392827)
+++ /luni/src/test/java/tests/api/java/lang/CharacterTest.java (working copy)
@@ -55,24 +55,6 @@
}
/**
- * @tests java.lang.Character#compareTo(java.lang.Object)
- */
- public void test_compareToLjava_lang_Object() {
- // Test for method int java.lang.Character.compareTo(java.lang.Object)
- Character c = new Character('c');
- assertTrue("Returned false for same Character",
- c.compareTo((Object) c) == 0);
- try {
- c.compareTo(new Object());
- } catch (ClassCastException e) {
- // Correct
- return;
- }
- fail(
- "Failed to throw exception when comparing to non-Character");
- }
-
- /**
* @tests java.lang.Character#digit(char, int)
*/
public void test_digitCI() {
Index: /luni/src/test/java/tests/api/java/lang/DoubleTest.java
===================================================================
--- /luni/src/test/java/tests/api/java/lang/DoubleTest.java (revision 392827)
+++ /luni/src/test/java/tests/api/java/lang/DoubleTest.java (working copy)
@@ -343,6 +343,12 @@
.compareTo(D1) == 1);
}
}
+
+ try {
+ new Double(0.0D).compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
}
/**
Index: /luni/src/test/java/tests/api/java/lang/FloatTest.java
===================================================================
--- /luni/src/test/java/tests/api/java/lang/FloatTest.java (revision 392827)
+++ /luni/src/test/java/tests/api/java/lang/FloatTest.java (working copy)
@@ -182,6 +182,12 @@
.compareTo(F1) == 1);
}
}
+
+ try {
+ new Float(0.0F).compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
}
/**
Index: /luni/src/test/java/tests/api/java/lang/IntegerTest.java
===================================================================
--- /luni/src/test/java/tests/api/java/lang/IntegerTest.java (revision 392827)
+++ /luni/src/test/java/tests/api/java/lang/IntegerTest.java (working copy)
@@ -61,27 +61,12 @@
.compareTo(new Integer(-2)) == 0);
assertTrue("3 compared to 2 gave non-positive answer", new Integer(3)
.compareTo(new Integer(2)) > 0);
- }
-
- /**
- * @tests java.lang.Integer#compareTo(java.lang.Object)
- */
- public void test_compareToLjava_lang_Object() {
- // Test for method int java.lang.Integer.compareTo(java.lang.Object)
- assertTrue("-2 compared to 1 gave non-negative answer", new Integer(-2)
- .compareTo((Object) new Integer(1)) < 0);
- assertTrue("-2 compared to -2 gave non-zero answer", new Integer(-2)
- .compareTo((Object) new Integer(-2)) == 0);
- assertTrue("3 compared to 2 gave non-positive answer", new Integer(3)
- .compareTo((Object) new Integer(2)) > 0);
- try {
- new Integer(3).compareTo(new Object());
- } catch (ClassCastException e) {
- // correct
- return;
- }
- fail(
- "3 compared to a non-integer object failed to throw an exception");
+
+ try {
+ new Integer(0).compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
}
/**
Index: /luni/src/test/java/tests/api/java/lang/LongTest.java
===================================================================
--- /luni/src/test/java/tests/api/java/lang/LongTest.java (revision 392827)
+++ /luni/src/test/java/tests/api/java/lang/LongTest.java (working copy)
@@ -62,27 +62,12 @@
.compareTo(new Long(-2L)) == 0);
assertTrue("3 compared to 2 gave non-positive answer", new Long(3L)
.compareTo(new Long(2L)) > 0);
- }
-
- /**
- * @tests java.lang.Long#compareTo(java.lang.Object)
- */
- public void test_compareToLjava_lang_Object() {
- // Test for method int java.lang.Long.compareTo(java.lang.Object)
- assertTrue("-2 compared to 1 gave non-negative answer", new Long(-2L)
- .compareTo((Object) new Long(1L)) < 0);
- assertTrue("-2 compared to -2 gave non-zero answer", new Long(-2L)
- .compareTo((Object) new Long(-2L)) == 0);
- assertTrue("3 compared to 2 gave non-positive answer", new Long(3L)
- .compareTo((Object) new Long(2L)) > 0);
- try {
- new Long(3).compareTo(new Object());
- } catch (ClassCastException e) {
- // correct
- return;
- }
- fail(
- "3 compared to a non-integer object failed to throw an exception");
+
+ try {
+ new Long(0).compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
}
/**
Index: /luni/src/test/java/tests/api/java/lang/ShortTest.java
===================================================================
--- /luni/src/test/java/tests/api/java/lang/ShortTest.java (revision 392827)
+++ /luni/src/test/java/tests/api/java/lang/ShortTest.java (working copy)
@@ -55,34 +55,6 @@
}
/**
- * @tests java.lang.Short#compareTo(java.lang.Object)
- */
- public void test_compareToLjava_lang_Object() {
- // Test for method int java.lang.Short.compareTo(java.lang.Object)
- Short s = new Short((short) 1);
- Object x = new Short((short) 3);
- assertTrue(
- "Should have returned negative value when compared to greater short",
- s.compareTo(x) < 0);
- x = new Short((short) -1);
- assertTrue(
- "Should have returned positive value when compared to lesser short",
- s.compareTo(x) > 0);
- x = new Short((short) 1);
- assertTrue("Should have returned zero when compared to equal short", s
- .compareTo(x) == 0);
- x = "5";
- try {
- s.compareTo(x);
- } catch (ClassCastException e) {
- // correct
- return;
- }
- fail(
- "Should have thrown ClassCastException when compared to non-short");
- }
-
- /**
* @tests java.lang.Short#compareTo(java.lang.Short)
*/
public void test_compareToLjava_lang_Short() {
@@ -99,6 +71,12 @@
x = new Short((short) 1);
assertTrue("Should have returned zero when compared to equal short", s
.compareTo(x) == 0);
+
+ try {
+ new Short((short)0).compareTo(null);
+ fail("No NPE");
+ } catch (NullPointerException e) {
+ }
}
/**
Index: /luni/src/test/java/tests/api/java/util/CollectionsTest.java
===================================================================
--- /luni/src/test/java/tests/api/java/util/CollectionsTest.java (revision 393698)
+++ /luni/src/test/java/tests/api/java/util/CollectionsTest.java (working copy)
@@ -585,7 +585,7 @@
Collections.shuffle(list, new Random(200));
for (int counter = 0; counter < size - 1; counter++) {
- if (((Integer) list.get(counter)).compareTo(list.get(counter + 1)) > 0) {
+ if (((Integer) list.get(counter)).compareTo((Integer)list.get(counter + 1)) > 0) {
sorted = false;
}
}
@@ -668,7 +668,7 @@
for (int counter = 0; counter < llSize - 1; counter++) {
assertTrue(
"Sorting shuffled list resulted in unsorted list",
- ((Integer) ll.get(counter)).compareTo(ll.get(counter + 1)) < 0);
+ ((Integer) ll.get(counter)).compareTo((Integer)ll.get(counter + 1)) < 0);
}
for (int counter = 0; counter < rllSize - 1; counter++) {
@@ -674,7 +674,7 @@
for (int counter = 0; counter < rllSize - 1; counter++) {
assertTrue("Sorting reversed list resulted in unsorted list",
((Integer) reversedLinkedList.get(counter))
- .compareTo(reversedLinkedList.get(counter + 1)) < 0);
+ .compareTo((Integer)reversedLinkedList.get(counter + 1)) < 0);
}
}