harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r389476 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/lang/ test/java/org/apache/harmony/tests/java/lang/
Date Tue, 28 Mar 2006 11:09:15 GMT
Author: tellison
Date: Tue Mar 28 03:09:11 2006
New Revision: 389476

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

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuffer.java
    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/StringBufferTest.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/StringBuffer.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuffer.java?rev=389476&r1=389475&r2=389476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuffer.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/StringBuffer.java
Tue Mar 28 03:09:11 2006
@@ -15,7 +15,6 @@
 
 package java.lang;
 
-
 import java.io.IOException;
 import java.io.InvalidObjectException;
 import java.io.ObjectInputStream;
@@ -23,8 +22,6 @@
 import java.io.Serializable;
 import java.util.Arrays;
 
-import org.apache.harmony.luni.util.NotYetImplementedException;
-
 /**
  * StringBuffer is a variable size contiguous indexable array of characters. The
  * length of the StringBuffer is the number of characters it contains. The
@@ -1128,59 +1125,84 @@
 
     /**
      * <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)
      * @since 1.5
      */
-    public int codePointAt(int index) {
-        // TODO Implement Java 5 code point functionality.
-        //Note: synchronization is handled by 'charAt' method
-        return charAt(index);
+    public synchronized int codePointAt(int index) {
+        if (index < 0 || index >= count)
+            throw new IndexOutOfBoundsException();
+        return Character.codePointAt(value, index, count);
     }
 
     /**
      * <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)
      * @since 1.5
      */
-    public int codePointBefore(int index) {
-        // TODO Implement Java 5 code point functionality.
-        //Note: synchronization is handled by 'codePointAt' method
-        return codePointAt(index - 1);
+    public synchronized int codePointBefore(int index) {
+        if (index < 1 || index > count)
+            throw new IndexOutOfBoundsException();
+        return Character.codePointBefore(value, 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()}.
      * @since 1.5
      */
     public synchronized int codePointCount(int beginIndex, int endIndex) {
-        // TODO Implement Java 5 code point functionality.
         if (beginIndex < 0 || endIndex > count || beginIndex > endIndex)
             throw new IndexOutOfBoundsException();
-        return endIndex - beginIndex;
+        return Character.codePointCount(value, beginIndex, endIndex
+                - beginIndex);
     }
 
     /**
      * <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>.
      * @since 1.5
      */
     public synchronized int offsetByCodePoints(int index, int codePointOffset) {
-        // TODO Implement Java 5 code point functionality.
-        throw new NotYetImplementedException();
+        return Character.offsetByCodePoints(value, 0, count, index,
+                codePointOffset);
     }
 
     /**
@@ -1221,17 +1243,18 @@
 
     /**
      * <p>
-     * <b>NOTE - This method is currently NOT completely implemented and just
-     * casts the <code>codePoint</code> to a <code>char</code> and
appends it.</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>
-     * @param codePoint
-     * @return
+     * 
+     * @param codePoint The Unicode code point to encode and append.
+     * @return A reference to this object.
+     * @see Character#toChars(int)
      * @since 1.5
      */
     public StringBuffer appendCodePoint(int codePoint) {
-        // TODO Implement Java 5 code point functionality.
-        append((char) codePoint);
-        return this;
+        return append(Character.toChars(codePoint));
     }
 
     /**

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=389476&r1=389475&r2=389476&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
Tue Mar 28 03:09:11 2006
@@ -409,6 +409,8 @@
      * @see Character#codePointAt(char[], int, int)
      */
     public int codePointAt(int index) {
+        if (index < 0 || index >= length)
+            throw new IndexOutOfBoundsException();
         return Character.codePointAt(buffer, index, length);
     }
 
@@ -427,6 +429,8 @@
      * @see Character#codePointBefore(char[], int, int)
      */
     public int codePointBefore(int index) {
+        if (index < 1 || index > length)
+            throw new IndexOutOfBoundsException();
         return Character.codePointBefore(buffer, index);
     }
 

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBufferTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBufferTest.java?rev=389476&r1=389475&r2=389476&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBufferTest.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/StringBufferTest.java
Tue Mar 28 03:09:11 2006
@@ -214,4 +214,187 @@
             // Expected
         }
     }
+    
+    /**
+     * @tests java.lang.StringBuffer.appendCodePoint(int)'
+     */
+    public void test_appendCodePointI() {
+        StringBuffer sb = new StringBuffer();
+        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.StringBuffer.codePointAt(int)
+     */
+    public void test_codePointAtI() {
+        StringBuffer sb = new StringBuffer("abc");
+        assertEquals('a', sb.codePointAt(0));
+        assertEquals('b', sb.codePointAt(1));
+        assertEquals('c', sb.codePointAt(2));
+        
+        sb = new StringBuffer("\uD800\uDC00");
+        assertEquals(0x10000, sb.codePointAt(0));
+        assertEquals('\uDC00', sb.codePointAt(1));
+        
+        try {
+            sb.codePointAt(-1);
+            fail("No IOOBE on negative index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointAt(sb.length());
+            fail("No IOOBE on index equal to length.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointAt(sb.length() + 1);
+            fail("No IOOBE on index greater than length.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+    }
+
+    /**
+     * @tests java.lang.StringBuffer.codePointBefore(int)
+     */
+    public void test_codePointBeforeI() {
+        StringBuffer sb = new StringBuffer("abc");
+        assertEquals('a', sb.codePointBefore(1));
+        assertEquals('b', sb.codePointBefore(2));
+        assertEquals('c', sb.codePointBefore(3));
+        
+        sb = new StringBuffer("\uD800\uDC00");
+        assertEquals(0x10000, sb.codePointBefore(2));
+        assertEquals('\uD800', sb.codePointBefore(1));
+        
+        try {
+            sb.codePointBefore(0);
+            fail("No IOOBE on zero index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointBefore(-1);
+            fail("No IOOBE on negative index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointBefore(sb.length() + 1);
+            fail("No IOOBE on index greater than length.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+    }
+
+    /**
+     * @tests java.lang.StringBuffer.codePointCount(int, int)
+     */
+    public void test_codePointCountII() {
+        assertEquals(1, new StringBuffer("\uD800\uDC00").codePointCount(0, 2));
+        assertEquals(1, new StringBuffer("\uD800\uDC01").codePointCount(0, 2));
+        assertEquals(1, new StringBuffer("\uD801\uDC01").codePointCount(0, 2));
+        assertEquals(1, new StringBuffer("\uDBFF\uDFFF").codePointCount(0, 2));
+
+        assertEquals(3, new StringBuffer("a\uD800\uDC00b").codePointCount(0, 4));
+        assertEquals(4, new StringBuffer("a\uD800\uDC00b\uD800").codePointCount(0, 5));
+        
+        StringBuffer sb = new StringBuffer("abc");
+        try {
+            sb.codePointCount(-1, 2);
+            fail("No IOOBE for negative begin index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointCount(0, 4);
+            fail("No IOOBE for end index that's too large.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointCount(3, 2);
+            fail("No IOOBE for begin index larger than end index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+    }
+    
+    /**
+     * @tests java.lang.StringBuffer.offsetByCodePoints(int, int)'
+     */
+    public void test_offsetByCodePointsII() {
+        int result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(0, 2);
+        assertEquals(3, result);
+
+        result = new StringBuffer("abcd").offsetByCodePoints(3, -1);
+        assertEquals(2, result);
+
+        result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(0, 3);
+        assertEquals(4, result);
+
+        result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(3, -1);
+        assertEquals(1, result);
+
+        result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(3, 0);
+        assertEquals(3, result);
+
+        result = new StringBuffer("\uD800\uDC00bc").offsetByCodePoints(3, 0);
+        assertEquals(3, result);
+
+        result = new StringBuffer("a\uDC00bc").offsetByCodePoints(3, -1);
+        assertEquals(2, result);
+
+        result = new StringBuffer("a\uD800bc").offsetByCodePoints(3, -1);
+        assertEquals(2, result);
+        
+        StringBuffer sb = new StringBuffer("abc");
+        try {
+            sb.offsetByCodePoints(-1, 1);
+            fail("No IOOBE for negative index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.offsetByCodePoints(0, 4);
+            fail("No IOOBE for offset that's too large.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.offsetByCodePoints(3, -4);
+            fail("No IOOBE for offset that's too small.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.offsetByCodePoints(3, 1);
+            fail("No IOOBE for index that's too large.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.offsetByCodePoints(4, -1);
+            fail("No IOOBE for index that's too large.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+    }
 }

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=389476&r1=389475&r2=389476&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
Tue Mar 28 03:09:11 2006
@@ -425,6 +425,29 @@
         sb = new StringBuilder("\uD800\uDC00");
         assertEquals(0x10000, sb.codePointAt(0));
         assertEquals('\uDC00', sb.codePointAt(1));
+        
+        sb = new StringBuilder();
+        sb.append("abc");
+        try {
+            sb.codePointAt(-1);
+            fail("No IOOBE on negative index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointAt(sb.length());
+            fail("No IOOBE on index equal to length.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointAt(sb.length() + 1);
+            fail("No IOOBE on index greater than length.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
 	}
 
 	/**
@@ -439,6 +462,30 @@
         sb = new StringBuilder("\uD800\uDC00");
         assertEquals(0x10000, sb.codePointBefore(2));
         assertEquals('\uD800', sb.codePointBefore(1));
+        
+        sb = new StringBuilder();
+        sb.append("abc");
+        
+        try {
+            sb.codePointBefore(0);
+            fail("No IOOBE on zero index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointBefore(-1);
+            fail("No IOOBE on negative index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointBefore(sb.length() + 1);
+            fail("No IOOBE on index greater than length.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
 	}
 
 	/**
@@ -452,6 +499,29 @@
 
         assertEquals(3, new StringBuilder("a\uD800\uDC00b").codePointCount(0, 4));
         assertEquals(4, new StringBuilder("a\uD800\uDC00b\uD800").codePointCount(0, 5));
+        
+        StringBuilder sb = new StringBuilder();
+        sb.append("abc");
+        try {
+            sb.codePointCount(-1, 2);
+            fail("No IOOBE for negative begin index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointCount(0, 4);
+            fail("No IOOBE for end index that's too large.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.codePointCount(3, 2);
+            fail("No IOOBE for begin index larger than end index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
 	}
 
 	/**
@@ -1361,6 +1431,43 @@
 
         result = new StringBuilder("a\uD800bc").offsetByCodePoints(3, -1);
         assertEquals(2, result);
+        
+        StringBuilder sb = new StringBuilder();
+        sb.append("abc");
+        try {
+            sb.offsetByCodePoints(-1, 1);
+            fail("No IOOBE for negative index.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.offsetByCodePoints(0, 4);
+            fail("No IOOBE for offset that's too large.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.offsetByCodePoints(3, -4);
+            fail("No IOOBE for offset that's too small.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.offsetByCodePoints(3, 1);
+            fail("No IOOBE for index that's too large.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
+        
+        try {
+            sb.offsetByCodePoints(4, -1);
+            fail("No IOOBE for index that's too large.");
+        } catch (IndexOutOfBoundsException e) {
+            
+        }
 	}
 
 	/**



Mime
View raw message