X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.scl.ui%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fui%2Feditor2%2Fiterator%2FSequenceCharacterIterator.java;fp=bundles%2Forg.simantics.scl.ui%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fui%2Feditor2%2Fiterator%2FSequenceCharacterIterator.java;h=96520a31561865a7d9ca7f6f4264bee464eafcc4;hp=0000000000000000000000000000000000000000;hb=3286205a29ade556a0ac3e19c68ac6ebb3c8a62d;hpb=e81aaf01a022b7f9fd54924f843a8f8594682126 diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/iterator/SequenceCharacterIterator.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/iterator/SequenceCharacterIterator.java new file mode 100644 index 000000000..96520a315 --- /dev/null +++ b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor2/iterator/SequenceCharacterIterator.java @@ -0,0 +1,158 @@ +package org.simantics.scl.ui.editor2.iterator; + +import java.text.CharacterIterator; + +import org.eclipse.core.runtime.Assert; + + + +/** + * A CharSequence based implementation of CharacterIterator. + * + * @since 3.0 + */ +public class SequenceCharacterIterator implements CharacterIterator { + + private int fIndex= -1; + private final CharSequence fSequence; + private final int fFirst; + private final int fLast; + + private void invariant() { + Assert.isTrue(fIndex >= fFirst); + Assert.isTrue(fIndex <= fLast); + } + + /** + * Creates an iterator for the entire sequence. + * + * @param sequence the sequence backing this iterator + */ + public SequenceCharacterIterator(CharSequence sequence) { + this(sequence, 0); + } + + /** + * Creates an iterator. + * + * @param sequence the sequence backing this iterator + * @param first the first character to consider + * @throws IllegalArgumentException if the indices are out of bounds + */ + public SequenceCharacterIterator(CharSequence sequence, int first) throws IllegalArgumentException { + this(sequence, first, sequence.length()); + } + + /** + * Creates an iterator. + * + * @param sequence the sequence backing this iterator + * @param first the first character to consider + * @param last the last character index to consider + * @throws IllegalArgumentException if the indices are out of bounds + */ + public SequenceCharacterIterator(CharSequence sequence, int first, int last) throws IllegalArgumentException { + if (sequence == null) + throw new NullPointerException(); + if (first < 0 || first > last) + throw new IllegalArgumentException(); + if (last > sequence.length()) + throw new IllegalArgumentException(); + fSequence= sequence; + fFirst= first; + fLast= last; + fIndex= first; + invariant(); + } + + /* + * @see java.text.CharacterIterator#first() + */ + public char first() { + return setIndex(getBeginIndex()); + } + + /* + * @see java.text.CharacterIterator#last() + */ + public char last() { + if (fFirst == fLast) + return setIndex(getEndIndex()); + else + return setIndex(getEndIndex() - 1); + } + + /* + * @see java.text.CharacterIterator#current() + */ + public char current() { + if (fIndex >= fFirst && fIndex < fLast) + return fSequence.charAt(fIndex); + else + return DONE; + } + + /* + * @see java.text.CharacterIterator#next() + */ + public char next() { + return setIndex(Math.min(fIndex + 1, getEndIndex())); + } + + /* + * @see java.text.CharacterIterator#previous() + */ + public char previous() { + if (fIndex > getBeginIndex()) { + return setIndex(fIndex - 1); + } else { + return DONE; + } + } + + /* + * @see java.text.CharacterIterator#setIndex(int) + */ + public char setIndex(int position) { + if (position >= getBeginIndex() && position <= getEndIndex()) + fIndex= position; + else + throw new IllegalArgumentException(); + + invariant(); + return current(); + } + + /* + * @see java.text.CharacterIterator#getBeginIndex() + */ + public int getBeginIndex() { + return fFirst; + } + + /* + * @see java.text.CharacterIterator#getEndIndex() + */ + public int getEndIndex() { + return fLast; + } + + /* + * @see java.text.CharacterIterator#getIndex() + */ + public int getIndex() { + return fIndex; + } + + /* + * @see java.text.CharacterIterator#clone() + */ + @Override + public Object clone() { + try { + return super.clone(); + } catch (CloneNotSupportedException e) { + throw new InternalError(); + } + } +}