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;
16 import org.eclipse.jface.text.BadLocationException;
17 import org.eclipse.jface.text.IDocument;
21 * An <code>IDocument</code> based implementation of <code>CharacterIterator</code>.
25 public class DocumentCharacterIterator implements CharacterIterator, CharSequence {
27 private int fIndex= -1;
28 private final IDocument fDocument;
29 private final int fFirst;
30 private final int fLast;
32 private void invariant() {
33 Assert.isTrue(fIndex >= fFirst);
34 Assert.isTrue(fIndex <= fLast);
38 * Creates an iterator for the entire sequence.
40 * @param sequence the sequence backing this iterator
42 public DocumentCharacterIterator(IDocument sequence) {
47 * Creates an iterator.
49 * @param sequence the sequence backing this iterator
50 * @param first the first character to consider
51 * @throws IllegalArgumentException if the indices are out of bounds
53 public DocumentCharacterIterator(IDocument sequence, int first) throws IllegalArgumentException {
54 this(sequence, first, sequence.getLength());
58 * Creates an iterator.
60 * @param sequence the sequence backing this iterator
61 * @param first the first character to consider
62 * @param last the last character index to consider
63 * @throws IllegalArgumentException if the indices are out of bounds
65 public DocumentCharacterIterator(IDocument sequence, int first, int last) throws IllegalArgumentException {
67 throw new NullPointerException();
68 if (first < 0 || first > last)
69 throw new IllegalArgumentException();
70 if (last > sequence.getLength())
71 throw new IllegalArgumentException();
80 * @see java.text.CharacterIterator#first()
83 return setIndex(getBeginIndex());
87 * @see java.text.CharacterIterator#last()
91 return setIndex(getEndIndex());
93 return setIndex(getEndIndex() - 1);
97 * @see java.text.CharacterIterator#current()
99 public char current() {
100 if (fIndex >= fFirst && fIndex < fLast)
102 return fDocument.getChar(fIndex);
103 } catch (BadLocationException e) {
110 * @see java.text.CharacterIterator#next()
113 return setIndex(Math.min(fIndex + 1, getEndIndex()));
117 * @see java.text.CharacterIterator#previous()
119 public char previous() {
120 if (fIndex > getBeginIndex()) {
121 return setIndex(fIndex - 1);
128 * @see java.text.CharacterIterator#setIndex(int)
130 public char setIndex(int position) {
131 if (position >= getBeginIndex() && position <= getEndIndex())
134 throw new IllegalArgumentException();
141 * @see java.text.CharacterIterator#getBeginIndex()
143 public int getBeginIndex() {
148 * @see java.text.CharacterIterator#getEndIndex()
150 public int getEndIndex() {
155 * @see java.text.CharacterIterator#getIndex()
157 public int getIndex() {
162 * @see java.text.CharacterIterator#clone()
164 public Object clone() {
166 return super.clone();
167 } catch (CloneNotSupportedException e) {
168 throw new InternalError();
173 * @see java.lang.CharSequence#length()
175 public int length() {
176 return getEndIndex() - getBeginIndex();
180 * @see java.lang.CharSequence#charAt(int)
182 public char charAt(int index) {
183 if (index >= getBeginIndex() && index <= getEndIndex())
185 return fDocument.getChar(index);
186 } catch (BadLocationException e) {
187 // ignore and return DONE
191 throw new IllegalArgumentException();
195 * @see java.lang.CharSequence#subSequence(int, int)
197 public CharSequence subSequence(int start, int end) {
198 return new DocumentCharacterIterator(fDocument, getBeginIndex() + start, getBeginIndex() + start + end);