1 package org.simantics.scl.ui.editor2.iterator;
3 import java.text.CharacterIterator;
5 import org.eclipse.core.runtime.Assert;
10 * A <code>CharSequence</code> based implementation of <code>CharacterIterator</code>.
14 public class SequenceCharacterIterator implements CharacterIterator {
16 private int fIndex= -1;
17 private final CharSequence fSequence;
18 private final int fFirst;
19 private final int fLast;
21 private void invariant() {
22 Assert.isTrue(fIndex >= fFirst);
23 Assert.isTrue(fIndex <= fLast);
27 * Creates an iterator for the entire sequence.
29 * @param sequence the sequence backing this iterator
31 public SequenceCharacterIterator(CharSequence sequence) {
36 * Creates an iterator.
38 * @param sequence the sequence backing this iterator
39 * @param first the first character to consider
40 * @throws IllegalArgumentException if the indices are out of bounds
42 public SequenceCharacterIterator(CharSequence sequence, int first) throws IllegalArgumentException {
43 this(sequence, first, sequence.length());
47 * Creates an iterator.
49 * @param sequence the sequence backing this iterator
50 * @param first the first character to consider
51 * @param last the last character index to consider
52 * @throws IllegalArgumentException if the indices are out of bounds
54 public SequenceCharacterIterator(CharSequence sequence, int first, int last) throws IllegalArgumentException {
56 throw new NullPointerException();
57 if (first < 0 || first > last)
58 throw new IllegalArgumentException();
59 if (last > sequence.length())
60 throw new IllegalArgumentException();
69 * @see java.text.CharacterIterator#first()
72 return setIndex(getBeginIndex());
76 * @see java.text.CharacterIterator#last()
80 return setIndex(getEndIndex());
82 return setIndex(getEndIndex() - 1);
86 * @see java.text.CharacterIterator#current()
88 public char current() {
89 if (fIndex >= fFirst && fIndex < fLast)
90 return fSequence.charAt(fIndex);
96 * @see java.text.CharacterIterator#next()
99 return setIndex(Math.min(fIndex + 1, getEndIndex()));
103 * @see java.text.CharacterIterator#previous()
105 public char previous() {
106 if (fIndex > getBeginIndex()) {
107 return setIndex(fIndex - 1);
114 * @see java.text.CharacterIterator#setIndex(int)
116 public char setIndex(int position) {
117 if (position >= getBeginIndex() && position <= getEndIndex())
120 throw new IllegalArgumentException();
127 * @see java.text.CharacterIterator#getBeginIndex()
129 public int getBeginIndex() {
134 * @see java.text.CharacterIterator#getEndIndex()
136 public int getEndIndex() {
141 * @see java.text.CharacterIterator#getIndex()
143 public int getIndex() {
148 * @see java.text.CharacterIterator#clone()
151 public Object clone() {
153 return super.clone();
154 } catch (CloneNotSupportedException e) {
155 throw new InternalError();