Home | History | Annotate | Download | only in impl
      1 //  2017 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 package com.ibm.icu.impl;
      4 
      5 import java.text.CharacterIterator;
      6 
      7 /**
      8  * Implement the Java CharacterIterator interface on a CharSequence.
      9  * Intended for internal use by ICU only.
     10  */
     11 public class CSCharacterIterator implements CharacterIterator {
     12 
     13     private int index;
     14     private CharSequence seq;
     15 
     16 
     17     /**
     18      * Constructor.
     19      * @param text The CharSequence to iterate over.
     20      */
     21     public CSCharacterIterator(CharSequence text) {
     22         if (text == null) {
     23             throw new NullPointerException();
     24         }
     25         seq = text;
     26         index = 0;
     27     }
     28 
     29     /** @{inheritDoc} */
     30     @Override
     31     public char first() {
     32         index = 0;
     33         return current();
     34     }
     35 
     36     /** @{inheritDoc} */
     37     @Override
     38     public char last() {
     39         index = seq.length();
     40         return previous();
     41     }
     42 
     43     /** @{inheritDoc} */
     44     @Override
     45     public char current() {
     46         if (index == seq.length()) {
     47             return DONE;
     48         }
     49         return seq.charAt(index);
     50     }
     51 
     52     /** @{inheritDoc} */
     53     @Override
     54     public char next() {
     55         if (index < seq.length()) {
     56             ++index;
     57         }
     58         return current();
     59     }
     60 
     61     /** @{inheritDoc} */
     62     @Override
     63     public char previous() {
     64         if (index == 0) {
     65             return DONE;
     66         }
     67         --index;
     68         return current();
     69     }
     70 
     71     /** @{inheritDoc} */
     72     @Override
     73     public char setIndex(int position) {
     74         if (position < 0 || position > seq.length()) {
     75             throw new IllegalArgumentException();
     76         }
     77         index = position;
     78         return current();
     79     }
     80 
     81     /** @{inheritDoc} */
     82     @Override
     83     public int getBeginIndex() {
     84         return 0;
     85     }
     86 
     87     /** @{inheritDoc} */
     88     @Override
     89     public int getEndIndex() {
     90         return seq.length();
     91     }
     92 
     93     /** @{inheritDoc} */
     94     @Override
     95     public int getIndex() {
     96         return index;
     97     }
     98 
     99     /** @{inheritDoc} */
    100     @Override
    101     public Object clone() {
    102         CSCharacterIterator copy = new CSCharacterIterator(seq);
    103         copy.setIndex(index);
    104         return copy;
    105     }
    106 }
    107