Home | History | Annotate | Download | only in impl
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 1996-2015, International Business Machines Corporation and
      7  * others. All Rights Reserved.
      8  *******************************************************************************
      9  */
     10 package android.icu.impl;
     11 
     12 import android.icu.text.Replaceable;
     13 import android.icu.text.ReplaceableString;
     14 import android.icu.text.UCharacterIterator;
     15 import android.icu.text.UTF16;
     16 
     17 /**
     18  * DLF docs must define behavior when Replaceable is mutated underneath
     19  * the iterator.
     20  *
     21  * This and ICUCharacterIterator share some code, maybe they should share
     22  * an implementation, or the common state and implementation should be
     23  * moved up into UCharacterIterator.
     24  *
     25  * What are first, last, and getBeginIndex doing here?!?!?!
     26  * @hide Only a subset of ICU is exposed in Android
     27  */
     28 public class ReplaceableUCharacterIterator extends UCharacterIterator {
     29 
     30     // public constructor ------------------------------------------------------
     31 
     32     /**
     33      * Public constructor
     34      * @param replaceable text which the iterator will be based on
     35      */
     36     public ReplaceableUCharacterIterator(Replaceable replaceable){
     37         if(replaceable==null){
     38             throw new IllegalArgumentException();
     39         }
     40         this.replaceable  = replaceable;
     41         this.currentIndex = 0;
     42     }
     43 
     44     /**
     45      * Public constructor
     46      * @param str text which the iterator will be based on
     47      */
     48     public ReplaceableUCharacterIterator(String str){
     49         if(str==null){
     50             throw new IllegalArgumentException();
     51         }
     52         this.replaceable  = new ReplaceableString(str);
     53         this.currentIndex = 0;
     54     }
     55 
     56     /**
     57      * Public constructor
     58      * @param buf buffer of text on which the iterator will be based
     59      */
     60     public ReplaceableUCharacterIterator(StringBuffer buf){
     61         if(buf==null){
     62             throw new IllegalArgumentException();
     63         }
     64         this.replaceable  = new ReplaceableString(buf);
     65         this.currentIndex = 0;
     66     }
     67 
     68     // public methods ----------------------------------------------------------
     69 
     70     /**
     71      * Creates a copy of this iterator, does not clone the underlying
     72      * <code>Replaceable</code>object
     73      * @return copy of this iterator
     74      */
     75     @Override
     76     public Object clone(){
     77         try {
     78           return super.clone();
     79         } catch (CloneNotSupportedException e) {
     80             return null; // never invoked
     81         }
     82     }
     83 
     84     /**
     85      * Returns the current UTF16 character.
     86      * @return current UTF16 character
     87      */
     88     @Override
     89     public int current(){
     90         if (currentIndex < replaceable.length()) {
     91             return replaceable.charAt(currentIndex);
     92         }
     93         return DONE;
     94     }
     95 
     96     /**
     97      * Returns the current codepoint
     98      * @return current codepoint
     99      */
    100     @Override
    101     public int currentCodePoint(){
    102         // cannot use charAt due to it different
    103         // behaviour when index is pointing at a
    104         // trail surrogate, check for surrogates
    105 
    106         int ch = current();
    107         if(UTF16.isLeadSurrogate((char)ch)){
    108             // advance the index to get the next code point
    109             next();
    110             // due to post increment semantics current() after next()
    111             // actually returns the next char which is what we want
    112             int ch2 = current();
    113             // current should never change the current index so back off
    114             previous();
    115 
    116             if(UTF16.isTrailSurrogate((char)ch2)){
    117                 // we found a surrogate pair
    118                 return Character.toCodePoint((char)ch, (char)ch2);
    119             }
    120         }
    121         return ch;
    122     }
    123 
    124     /**
    125      * Returns the length of the text
    126      * @return length of the text
    127      */
    128     @Override
    129     public int getLength(){
    130         return replaceable.length();
    131     }
    132 
    133     /**
    134      * Gets the current currentIndex in text.
    135      * @return current currentIndex in text.
    136      */
    137     @Override
    138     public int getIndex(){
    139         return currentIndex;
    140     }
    141 
    142     /**
    143      * Returns next UTF16 character and increments the iterator's currentIndex by 1.
    144      * If the resulting currentIndex is greater or equal to the text length, the
    145      * currentIndex is reset to the text length and a value of DONECODEPOINT is
    146      * returned.
    147      * @return next UTF16 character in text or DONE if the new currentIndex is off the
    148      *         end of the text range.
    149      */
    150     @Override
    151     public int next(){
    152         if (currentIndex < replaceable.length()) {
    153             return replaceable.charAt(currentIndex++);
    154         }
    155         return DONE;
    156     }
    157 
    158 
    159     /**
    160      * Returns previous UTF16 character and decrements the iterator's currentIndex by
    161      * 1.
    162      * If the resulting currentIndex is less than 0, the currentIndex is reset to 0 and a
    163      * value of DONECODEPOINT is returned.
    164      * @return next UTF16 character in text or DONE if the new currentIndex is off the
    165      *         start of the text range.
    166      */
    167     @Override
    168     public int previous(){
    169         if (currentIndex > 0) {
    170             return replaceable.charAt(--currentIndex);
    171         }
    172         return DONE;
    173     }
    174 
    175     /**
    176      * <p>Sets the currentIndex to the specified currentIndex in the text and returns that
    177      * single UTF16 character at currentIndex.
    178      * This assumes the text is stored as 16-bit code units.</p>
    179      * @param currentIndex the currentIndex within the text.
    180      * @exception IllegalArgumentException is thrown if an invalid currentIndex is
    181      *            supplied. i.e. currentIndex is out of bounds.
    182      * @returns the character at the specified currentIndex or DONE if the specified
    183      *         currentIndex is equal to the end of the text.
    184      */
    185     @Override
    186     public void setIndex(int currentIndex) throws IndexOutOfBoundsException{
    187         if (currentIndex < 0 || currentIndex > replaceable.length()) {
    188             throw new IndexOutOfBoundsException();
    189         }
    190         this.currentIndex = currentIndex;
    191     }
    192 
    193     @Override
    194     public int getText(char[] fillIn, int offset){
    195         int length = replaceable.length();
    196         if(offset < 0 || offset + length > fillIn.length){
    197             throw new IndexOutOfBoundsException(Integer.toString(length));
    198         }
    199         replaceable.getChars(0,length,fillIn,offset);
    200         return length;
    201     }
    202 
    203     // private data members ----------------------------------------------------
    204 
    205     /**
    206      * Replacable object
    207      */
    208     private Replaceable replaceable;
    209     /**
    210      * Current currentIndex
    211      */
    212     private int currentIndex;
    213 
    214 }
    215