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