1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.text;
13 import java.text.CharacterIterator;
15 import org.eclipse.core.runtime.Assert;
20 * A <code>CharSequence</code> based implementation of <code>CharacterIterator</code>.
24 public class SequenceCharacterIterator implements CharacterIterator {
26 private int fIndex= -1;
27 private final CharSequence fSequence;
28 private final int fFirst;
29 private final int fLast;
31 private void invariant() {
32 Assert.isTrue(fIndex >= fFirst);
33 Assert.isTrue(fIndex <= fLast);
37 * Creates an iterator for the entire sequence.
39 * @param sequence the sequence backing this iterator
41 public SequenceCharacterIterator(CharSequence sequence) {
46 * Creates an iterator.
48 * @param sequence the sequence backing this iterator
49 * @param first the first character to consider
50 * @throws IllegalArgumentException if the indices are out of bounds
52 public SequenceCharacterIterator(CharSequence sequence, int first) throws IllegalArgumentException {
53 this(sequence, first, sequence.length());
57 * Creates an iterator.
59 * @param sequence the sequence backing this iterator
60 * @param first the first character to consider
61 * @param last the last character index to consider
62 * @throws IllegalArgumentException if the indices are out of bounds
64 public SequenceCharacterIterator(CharSequence sequence, int first, int last) throws IllegalArgumentException {
66 throw new NullPointerException();
67 if (first < 0 || first > last)
68 throw new IllegalArgumentException();
69 if (last > sequence.length())
70 throw new IllegalArgumentException();
79 * @see java.text.CharacterIterator#first()
82 return setIndex(getBeginIndex());
86 * @see java.text.CharacterIterator#last()
90 return setIndex(getEndIndex());
92 return setIndex(getEndIndex() - 1);
96 * @see java.text.CharacterIterator#current()
98 public char current() {
99 if (fIndex >= fFirst && fIndex < fLast)
100 return fSequence.charAt(fIndex);
106 * @see java.text.CharacterIterator#next()
109 return setIndex(Math.min(fIndex + 1, getEndIndex()));
113 * @see java.text.CharacterIterator#previous()
115 public char previous() {
116 if (fIndex > getBeginIndex()) {
117 return setIndex(fIndex - 1);
124 * @see java.text.CharacterIterator#setIndex(int)
126 public char setIndex(int position) {
127 if (position >= getBeginIndex() && position <= getEndIndex())
130 throw new IllegalArgumentException();
137 * @see java.text.CharacterIterator#getBeginIndex()
139 public int getBeginIndex() {
144 * @see java.text.CharacterIterator#getEndIndex()
146 public int getEndIndex() {
151 * @see java.text.CharacterIterator#getIndex()
153 public int getIndex() {
158 * @see java.text.CharacterIterator#clone()
161 public Object clone() {
163 return super.clone();
164 } catch (CloneNotSupportedException e) {
165 throw new InternalError();