Return-Path: Delivered-To: apmail-incubator-harmony-commits-archive@www.apache.org Received: (qmail 56741 invoked from network); 9 May 2006 03:47:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 9 May 2006 03:47:28 -0000 Received: (qmail 56340 invoked by uid 500); 9 May 2006 03:47:28 -0000 Delivered-To: apmail-incubator-harmony-commits-archive@incubator.apache.org Received: (qmail 56304 invoked by uid 500); 9 May 2006 03:47:27 -0000 Mailing-List: contact harmony-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: harmony-dev@incubator.apache.org Delivered-To: mailing list harmony-commits@incubator.apache.org Received: (qmail 56293 invoked by uid 99); 9 May 2006 03:47:27 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 May 2006 20:47:27 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 08 May 2006 20:47:26 -0700 Received: (qmail 56644 invoked by uid 65534); 9 May 2006 03:47:06 -0000 Date: 9 May 2006 03:47:06 -0000 Message-ID: <20060509034706.56641.qmail@minotaur.apache.org> From: smishura@apache.org To: harmony-commits@incubator.apache.org Subject: 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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); } + + /** + *

+ * 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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. + *

+ * + * @param array The 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 The Object[] 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 in origArrays. + * @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; + } } 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)); + } }