You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/27 18:40:27 UTC

svn commit: r389195 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/lang/StringBuilder.java test/java/org/apache/harmony/tests/java/lang/StringBuilderTest.java

Author: tellison
Date: Mon Mar 27 08:40:26 2006
New Revision: 389195

URL: http://svn.apache.org/viewcvs?rev=389195&view=rev
Log:
Apply patch HARMONY-253 ([classlib][luni] Implement StringBuilder code point functionality)

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuilder.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBuilderTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuilder.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuilder.java?rev=389195&r1=389194&r2=389195&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuilder.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuilder.java Mon Mar 27 08:40:26 2006
@@ -20,8 +20,6 @@
 import java.io.ObjectOutputStream;
 import java.io.Serializable;
 
-import org.apache.harmony.luni.util.NotYetImplementedException;
-
 /**
  * <p>
  * A modifiable {@link CharSequence sequence of characters} for use in creating
@@ -78,10 +76,10 @@
 
     /**
      * <p>
-     * Constructs an instance with a specificed capacity.
+     * Constructs an instance with a specified capacity.
      * </p>
      * 
-     * @param capacity The initial capicity to use.
+     * @param capacity The initial capacity to use.
      * 
      * @throws NegativeArraySizeException if the <code>capacity</code>
      *         parameter is <code>null</code>.
@@ -357,14 +355,17 @@
 
     /**
      * <p>
-     * <b>NOTE - This method is currently not implemented and always throws a
-     * {@link NotYetImplementedException}.</b>
+     * Appends the encoded Unicode code point to this object. The code point is
+     * converted to a <code>char[]</code> as defined by
+     * {@link Character#toChars(int)}.
      * </p>
-     * TODO javadoc
+     * 
+     * @param codePoint The Unicode code point to encode and append.
+     * @return A reference to this object.
+     * @see Character#toChars(int)
      */
     public StringBuilder appendCodePoint(int codePoint) {
-        // TODO Implement Java 5 code point functionality.
-        throw new NotYetImplementedException();
+        return append(Character.toChars(codePoint));
     }
 
     /**
@@ -396,41 +397,57 @@
 
     /**
      * <p>
-     * <b>NOTE - This method is currently NOT completely implemented and just
-     * delegates to the {@link #charAt(int)} method.</b>
+     * Retrieves the Unicode code point value at the <code>index</code>.
      * </p>
-     * TODO javadoc
+     * 
+     * @param index The index to the <code>char</code> code unit within this
+     *        object.
+     * @return The Unicode code point value.
+     * @throws IndexOutOfBoundsException if <code>index</code> is negative or
+     *         greater than or equal to {@link #length()}.
+     * @see Character
+     * @see Character#codePointAt(char[], int, int)
      */
     public int codePointAt(int index) {
-        // TODO Implement Java 5 code point functionality.
-        return charAt(index);
+        return Character.codePointAt(buffer, index, length);
     }
 
     /**
      * <p>
-     * <b>NOTE - This method is currently NOT completely implemented and just
-     * delegates to the {@link #charAt(int)} method by retrieving the character
-     * at the preceding index.</b>
+     * Retrieves the Unicode code point value that precedes the
+     * <code>index</code>.
      * </p>
-     * TODO javadoc
+     * 
+     * @param index The index to the <code>char</code> code unit within this
+     *        object.
+     * @return The Unicode code point value.
+     * @throws IndexOutOfBoundsException if <code>index</code> is less than 1
+     *         or greater than {@link #length()}.
+     * @see Character
+     * @see Character#codePointBefore(char[], int, int)
      */
     public int codePointBefore(int index) {
-        // TODO Implement Java 5 code point functionality.
-        return codePointAt(index - 1);
+        return Character.codePointBefore(buffer, index);
     }
 
     /**
      * <p>
-     * <b>NOTE - This method is currently NOT completely implemented and just
-     * return the difference between the index parameters.</b>
+     * Calculates the number of Unicode code points between
+     * <code>beginIndex</code> and <code>endIndex</code>.
      * </p>
-     * TODO javadoc
+     * 
+     * @param beginIndex The inclusive beginning index of the subsequence.
+     * @param endIndex The exclusive end index of the subsequence.
+     * @return The number of Unicode code points in the subsequence.
+     * @throws IndexOutOfBoundsException if <code>beginIndex</code> is
+     *         negative or greater than <code>endIndex</code> or
+     *         <code>endIndex</code> is greater than {@link #length()}.
      */
     public int codePointCount(int beginIndex, int endIndex) {
-        // TODO Implement Java 5 code point functionality.
         if (beginIndex < 0 || endIndex > length || beginIndex > endIndex)
             throw new IndexOutOfBoundsException();
-        return endIndex - beginIndex;
+        return Character.codePointCount(buffer, beginIndex, endIndex
+                - beginIndex);
     }
 
     /**
@@ -470,7 +487,7 @@
      * @param index The index of the character to delete.
      * @return A reference to this object.
      * @throws StringIndexOutOfBoundsException if <code>index</code> is less
-     *         than zero or is greather than or equal to the current length.
+     *         than zero or is greater than or equal to the current length.
      */
     public StringBuilder deleteCharAt(int index) {
         // check for index values past length, as 'delete' will massage them out
@@ -481,12 +498,12 @@
 
     /**
      * <p>
-     * Ensures that this object has a minimum capicity available before
-     * requiring the internal buffer to be enlarged. The general policy of thi
+     * Ensures that this object has a minimum capacity available before
+     * requiring the internal buffer to be enlarged. The general policy of this
      * method is that if the <code>minimumCapacity</code> is larger than the
      * current {@link #capacity()}, then the capacity will be increased to the
      * largest value of either the <code>minimumCapacity</code> or the current
-     * capcity multiplied by two plus two. Although this is the general policy,
+     * capacity multiplied by two plus two. Although this is the general policy,
      * there is no guarantee that the capacity will change.
      * </p>
      * 
@@ -523,7 +540,7 @@
      * @throws IndexOutOfBoundsException if the <code>srcBegin</code> is
      *         negative, the <code>dstBegin</code> is negative, the
      *         <code>srcBegin</code> is greater than <code>srcEnd</code>,
-     *         the <code>srcEnd</code> is greather than the current
+     *         the <code>srcEnd</code> is greater than the current
      *         {@link #length()} or <code>dstBegin + srcEnd - srcBegin</code>
      *         is greater than <code>dst.lenbth</code>.
      */
@@ -558,8 +575,7 @@
     public int indexOf(String str) {
         if (str == null)
             throw new NullPointerException();
-        // TODO optimize
-        return this.toString().indexOf(str);
+        return indexOf(str, 0);
     }
 
     /**
@@ -584,7 +600,7 @@
     public int indexOf(String str, int fromIndex) {
         if (str == null)
             throw new NullPointerException();
-        // TODO optimize
+        //TODO optimize
         return this.toString().indexOf(str, fromIndex);
     }
 
@@ -923,8 +939,7 @@
     public int lastIndexOf(String str) {
         if (str == null)
             throw new NullPointerException();
-        // TODO optimize
-        return this.toString().lastIndexOf(str);
+        return lastIndexOf(str, length);
     }
 
     /**
@@ -966,14 +981,21 @@
 
     /**
      * <p>
-     * <b>NOTE - This method is currently not implemented and always throws a
-     * {@link NotYetImplementedException}.</b>
+     * Returns the index within this object that is offset from
+     * <code>index</code> by <code>codePointOffset</code> code points.
      * </p>
-     * TODO javadoc
+     * 
+     * @param index The index within this object to calculate the offset from.
+     * @param codePointOffset The number of code points to count.
+     * @return The index within this object that is the offset.
+     * @throws IndexOutOfBoundsException if <code>index</code> is negative or
+     *         greater than {@link #length()} or if there aren't enough code
+     *         points before or after <code>index</code> to match
+     *         <code>codePointOffset</code>.
      */
     public int offsetByCodePoints(int index, int codePointOffset) {
-        // TODO Implement Java 5 code point functionality.
-        throw new NotYetImplementedException();
+        return Character.offsetByCodePoints(buffer, 0, length, index,
+                codePointOffset);
     }
 
     /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBuilderTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBuilderTest.java?rev=389195&r1=389194&r2=389195&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBuilderTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBuilderTest.java Mon Mar 27 08:40:26 2006
@@ -23,7 +23,7 @@
 
 public class StringBuilderTest extends TestCase {
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.StringBuilder()
 	 */
 	public void test_Constructor() {
@@ -32,7 +32,7 @@
 		assertEquals(16, sb.capacity());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.StringBuilder(int)
 	 */
 	public void test_ConstructorI() {
@@ -50,7 +50,7 @@
 		assertNotNull(new StringBuilder(0));
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.StringBuilder(CharSequence)
 	 */
 	public void test_ConstructorLjava_lang_CharSequence() {
@@ -70,7 +70,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.StringBuilder(String)
 	 */
 	public void test_ConstructorLjava_lang_String() {
@@ -85,7 +85,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(boolean)
 	 */
 	public void test_appendZ() {
@@ -97,7 +97,7 @@
 		assertEquals("false", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(char)
 	 */
 	public void test_appendC() {
@@ -109,7 +109,7 @@
 		assertEquals("b", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(char[])
 	 */
 	public void test_append$C() {
@@ -127,7 +127,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(char[], int, int)
 	 */
 	public void test_append$CII() {
@@ -179,7 +179,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(CharSequence)
 	 */
 	public void test_appendLjava_lang_CharSequence() {
@@ -194,7 +194,7 @@
 		assertEquals("null", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(CharSequence, int, int)
 	 */
 	public void test_appendLjava_lang_CharSequenceII() {
@@ -215,7 +215,7 @@
 		assertEquals("nu", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(double)
 	 */
 	public void test_appendD() {
@@ -245,7 +245,7 @@
 		assertEquals(String.valueOf(Double.MAX_VALUE), sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(float)
 	 */
 	public void test_appendF() {
@@ -275,7 +275,7 @@
 		assertEquals(String.valueOf(Float.MAX_VALUE), sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(int)
 	 */
 	public void test_appendI() {
@@ -296,7 +296,7 @@
 		assertEquals(String.valueOf(Integer.MAX_VALUE), sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(long)
 	 */
 	public void test_appendL() {
@@ -317,7 +317,7 @@
 		assertEquals(String.valueOf(Integer.MAX_VALUE), sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(Object)'
 	 */
 	public void test_appendLjava_lang_Object() {
@@ -330,7 +330,7 @@
 		assertEquals("null", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(String)
 	 */
 	public void test_appendLjava_lang_String() {
@@ -345,7 +345,7 @@
 		assertEquals("null", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.append(StringBuffer)
 	 */
 	public void test_appendLjava_lang_StringBuffer() {
@@ -360,14 +360,20 @@
 		assertEquals("null", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.appendCodePoint(int)'
 	 */
 	public void test_appendCodePointI() {
-		// TODO Implement Java 5 code point functionality.
+		StringBuilder sb = new StringBuilder();
+        sb.appendCodePoint(0x10000);
+        assertEquals("\uD800\uDC00", sb.toString());
+        sb.append("fixture");
+        assertEquals("\uD800\uDC00fixture", sb.toString());
+        sb.appendCodePoint(0x00010FFFF);
+        assertEquals("\uD800\uDC00fixture\uDBFF\uDFFF", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.capacity()'
 	 */
 	public void test_capacity() {
@@ -377,7 +383,7 @@
 		assertTrue(sb.capacity() > 16);
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.charAt(int)'
 	 */
 	public void test_charAtI() {
@@ -407,28 +413,48 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.codePointAt(int)
 	 */
 	public void test_codePointAtI() {
-		// TODO Implement Java 5 code point functionality.
+        StringBuilder sb = new StringBuilder("abc");
+        assertEquals('a', sb.codePointAt(0));
+        assertEquals('b', sb.codePointAt(1));
+        assertEquals('c', sb.codePointAt(2));
+        
+        sb = new StringBuilder("\uD800\uDC00");
+        assertEquals(0x10000, sb.codePointAt(0));
+        assertEquals('\uDC00', sb.codePointAt(1));
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.codePointBefore(int)
 	 */
 	public void test_codePointBeforeI() {
-		// TODO Implement Java 5 code point functionality.
+        StringBuilder sb = new StringBuilder("abc");
+        assertEquals('a', sb.codePointBefore(1));
+        assertEquals('b', sb.codePointBefore(2));
+        assertEquals('c', sb.codePointBefore(3));
+        
+        sb = new StringBuilder("\uD800\uDC00");
+        assertEquals(0x10000, sb.codePointBefore(2));
+        assertEquals('\uD800', sb.codePointBefore(1));
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.codePointCount(int, int)
 	 */
 	public void test_codePointCountII() {
-		// TODO Implement Java 5 code point functionality.
+        assertEquals(1, new StringBuilder("\uD800\uDC00").codePointCount(0, 2));
+        assertEquals(1, new StringBuilder("\uD800\uDC01").codePointCount(0, 2));
+        assertEquals(1, new StringBuilder("\uD801\uDC01").codePointCount(0, 2));
+        assertEquals(1, new StringBuilder("\uDBFF\uDFFF").codePointCount(0, 2));
+
+        assertEquals(3, new StringBuilder("a\uD800\uDC00b").codePointCount(0, 4));
+        assertEquals(4, new StringBuilder("a\uD800\uDC00b\uD800").codePointCount(0, 5));
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.delete(int, int)
 	 */
 	public void test_deleteII() {
@@ -472,7 +498,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.deleteCharAt(int)
 	 */
 	public void test_deleteCharAtI() {
@@ -512,7 +538,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.ensureCapacity(int)'
 	 */
 	public void test_ensureCapacityI() {
@@ -526,7 +552,7 @@
 		assertEquals(55, sb.capacity());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.getChars(int, int, char[], int)'
 	 */
 	public void test_getCharsII$CI() {
@@ -597,7 +623,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.indexOf(String)
 	 */
 	public void test_indexOfLjava_lang_String() {
@@ -616,7 +642,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.indexOf(String, int)
 	 */
 	public void test_IndexOfStringInt() {
@@ -645,7 +671,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, boolean)
 	 */
 	public void test_insertIZ() {
@@ -687,7 +713,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, char)
 	 */
 	public void test_insertIC() {
@@ -734,7 +760,7 @@
 //		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, char[])'
 	 */
 	public void test_insertI$C() {
@@ -785,7 +811,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, char[], int, int)
 	 */
 	public void test_insertI$CII() {
@@ -875,7 +901,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, CharSequence)
 	 */
 	public void test_insertILjava_lang_CharSequence() {
@@ -917,7 +943,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, CharSequence, int, int)
 	 */
 	public void test_insertILjava_lang_CharSequenceII() {
@@ -998,7 +1024,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, double)
 	 */
 	public void test_insertID() {
@@ -1040,7 +1066,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, float)
 	 */
 	public void test_insertIF() {
@@ -1082,7 +1108,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, int)
 	 */
 	public void test_insertII() {
@@ -1124,7 +1150,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, long)
 	 */
 	public void test_insertIJ() {
@@ -1166,7 +1192,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, Object)
 	 */
 	public void test_insertILjava_lang_Object() {
@@ -1208,7 +1234,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.insert(int, String)
 	 */
 	public void test_insertILjava_lang_String() {
@@ -1250,7 +1276,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.lastIndexOf(String)
 	 */
 	public void test_lastIndexOfLjava_lang_String() {
@@ -1269,7 +1295,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.lastIndexOf(String, int)
 	 */
 	public void test_lastIndexOfLjava_lang_StringI() {
@@ -1298,7 +1324,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.length()
 	 */
 	public void test_length() {
@@ -1308,14 +1334,36 @@
 		assertEquals(4, sb.length());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.offsetByCodePoints(int, int)'
 	 */
 	public void test_offsetByCodePointsII() {
-		// TODO Implement Java 5 code point functionality.
+        int result = new StringBuilder("a\uD800\uDC00b").offsetByCodePoints(0, 2);
+        assertEquals(3, result);
+
+        result = new StringBuilder("abcd").offsetByCodePoints(3, -1);
+        assertEquals(2, result);
+
+        result = new StringBuilder("a\uD800\uDC00b").offsetByCodePoints(0, 3);
+        assertEquals(4, result);
+
+        result = new StringBuilder("a\uD800\uDC00b").offsetByCodePoints(3, -1);
+        assertEquals(1, result);
+
+        result = new StringBuilder("a\uD800\uDC00b").offsetByCodePoints(3, 0);
+        assertEquals(3, result);
+
+        result = new StringBuilder("\uD800\uDC00bc").offsetByCodePoints(3, 0);
+        assertEquals(3, result);
+
+        result = new StringBuilder("a\uDC00bc").offsetByCodePoints(3, -1);
+        assertEquals(2, result);
+
+        result = new StringBuilder("a\uD800bc").offsetByCodePoints(3, -1);
+        assertEquals(2, result);
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.replace(int, int, String)'
 	 */
 	public void test_replaceIILjava_lang_String() {
@@ -1373,7 +1421,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.reverse()
 	 */
 	public void test_reverse() {
@@ -1395,7 +1443,7 @@
 		assertEquals("", sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.setCharAt(int, char)
 	 */
 	public void test_setCharAtIC() {
@@ -1432,7 +1480,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.setLength(int)'
 	 */
 	public void test_setLengthI() {
@@ -1456,7 +1504,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.subSequence(int, int)
 	 */
 	public void test_subSequenceII() {
@@ -1497,7 +1545,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.substring(int)
 	 */
 	public void test_substringI() {
@@ -1531,7 +1579,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.substring(int, int)
 	 */
 	public void test_substringII() {
@@ -1572,7 +1620,7 @@
 		}
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.toString()'
 	 */
 	public void test_toString() {
@@ -1581,7 +1629,7 @@
 		assertEquals(fixture, sb.toString());
 	}
 
-	/*
+	/**
 	 * @tests java.lang.StringBuilder.trimToSize()'
 	 */
 	public void test_trimToSize() {