Home | History | Annotate | Download | only in impl
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /**
      4 *******************************************************************************
      5 * Copyright (C) 1996-2010, International Business Machines Corporation and    *
      6 * others. All Rights Reserved.                                                *
      7 *******************************************************************************
      8 */
      9 
     10 package com.ibm.icu.impl;
     11 
     12 import com.ibm.icu.text.UCharacterIterator;
     13 
     14 
     15 /**
     16  * @author Doug Felt
     17  *
     18  */
     19 
     20 public final class UCharArrayIterator extends UCharacterIterator {
     21     private final char[] text;
     22     private final int start;
     23     private final int limit;
     24     private int pos;
     25 
     26     public UCharArrayIterator(char[] text, int start, int limit) {
     27         if (start < 0 || limit > text.length || start > limit) {
     28             throw new IllegalArgumentException("start: " + start + " or limit: "
     29                                                + limit + " out of range [0, "
     30                                                + text.length + ")");
     31         }
     32         this.text = text;
     33         this.start = start;
     34         this.limit = limit;
     35 
     36         this.pos = start;
     37     }
     38 
     39     @Override
     40     public int current() {
     41         return pos < limit ? text[pos] : DONE;
     42     }
     43 
     44     @Override
     45     public int getLength() {
     46         return limit - start;
     47     }
     48 
     49     @Override
     50     public int getIndex() {
     51         return pos - start;
     52     }
     53 
     54     @Override
     55     public int next() {
     56         return pos < limit ? text[pos++] : DONE;
     57     }
     58 
     59     @Override
     60     public int previous() {
     61         return pos > start ? text[--pos] : DONE;
     62     }
     63 
     64     @Override
     65     public void setIndex(int index) {
     66         if (index < 0 || index > limit - start) {
     67             throw new IndexOutOfBoundsException("index: " + index +
     68                                                 " out of range [0, "
     69                                                 + (limit - start) + ")");
     70         }
     71         pos = start + index;
     72     }
     73 
     74     @Override
     75     public int getText(char[] fillIn, int offset) {
     76         int len = limit - start;
     77         System.arraycopy(text, start, fillIn, offset, len);
     78         return len;
     79     }
     80 
     81     /**
     82      * Creates a copy of this iterator, does not clone the underlying
     83      * <code>Replaceable</code>object
     84      * @return copy of this iterator
     85      */
     86     @Override
     87     public Object clone(){
     88         try {
     89           return super.clone();
     90         } catch (CloneNotSupportedException e) {
     91             return null; // never invoked
     92         }
     93     }
     94 }