| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
|
| 21 |
import java.text.CharacterIterator; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class CharSequenceCharacterIterator implements CharacterIterator { |
| 28 |
private final int mBeginIndex, mEndIndex; |
| 29 |
private int mIndex; |
| 30 |
private final CharSequence mCharSeq; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
} |