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;
18 * A <code>CharSequence</code> based implementation of
19 * <code>CharacterIterator</code>.
23 public class SequenceCharacterIterator implements CharacterIterator {
25 private int fIndex = -1;
27 private final CharSequence fSequence;
29 private final int fFirst;
31 private final int fLast;
33 private void invariant() {
34 Assert.isTrue(fIndex >= fFirst);
35 Assert.isTrue(fIndex <= fLast);
39 * Creates an iterator for the entire sequence.
42 * the sequence backing this iterator
44 public SequenceCharacterIterator(CharSequence sequence) {
49 * Creates an iterator.
52 * the sequence backing this iterator
54 * the first character to consider
55 * @throws IllegalArgumentException
56 * if the indices are out of bounds
58 public SequenceCharacterIterator(CharSequence sequence, int first)
59 throws IllegalArgumentException {
60 this(sequence, first, sequence.length());
64 * Creates an iterator.
67 * the sequence backing this iterator
69 * the first character to consider
71 * the last character index to consider
72 * @throws IllegalArgumentException
73 * if the indices are out of bounds
75 public SequenceCharacterIterator(CharSequence sequence, int first, int last)
76 throws IllegalArgumentException {
78 throw new NullPointerException();
79 if (first < 0 || first > last)
80 throw new IllegalArgumentException();
81 if (last > sequence.length())
82 throw new IllegalArgumentException();
91 * @see java.text.CharacterIterator#first()
94 return setIndex(getBeginIndex());
98 * @see java.text.CharacterIterator#last()
102 return setIndex(getEndIndex());
104 return setIndex(getEndIndex() - 1);
108 * @see java.text.CharacterIterator#current()
110 public char current() {
111 if (fIndex >= fFirst && fIndex < fLast)
112 return fSequence.charAt(fIndex);
118 * @see java.text.CharacterIterator#next()
121 return setIndex(Math.min(fIndex + 1, getEndIndex()));
125 * @see java.text.CharacterIterator#previous()
127 public char previous() {
128 if (fIndex > getBeginIndex()) {
129 return setIndex(fIndex - 1);
136 * @see java.text.CharacterIterator#setIndex(int)
138 public char setIndex(int position) {
139 if (position >= getBeginIndex() && position <= getEndIndex())
142 throw new IllegalArgumentException();
149 * @see java.text.CharacterIterator#getBeginIndex()
151 public int getBeginIndex() {
156 * @see java.text.CharacterIterator#getEndIndex()
158 public int getEndIndex() {
163 * @see java.text.CharacterIterator#getIndex()
165 public int getIndex() {
170 * @see java.text.CharacterIterator#clone()
172 public Object clone() {
174 return super.clone();
175 } catch (CloneNotSupportedException e) {
176 throw new InternalError();