1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
13 import java.text.CharacterIterator;
15 import org.eclipse.jface.text.Assert;
19 * A <code>CharSequence</code> based implementation of <code>CharacterIterator</code>.
23 public class SequenceCharacterIterator implements CharacterIterator {
25 private int fIndex= -1;
26 private final CharSequence fSequence;
27 private final int fFirst;
28 private final int fLast;
30 private void invariant() {
31 Assert.isTrue(fIndex >= fFirst);
32 Assert.isTrue(fIndex <= fLast);
36 * Creates an iterator for the entire sequence.
38 * @param sequence the sequence backing this iterator
40 public SequenceCharacterIterator(CharSequence sequence) {
45 * Creates an iterator.
47 * @param sequence the sequence backing this iterator
48 * @param first the first character to consider
49 * @throws IllegalArgumentException if the indices are out of bounds
51 public SequenceCharacterIterator(CharSequence sequence, int first) throws IllegalArgumentException {
52 this(sequence, first, sequence.length());
56 * Creates an iterator.
58 * @param sequence the sequence backing this iterator
59 * @param first the first character to consider
60 * @param last the last character index to consider
61 * @throws IllegalArgumentException if the indices are out of bounds
63 public SequenceCharacterIterator(CharSequence sequence, int first, int last) throws IllegalArgumentException {
65 throw new NullPointerException();
66 if (first < 0 || first > last)
67 throw new IllegalArgumentException();
68 if (last > sequence.length())
69 throw new IllegalArgumentException();
78 * @see java.text.CharacterIterator#first()
81 return setIndex(getBeginIndex());
85 * @see java.text.CharacterIterator#last()
89 return setIndex(getEndIndex());
91 return setIndex(getEndIndex() - 1);
95 * @see java.text.CharacterIterator#current()
97 public char current() {
98 if (fIndex >= fFirst && fIndex < fLast)
99 return fSequence.charAt(fIndex);
105 * @see java.text.CharacterIterator#next()
108 return setIndex(Math.min(fIndex + 1, getEndIndex()));
112 * @see java.text.CharacterIterator#previous()
114 public char previous() {
115 if (fIndex > getBeginIndex()) {
116 return setIndex(fIndex - 1);
123 * @see java.text.CharacterIterator#setIndex(int)
125 public char setIndex(int position) {
126 if (position >= getBeginIndex() && position <= getEndIndex())
129 throw new IllegalArgumentException();
136 * @see java.text.CharacterIterator#getBeginIndex()
138 public int getBeginIndex() {
143 * @see java.text.CharacterIterator#getEndIndex()
145 public int getEndIndex() {
150 * @see java.text.CharacterIterator#getIndex()
152 public int getIndex() {
157 * @see java.text.CharacterIterator#clone()
159 public Object clone() {
161 return super.clone();
162 } catch (CloneNotSupportedException e) {
163 throw new InternalError();