Index: /luni/src/main/java/java/util/Arrays.java =================================================================== --- /luni/src/main/java/java/util/Arrays.java (revision 390753) +++ /luni/src/main/java/java/util/Arrays.java (working copy) @@ -21,6 +21,8 @@ /** * Arrays contains static methods which operate on arrays. + * + * @since 1.2 */ public class Arrays { @@ -312,7 +314,7 @@ * is the -index - 1 where the element would be inserted * * @exception ClassCastException - * when an element in the array or the seach element does not + * when an element in the array or the search element does not * implement Comparable, or cannot be compared to each other */ public static int binarySearch(Object[] array, Object object) { @@ -344,7 +346,7 @@ * is the -index - 1 where the element would be inserted * * @exception ClassCastException - * when an element in the array and the seach element cannot + * when an element in the array and the search element cannot * be compared to each other using the Comparator */ public static int binarySearch(Object[] array, Object object, @@ -1985,4 +1987,418 @@ if ((length = d - c) > 0) sort(end - length, end, array); } + + /** + *
+ * Creates a String representation of the
+ * boolean[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(boolean)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
boolean array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(boolean[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 5);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a String representation of the
+ * byte[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(byte)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
byte array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(byte[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 3);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a String representation of the
+ * char[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(char)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
char array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(char[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 2);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a String representation of the
+ * double[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(double)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
double array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(double[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 5);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a String representation of the
+ * float[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(float)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
float array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(float[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 5);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a String representation of the
+ * int[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(int)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
int array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(int[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 4);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a String representation of the
+ * long[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(long)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
long array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(long[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 4);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a String representation of the
+ * short[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(short)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
short array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(short[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 4);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a String representation of the
+ * Object[] passed. The result is surrounded by brackets ("[]"),
+ * each element is converted to a String via the
+ * {@link String#valueOf(Object)} and separated by
+ * ", ". If the array is null,
+ * then "null" is returned.
+ *
Object array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String toString(Object[] array) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+ StringBuilder sb = new StringBuilder(2 + array.length * 5);
+ sb.append('[');
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ sb.append(array[i]);
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ *
+ * Creates a "deep" String representation of the
+ * Object[] passed, such that if the array contains other
+ * arrays, the String representation of those arrays is
+ * generated as well.
+ *
+ * If any of the elements are primitive arrays, the generation is delegated
+ * to the other toString methods in this class. If any
+ * element contains a reference to the original array, then it will be
+ * represented as "[...]". If an element is an
+ * Object[], then its representation is generated by a
+ * recursive call to this method. All other elements are converted via the
+ * {@link String#valueOf(Object)} method.
+ *
Object array to convert.
+ * @return The String representation of array.
+ * @since 1.5
+ */
+ public static String deepToString(Object[] array) {
+ // delegate this to the recursive method
+ return deepToStringImpl(array, new Object[] { array }, null);
+ }
+
+ /**
+ * + * Implementation method used by {@link #deepToString(Object[])}. + *
+ * + * @param array TheObject[] to dive into.
+ * @param original The original Object[]; used to test for
+ * self references.
+ * @param sb The StringBuilder instance to append to or
+ * null one hasn't been created yet.
+ * @return The result.
+ * @see #deepToString(Object[])
+ */
+ private static String deepToStringImpl(Object[] array, Object[] origArrays,
+ StringBuilder sb) {
+ if (array == null)
+ return "null";
+ if (array.length == 0)
+ return "[]";
+
+ if (sb == null)
+ sb = new StringBuilder(2 + array.length * 5);
+ sb.append('[');
+
+ for (int i = 0; i < array.length; i++) {
+ if (i != 0) {
+ sb.append(", ");
+ }
+ // establish current element
+ Object elem = array[i];
+ if (elem == null) {
+ // element is null
+ sb.append("null");
+ } else {
+ // get the Class of the current element
+ Class elemClass = elem.getClass();
+ if (elemClass.isArray()) {
+ // element is an array type
+
+ // get the declared Class of the array (element)
+ Class elemElemClass = elemClass.getComponentType();
+ if (elemElemClass.isPrimitive()) {
+ // element is a primitive array
+ if (boolean.class.equals(elemElemClass)) {
+ sb.append(toString((boolean[]) elem));
+ } else if (byte.class.equals(elemElemClass)) {
+ sb.append(toString((byte[]) elem));
+ } else if (char.class.equals(elemElemClass)) {
+ sb.append(toString((char[]) elem));
+ } else if (double.class.equals(elemElemClass)) {
+ sb.append(toString((double[]) elem));
+ } else if (float.class.equals(elemElemClass)) {
+ sb.append(toString((float[]) elem));
+ } else if (int.class.equals(elemElemClass)) {
+ sb.append(toString((int[]) elem));
+ } else if (long.class.equals(elemElemClass)) {
+ sb.append(toString((long[]) elem));
+ } else if (short.class.equals(elemElemClass)) {
+ sb.append(toString((short[]) elem));
+ } else {
+ // no other possible primitives, so we assert that
+ throw new AssertionError();
+ }
+ } else {
+ // element is an Object[], so we assert that
+ assert elem instanceof Object[];
+ if (deepToStringImplContains(origArrays, elem)) {
+ sb.append("[...]");
+ } else {
+ Object[] newArray = (Object[]) elem;
+ Object[] newOrigArrays = new Object[origArrays.length + 1];
+ System.arraycopy(origArrays, 0, newOrigArrays, 0,
+ origArrays.length);
+ newOrigArrays[origArrays.length] = newArray;
+ // make the recursive call to this method
+ deepToStringImpl(newArray, newOrigArrays, sb);
+ }
+ }
+ } else { // element is NOT an array, just an Object
+ sb.append(array[i]);
+ }
+ }
+ }
+ sb.append(']');
+ return sb.toString();
+ }
+
+ /**
+ * + * Utility method used to assist the implementation of + * {@link #deepToString(Object[])}. + *
+ * + * @param origArrays An array of Object[] references. + * @param array An Object[] reference to look for inorigArrays.
+ * @return A value of true if array is an
+ * element in origArrays.
+ */
+ private static boolean deepToStringImplContains(Object[] origArrays,
+ Object array) {
+ if (origArrays == null || origArrays.length == 0)
+ return false;
+ for (int i = 0; i < origArrays.length; i++) {
+ if (origArrays[i] == array)
+ return true;
+ }
+ return false;
+ }
}
Index: /luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java
===================================================================
--- /luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java (revision 386494)
+++ /luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java (working copy)
@@ -175,4 +175,123 @@
assertTrue("Assert 2: specials sort incorrectly" + Arrays.asList(print2),
Arrays.equals(specials2, answer));
}
+
+ public void test_toString$Z() {
+ assertEquals("null", Arrays.toString((boolean[])null));
+ assertEquals("[]", Arrays.toString(new boolean[] {}));
+ assertEquals("[true]", Arrays.toString(new boolean[] {true}));
+ assertEquals("[true, false]", Arrays.toString(new boolean[] {true,false}));
+ assertEquals("[true, false, true]", Arrays.toString(new boolean[] {true,false,true}));
+ }
+
+ public void test_toString$B() {
+ assertEquals("null", Arrays.toString((byte[])null));
+ assertEquals("[]", Arrays.toString(new byte[] {}));
+ assertEquals("[0]", Arrays.toString(new byte[] {0}));
+ assertEquals("[-1, 0]", Arrays.toString(new byte[] {-1,0}));
+ assertEquals("[-1, 0, 1]", Arrays.toString(new byte[] {-1,0,1}));
+ }
+
+ public void test_toString$C() {
+ assertEquals("null", Arrays.toString((char[])null));
+ assertEquals("[]", Arrays.toString(new char[] {}));
+ assertEquals("[a]", Arrays.toString(new char[] {'a'}));
+ assertEquals("[a, b]", Arrays.toString(new char[] {'a','b'}));
+ assertEquals("[a, b, c]", Arrays.toString(new char[] {'a','b','c'}));
+ }
+
+ public void test_toString$D() {
+ assertEquals("null", Arrays.toString((double[])null));
+ assertEquals("[]", Arrays.toString(new double[] {}));
+ assertEquals("[0.0]", Arrays.toString(new double[] {0.0D}));
+ assertEquals("[-1.0, 0.0]", Arrays.toString(new double[] {-1.0D, 0.0D}));
+ assertEquals("[-1.0, 0.0, 1.0]", Arrays.toString(new double[] {-1.0D, 0.0D, 1.0D}));
+ }
+
+ public void test_toString$F() {
+ assertEquals("null", Arrays.toString((float[])null));
+ assertEquals("[]", Arrays.toString(new float[] {}));
+ assertEquals("[0.0]", Arrays.toString(new float[] {0.0F}));
+ assertEquals("[-1.0, 0.0]", Arrays.toString(new float[] {-1.0F, 0.0F}));
+ assertEquals("[-1.0, 0.0, 1.0]", Arrays.toString(new float[] {-1.0F, 0.0F, 1.0F}));
+ }
+
+ public void test_toString$I() {
+ assertEquals("null", Arrays.toString((int[])null));
+ assertEquals("[]", Arrays.toString(new int[] {}));
+ assertEquals("[0]", Arrays.toString(new int[] {0}));
+ assertEquals("[-1, 0]", Arrays.toString(new int[] {-1, 0}));
+ assertEquals("[-1, 0, 1]", Arrays.toString(new int[] {-1, 0, 1}));
+ }
+
+ public void test_toString$J() {
+ assertEquals("null", Arrays.toString((long[])null));
+ assertEquals("[]", Arrays.toString(new long[] {}));
+ assertEquals("[0]", Arrays.toString(new long[] {0}));
+ assertEquals("[-1, 0]", Arrays.toString(new long[] {-1, 0}));
+ assertEquals("[-1, 0, 1]", Arrays.toString(new long[] {-1, 0, 1}));
+ }
+
+ public void test_toString$S() {
+ assertEquals("null", Arrays.toString((short[])null));
+ assertEquals("[]", Arrays.toString(new short[] {}));
+ assertEquals("[0]", Arrays.toString(new short[] {0}));
+ assertEquals("[-1, 0]", Arrays.toString(new short[] {-1, 0}));
+ assertEquals("[-1, 0, 1]", Arrays.toString(new short[] {-1, 0, 1}));
+ }
+
+ public void test_toString$Ljava_lang_Object() {
+ assertEquals("null", Arrays.toString((Object[])null));
+ assertEquals("[]", Arrays.toString(new Object[] {}));
+ assertEquals("[fixture]", Arrays.toString(new Object[] {"fixture"}));
+ assertEquals("[fixture, null]", Arrays.toString(new Object[] {"fixture", null}));
+ assertEquals("[fixture, null, fixture]", Arrays.toString(new Object[] {"fixture", null, "fixture"}));
+ }
+
+ public void test_deepToString$java_lang_Object() {
+ assertEquals("null", Arrays.deepToString((Object[])null));
+ assertEquals("[]", Arrays.deepToString(new Object[] {}));
+ assertEquals("[fixture]", Arrays.deepToString(new Object[] {"fixture"}));
+ assertEquals("[fixture, null]", Arrays.deepToString(new Object[] {"fixture", null}));
+ assertEquals("[fixture, null, fixture]", Arrays.deepToString(new Object[] {"fixture", null, "fixture"}));
+
+ Object[] fixture = new Object[1];
+ fixture[0] = fixture;
+ assertEquals("[[...]]", Arrays.deepToString(fixture));
+
+ fixture = new Object[2];
+ fixture[0] = "fixture";
+ fixture[1] = fixture;
+ assertEquals("[fixture, [...]]", Arrays.deepToString(fixture));
+
+ fixture = new Object[10];
+ fixture[0] = new boolean[] {true, false};
+ fixture[1] = new byte[] {0, 1};
+ fixture[2] = new char[] {'a', 'b'};
+ fixture[3] = new double[] {0.0D, 1.0D};
+ fixture[4] = new float[] {0.0F, 1.0F};
+ fixture[5] = new int[] {0, 1};
+ fixture[6] = new long[] {0L, 1L};
+ fixture[7] = new short[] {0, 1};
+ fixture[8] = fixture[0];
+ fixture[9] = new Object[9];
+ ((Object[])fixture[9])[0] = fixture;
+ ((Object[])fixture[9])[1] = fixture[1];
+ ((Object[])fixture[9])[2] = fixture[2];
+ ((Object[])fixture[9])[3] = fixture[3];
+ ((Object[])fixture[9])[4] = fixture[4];
+ ((Object[])fixture[9])[5] = fixture[5];
+ ((Object[])fixture[9])[6] = fixture[6];
+ ((Object[])fixture[9])[7] = fixture[7];
+ Object[] innerFixture = new Object[4];
+ innerFixture[0] = "innerFixture0";
+ innerFixture[1] = innerFixture;
+ innerFixture[2] = fixture;
+ innerFixture[3] = "innerFixture3";
+ ((Object[])fixture[9])[8] = innerFixture;
+
+ String expected = "[[true, false], [0, 1], [a, b], [0.0, 1.0], [0.0, 1.0], [0, 1], [0, 1], [0, 1], [true, false], [[...], [0, 1], [a, b], [0.0, 1.0], [0.0, 1.0], [0, 1], [0, 1], [0, 1], [innerFixture0, [...], [...], innerFixture3]]]";
+
+ assertEquals(expected, Arrays.deepToString(fixture));
+ }
}