• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.text;
18 
19 import android.annotation.NonNull;
20 
21 import java.text.CharacterIterator;
22 
23 /**
24  * An implementation of {@link java.text.CharacterIterator} that iterates over a given CharSequence.
25  * {@hide}
26  */
27 @android.ravenwood.annotation.RavenwoodKeepWholeClass
28 public class CharSequenceCharacterIterator implements CharacterIterator {
29     private final int mBeginIndex, mEndIndex;
30     private int mIndex;
31     private final CharSequence mCharSeq;
32 
33     /**
34      * Constructs the iterator given a CharSequence and a range. The position of the iterator index
35      * is set to the beginning of the range.
36      */
CharSequenceCharacterIterator(@onNull CharSequence text, int start, int end)37     public CharSequenceCharacterIterator(@NonNull CharSequence text, int start, int end) {
38         mCharSeq = text;
39         mBeginIndex = mIndex = start;
40         mEndIndex = end;
41     }
42 
first()43     public char first() {
44         mIndex = mBeginIndex;
45         return current();
46     }
47 
last()48     public char last() {
49         if (mBeginIndex == mEndIndex) {
50             mIndex = mEndIndex;
51             return DONE;
52         } else {
53             mIndex = mEndIndex - 1;
54             return mCharSeq.charAt(mIndex);
55         }
56     }
57 
current()58     public char current() {
59         return (mIndex == mEndIndex) ? DONE : mCharSeq.charAt(mIndex);
60     }
61 
next()62     public char next() {
63         mIndex++;
64         if (mIndex >= mEndIndex) {
65             mIndex = mEndIndex;
66             return DONE;
67         } else {
68             return mCharSeq.charAt(mIndex);
69         }
70     }
71 
previous()72     public char previous() {
73         if (mIndex <= mBeginIndex) {
74             return DONE;
75         } else {
76             mIndex--;
77             return mCharSeq.charAt(mIndex);
78         }
79     }
80 
setIndex(int position)81     public char setIndex(int position) {
82         if (mBeginIndex <= position && position <= mEndIndex) {
83             mIndex = position;
84             return current();
85         } else {
86             throw new IllegalArgumentException("invalid position");
87         }
88     }
89 
getBeginIndex()90     public int getBeginIndex() {
91         return mBeginIndex;
92     }
93 
getEndIndex()94     public int getEndIndex() {
95         return mEndIndex;
96     }
97 
getIndex()98     public int getIndex() {
99         return mIndex;
100     }
101 
clone()102     public Object clone() {
103         try {
104             return super.clone();
105         } catch (CloneNotSupportedException e) {
106             throw new InternalError();
107         }
108     }
109 }
110