Index: /luni/src/main/java/java/lang/Boolean.java =================================================================== --- /luni/src/main/java/java/lang/Boolean.java (revision 394440) +++ /luni/src/main/java/java/lang/Boolean.java (working copy) @@ -15,12 +15,13 @@ package java.lang; +import java.io.Serializable; + /** *

Boolean is the wrapper for the primitive type boolean.

* @since 1.0 */ -public final class Boolean implements java.io.Serializable { - //TODO Add Comparable to implements when generics are supported. +public final class Boolean implements Serializable, Comparable { private static final long serialVersionUID = -3665804199014368530L; @@ -30,10 +31,10 @@ private final boolean value; /** - * The java.lang.Class that represents this class. - */ - public static final Class TYPE = new boolean[0].getClass() - .getComponentType(); + * The java.lang.Class that represents this class. + */ + public static final Class TYPE = new boolean[0].getClass() + .getComponentType(); // Note: This can't be set to "boolean.class", since *that* is // defined to be "java.lang.Boolean.TYPE"; @@ -58,7 +59,7 @@ * The name of the desired boolean. */ public Boolean(String string) { - this(toBoolean(string)); + this(parseBoolean(string)); } /** @@ -100,22 +101,34 @@ return (o == this) || ((o instanceof Boolean) && (value == ((Boolean) o).value)); } + + /** + *

+ * Compares this Boolean to another Boolean. + * If this instance has the same value as the instance passed, then + * 0 is returned. If this instance is true and + * the instance passed is false, then a positive value is + * returned. If this instance is false and the instance + * passed is true, then a negative value is returned. + *

+ * + * @param that The instance to compare to. + * @throws java.lang.NullPointerException if that is + * null. + * @since 1.5 + * @see java.lang.Comparable + */ + public int compareTo(Boolean that) { + if (that == null) + throw new NullPointerException(); + + if (this.value == that.value) + return 0; + + return this.value ? 1 : -1; + } /** - * Answers true if the system property described by the argument equal to - * "true" using case insensitive comparison, and false otherwise. - * - * @param string - * The name of the desired boolean. - * @return The boolean value. - */ - public static boolean getBoolean(String string) { - if (string == null || string.length() == 0) - return false; - return (toBoolean(System.getProperty(string))); - } - - /** * Answers an integer hash code for the receiver. Any two objects which * answer true when passed to equals must * answer the same value for this method. @@ -127,20 +140,31 @@ public int hashCode() { return value ? 1231 : 1237; } - - /** - * Answers true if the argument is equal to "true" using case insensitive - * comparison, and false otherwise. - * - * @param string - * The name of the desired boolean. - * @return the boolean value. - */ - static boolean toBoolean(String string) { - return parseBoolean(string); - } /** + * Answers a string containing a concise, human-readable description of the + * receiver. + * + * @return a printable representation for the receiver. + */ + public String toString() { + return String.valueOf(value); + } + + /** + * Answers true if the system property described by the argument equal to + * "true" using case insensitive comparison, and false otherwise. + * + * @param string The name of the desired boolean. + * @return The boolean value. + */ + public static boolean getBoolean(String string) { + if (string == null || string.length() == 0) + return false; + return (parseBoolean(System.getProperty(string))); + } + + /** *

* Parses the string as a boolean. If the string is not * null and is equal to "true", regardless @@ -156,16 +180,6 @@ } /** - * Answers a string containing a concise, human-readable description of the - * receiver. - * - * @return a printable representation for the receiver. - */ - public String toString() { - return String.valueOf(value); - } - - /** * Converts the specified boolean to its string representation. When the * boolean is true answer "true", otherwise answer * "false". @@ -188,7 +202,7 @@ * @return the boolean value. */ public static Boolean valueOf(String string) { - return toBoolean(string) ? Boolean.TRUE : Boolean.FALSE; + return parseBoolean(string) ? Boolean.TRUE : Boolean.FALSE; } /** Index: /luni/src/test/java/org/apache/harmony/tests/java/lang/BooleanTest.java =================================================================== --- /luni/src/test/java/org/apache/harmony/tests/java/lang/BooleanTest.java (revision 392441) +++ /luni/src/test/java/org/apache/harmony/tests/java/lang/BooleanTest.java (working copy) @@ -132,4 +132,20 @@ assertFalse(Boolean.parseBoolean("")); assertFalse(Boolean.parseBoolean("invalid")); } + + /** + * @tests java.lang.Boolean#compareTo(Boolean) + */ + public void test_compareToLjava_lang_Boolean() { + assertTrue(Boolean.TRUE.compareTo(Boolean.TRUE) == 0); + assertTrue(Boolean.FALSE.compareTo(Boolean.FALSE) == 0); + assertTrue(Boolean.TRUE.compareTo(Boolean.FALSE) > 0); + assertTrue(Boolean.FALSE.compareTo(Boolean.TRUE) < 0); + + try { + Boolean.TRUE.compareTo(null); + fail("No NPE"); + } catch (NullPointerException e) { + } + } }