1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
19import android.annotation.NonNull;
20
21import java.text.CharacterIterator;
22
23/**
24 * An implementation of {@link java.text.CharacterIterator} that iterates over a given CharSequence.
25 * {@hide}
26 */
27public class CharSequenceCharacterIterator implements CharacterIterator {
28    private final int mBeginIndex, mEndIndex;
29    private int mIndex;
30    private final CharSequence mCharSeq;
31
32    /**
33     * Constructs the iterator given a CharSequence and a range. The position of the iterator index
34     * is set to the beginning of the range.
35     */
36    public CharSequenceCharacterIterator(@NonNull CharSequence text, int start, int end) {
37        mCharSeq = text;
38        mBeginIndex = mIndex = start;
39        mEndIndex = end;
40    }
41
42    public char first() {
43        mIndex = mBeginIndex;
44        return current();
45    }
46
47    public char last() {
48        if (mBeginIndex == mEndIndex) {
49            mIndex = mEndIndex;
50            return DONE;
51        } else {
52            mIndex = mEndIndex - 1;
53            return mCharSeq.charAt(mIndex);
54        }
55    }
56
57    public char current() {
58        return (mIndex == mEndIndex) ? DONE : mCharSeq.charAt(mIndex);
59    }
60
61    public char next() {
62        mIndex++;
63        if (mIndex >= mEndIndex) {
64            mIndex = mEndIndex;
65            return DONE;
66        } else {
67            return mCharSeq.charAt(mIndex);
68        }
69    }
70
71    public char previous() {
72        if (mIndex <= mBeginIndex) {
73            return DONE;
74        } else {
75            mIndex--;
76            return mCharSeq.charAt(mIndex);
77        }
78    }
79
80    public char setIndex(int position) {
81        if (mBeginIndex <= position && position <= mEndIndex) {
82            mIndex = position;
83            return current();
84        } else {
85            throw new IllegalArgumentException("invalid position");
86        }
87    }
88
89    public int getBeginIndex() {
90        return mBeginIndex;
91    }
92
93    public int getEndIndex() {
94        return mEndIndex;
95    }
96
97    public int getIndex() {
98        return mIndex;
99    }
100
101    public Object clone() {
102        try {
103            return super.clone();
104        } catch (CloneNotSupportedException e) {
105            throw new InternalError();
106        }
107    }
108}
109