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;
16 //import org.eclipse.jface.text.Assert;
17 import org.eclipse.core.runtime.Assert;
20 * A <code>CharSequence</code> based implementation of
21 * <code>CharacterIterator</code>.
25 public class SequenceCharacterIterator implements CharacterIterator {
27 private int fIndex = -1;
29 private final CharSequence fSequence;
31 private final int fFirst;
33 private final int fLast;
35 private void invariant() {
36 Assert.isTrue(fIndex >= fFirst);
37 Assert.isTrue(fIndex <= fLast);
41 * Creates an iterator for the entire sequence.
44 * the sequence backing this iterator
46 public SequenceCharacterIterator(CharSequence sequence) {
51 * Creates an iterator.
54 * the sequence backing this iterator
56 * the first character to consider
57 * @throws IllegalArgumentException
58 * if the indices are out of bounds
60 public SequenceCharacterIterator(CharSequence sequence, int first)
61 throws IllegalArgumentException {
62 this(sequence, first, sequence.length());
66 * Creates an iterator.
69 * the sequence backing this iterator
71 * the first character to consider
73 * the last character index to consider
74 * @throws IllegalArgumentException
75 * if the indices are out of bounds
77 public SequenceCharacterIterator(CharSequence sequence, int first, int last)
78 throws IllegalArgumentException {
80 throw new NullPointerException();
81 if (first < 0 || first > last)
82 throw new IllegalArgumentException();
83 if (last > sequence.length())
84 throw new IllegalArgumentException();
93 * @see java.text.CharacterIterator#first()
96 return setIndex(getBeginIndex());
100 * @see java.text.CharacterIterator#last()
104 return setIndex(getEndIndex());
106 return setIndex(getEndIndex() - 1);
110 * @see java.text.CharacterIterator#current()
112 public char current() {
113 if (fIndex >= fFirst && fIndex < fLast)
114 return fSequence.charAt(fIndex);
120 * @see java.text.CharacterIterator#next()
123 return setIndex(Math.min(fIndex + 1, getEndIndex()));
127 * @see java.text.CharacterIterator#previous()
129 public char previous() {
130 if (fIndex > getBeginIndex()) {
131 return setIndex(fIndex - 1);
138 * @see java.text.CharacterIterator#setIndex(int)
140 public char setIndex(int position) {
141 if (position >= getBeginIndex() && position <= getEndIndex())
144 throw new IllegalArgumentException();
151 * @see java.text.CharacterIterator#getBeginIndex()
153 public int getBeginIndex() {
158 * @see java.text.CharacterIterator#getEndIndex()
160 public int getEndIndex() {
165 * @see java.text.CharacterIterator#getIndex()
167 public int getIndex() {
172 * @see java.text.CharacterIterator#clone()
174 public Object clone() {
176 return super.clone();
177 } catch (CloneNotSupportedException e) {
178 throw new InternalError();