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-2010, International Business Machines Corporation and    *
      7  * others. All Rights Reserved.                                                *
      8  *******************************************************************************
      9  */
     10 package android.icu.impl;
     11 
     12 import java.text.CharacterIterator;
     13 
     14 import android.icu.text.UCharacterIterator;
     15 
     16 /**
     17  * This class is a wrapper around CharacterIterator and implements the
     18  * UCharacterIterator protocol
     19  * @author ram
     20  * @hide Only a subset of ICU is exposed in Android
     21  */
     22 
     23 public class CharacterIteratorWrapper extends UCharacterIterator {
     24 
     25     private CharacterIterator iterator;
     26 
     27 
     28     public CharacterIteratorWrapper(CharacterIterator iter){
     29         if(iter==null){
     30             throw new IllegalArgumentException();
     31         }
     32         iterator     = iter;
     33     }
     34 
     35     /**
     36      * @see UCharacterIterator#current()
     37      */
     38     @Override
     39     public int current() {
     40         int c = iterator.current();
     41         if(c==CharacterIterator.DONE){
     42           return DONE;
     43         }
     44         return c;
     45     }
     46 
     47     /**
     48      * @see UCharacterIterator#getLength()
     49      */
     50     @Override
     51     public int getLength() {
     52         return (iterator.getEndIndex() - iterator.getBeginIndex());
     53     }
     54 
     55     /**
     56      * @see UCharacterIterator#getIndex()
     57      */
     58     @Override
     59     public int getIndex() {
     60         return iterator.getIndex();
     61     }
     62 
     63     /**
     64      * @see UCharacterIterator#next()
     65      */
     66     @Override
     67     public int next() {
     68         int i = iterator.current();
     69         iterator.next();
     70         if(i==CharacterIterator.DONE){
     71           return DONE;
     72         }
     73         return i;
     74     }
     75 
     76     /**
     77      * @see UCharacterIterator#previous()
     78      */
     79     @Override
     80     public int previous() {
     81         int i = iterator.previous();
     82         if(i==CharacterIterator.DONE){
     83             return DONE;
     84         }
     85         return i;
     86     }
     87 
     88     /**
     89      * @see UCharacterIterator#setIndex(int)
     90      */
     91     @Override
     92     public void setIndex(int index) {
     93         try{
     94             iterator.setIndex(index);
     95         }catch(IllegalArgumentException e){
     96             throw new IndexOutOfBoundsException();
     97         }
     98     }
     99 
    100     /**
    101      * @see UCharacterIterator#setToLimit()
    102      */
    103     @Override
    104     public void setToLimit() {
    105         iterator.setIndex(iterator.getEndIndex());
    106     }
    107 
    108     /**
    109      * @see UCharacterIterator#getText(char[])
    110      */
    111     @Override
    112     public int getText(char[] fillIn, int offset){
    113         int length =iterator.getEndIndex() - iterator.getBeginIndex();
    114         int currentIndex = iterator.getIndex();
    115         if(offset < 0 || offset + length > fillIn.length){
    116             throw new IndexOutOfBoundsException(Integer.toString(length));
    117         }
    118 
    119         for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
    120             fillIn[offset++] = ch;
    121         }
    122         iterator.setIndex(currentIndex);
    123 
    124         return length;
    125     }
    126 
    127     /**
    128      * Creates a clone of this iterator.  Clones the underlying character iterator.
    129      * @see UCharacterIterator#clone()
    130      */
    131     @Override
    132     public Object clone(){
    133         try {
    134             CharacterIteratorWrapper result = (CharacterIteratorWrapper) super.clone();
    135             result.iterator = (CharacterIterator)this.iterator.clone();
    136             return result;
    137         } catch (CloneNotSupportedException e) {
    138             return null; // only invoked if bad underlying character iterator
    139         }
    140     }
    141 
    142 
    143     @Override
    144     public int moveIndex(int delta){
    145         int length = iterator.getEndIndex() - iterator.getBeginIndex();
    146         int idx = iterator.getIndex()+delta;
    147 
    148         if(idx < 0) {
    149             idx = 0;
    150         } else if(idx > length) {
    151             idx = length;
    152         }
    153         return iterator.setIndex(idx);
    154     }
    155 
    156     /**
    157      * @see UCharacterIterator#getCharacterIterator()
    158      */
    159     @Override
    160     public CharacterIterator getCharacterIterator(){
    161         return (CharacterIterator)iterator.clone();
    162     }
    163 }
    164