You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by sm...@apache.org on 2006/05/09 05:47:06 UTC
svn commit: r400302 - /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java /incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java
Author: smishura
Date: Sat May 6 05:28:20 2006
New Revision: 400302
URL: http://svn.apache.org/viewcvs?view=rev&rev=400302
Log:
Apply patch for HARMONY-294 ([classlib][luni] Implement the toString methods in java.util.Arrays).
Modified:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java
Url: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java?view=diff&rev=400302&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java&r1=400301&p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java&r2=400302
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Arrays.java Sat May 6 05:28:20 2006
@@ -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,
@@ -2012,4 +2014,418 @@
if ((length = d - c) > 0)
sort(end - length, end, array);
}
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>boolean[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(boolean)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>boolean</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>byte[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(byte)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>byte</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>char[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(char)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>char</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>double[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(double)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>double</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>float[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(float)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>float</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>int[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(int)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>int</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>long[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(long)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>long</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>short[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(short)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>short</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <code>String</code> representation of the
+ * <code>Object[]</code> passed. The result is surrounded by brackets (<code>"[]"</code>),
+ * each element is converted to a <code>String</code> via the
+ * {@link String#valueOf(Object)} and separated by
+ * <code>", "</code>. If the array is <code>null</code>,
+ * then <code>"null"</code> is returned.
+ * </p>
+ *
+ * @param array The <code>Object</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @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();
+ }
+
+ /**
+ * <p>
+ * Creates a <i>"deep"</i> <code>String</code> representation of the
+ * <code>Object[]</code> passed, such that if the array contains other
+ * arrays, the <code>String</code> representation of those arrays is
+ * generated as well.
+ * </p>
+ * <p>
+ * If any of the elements are primitive arrays, the generation is delegated
+ * to the other <code>toString</code> methods in this class. If any
+ * element contains a reference to the original array, then it will be
+ * represented as <code>"[...]"</code>. If an element is an
+ * <code>Object[]</code>, then its representation is generated by a
+ * recursive call to this method. All other elements are converted via the
+ * {@link String#valueOf(Object)} method.
+ * </p>
+ *
+ * @param array The <code>Object</code> array to convert.
+ * @return The <code>String</code> representation of <code>array</code>.
+ * @since 1.5
+ */
+ public static String deepToString(Object[] array) {
+ // delegate this to the recursive method
+ return deepToStringImpl(array, new Object[] { array }, null);
+ }
+
+ /**
+ * <p>
+ * Implementation method used by {@link #deepToString(Object[])}.
+ * </p>
+ *
+ * @param array The <code>Object[]</code> to dive into.
+ * @param original The original <code>Object[]</code>; used to test for
+ * self references.
+ * @param sb The <code>StringBuilder</code> instance to append to or
+ * <code>null</code> 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();
+ }
+
+ /**
+ * <p>
+ * Utility method used to assist the implementation of
+ * {@link #deepToString(Object[])}.
+ * </p>
+ *
+ * @param origArrays An array of Object[] references.
+ * @param array An Object[] reference to look for in <code>origArrays</code>.
+ * @return A value of <code>true</code> if <code>array</code> is an
+ * element in <code>origArrays</code>.
+ */
+ 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;
+ }
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java
Url: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java?view=diff&rev=400302&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java&r1=400301&p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java&r2=400302
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/util/ArraysTest.java Sat May 6 05:28:20 2006
@@ -175,4 +175,153 @@
assertTrue("Assert 2: specials sort incorrectly" + Arrays.asList(print2),
Arrays.equals(specials2, answer));
}
+
+ /**
+ * @tests java.util.Arrays#toString(boolean[])
+ */
+ 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}));
+ }
+
+ /**
+ * @tests java.util.Arrays#toString(byte[])
+ */
+ 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}));
+ }
+
+ /**
+ * @tests java.util.Arrays#toString(char[])
+ */
+ 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'}));
+ }
+
+ /**
+ * @tests java.util.Arrays#toString(double[])
+ */
+ 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}));
+ }
+
+ /**
+ * @tests java.util.Arrays#toString(float[])
+ */
+ 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}));
+ }
+
+ /**
+ * @tests java.util.Arrays#toString(int[])
+ */
+ 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}));
+ }
+
+ /**
+ * @tests java.util.Arrays#toString(long[])
+ */
+ 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}));
+ }
+
+ /**
+ * @tests java.util.Arrays#toString(short[])
+ */
+ 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}));
+ }
+
+ /**
+ * @tests java.util.Arrays#toString(Object[])
+ */
+ 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"}));
+ }
+
+ /**
+ * @tests java.util.Arrays#deepToString(Object[])
+ */
+ 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));
+ }
}