X-Git-Url: http://secure.phpeclipse.com
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/DocumentCharacterIterator.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/DocumentCharacterIterator.java
index 1aabb54..416ddaf 100644
--- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/DocumentCharacterIterator.java
+++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/text/DocumentCharacterIterator.java
@@ -1,10 +1,10 @@
/*******************************************************************************
- * Copyright (c) 2000, 2004 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Common Public License v1.0
+ * Copyright (c) 2000, 2005 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
@@ -16,63 +16,84 @@ import org.eclipse.jface.text.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
-
/**
- * An IDocument
based implementation of CharacterIterator
.
+ * An IDocument
based implementation of
+ * CharacterIterator
and CharSequence
. Note that
+ * the supplied document is not copied; if the document is modified during the
+ * lifetime of a DocumentCharacterIterator
, the methods
+ * returning document content may not always return the same values. Also, if
+ * accessing the document fails with a {@link BadLocationException}, any of
+ * CharacterIterator
methods as well as charAt
may
+ * return {@link CharacterIterator#DONE}.
*
* @since 3.0
*/
-public class DocumentCharacterIterator implements CharacterIterator, CharSequence {
+public class DocumentCharacterIterator implements CharacterIterator,
+ CharSequence {
+
+ private int fIndex = -1;
- private int fIndex= -1;
private final IDocument fDocument;
+
private final int fFirst;
+
private final int fLast;
-
+
private void invariant() {
Assert.isTrue(fIndex >= fFirst);
Assert.isTrue(fIndex <= fLast);
}
-
+
/**
- * Creates an iterator for the entire sequence.
+ * Creates an iterator for the entire document.
*
- * @param sequence the sequence backing this iterator
+ * @param document
+ * the document backing this iterator
*/
- public DocumentCharacterIterator(IDocument sequence) {
- this(sequence, 0);
+ public DocumentCharacterIterator(IDocument document) {
+ this(document, 0);
}
-
+
/**
- * Creates an iterator.
+ * Creates an iterator, starting at offset first
.
*
- * @param sequence the sequence backing this iterator
- * @param first the first character to consider
- * @throws IllegalArgumentException if the indices are out of bounds
+ * @param document
+ * the document backing this iterator
+ * @param first
+ * the first character to consider
+ * @throws IllegalArgumentException
+ * if the indices are out of bounds
*/
- public DocumentCharacterIterator(IDocument sequence, int first) throws IllegalArgumentException {
- this(sequence, first, sequence.getLength());
+ public DocumentCharacterIterator(IDocument document, int first)
+ throws IllegalArgumentException {
+ this(document, first, document.getLength());
}
/**
- * Creates an iterator.
+ * Creates an iterator for the document contents from first
+ * (inclusive) to last
(exclusive).
*
- * @param sequence the sequence backing this iterator
- * @param first the first character to consider
- * @param last the last character index to consider
- * @throws IllegalArgumentException if the indices are out of bounds
- */
- public DocumentCharacterIterator(IDocument sequence, int first, int last) throws IllegalArgumentException {
- if (sequence == null)
+ * @param document
+ * the document backing this iterator
+ * @param first
+ * the first character to consider
+ * @param last
+ * the last character index to consider
+ * @throws IllegalArgumentException
+ * if the indices are out of bounds
+ */
+ public DocumentCharacterIterator(IDocument document, int first, int last)
+ throws IllegalArgumentException {
+ if (document == null)
throw new NullPointerException();
if (first < 0 || first > last)
throw new IllegalArgumentException();
- if (last > sequence.getLength())
+ if (last > document.getLength())
throw new IllegalArgumentException();
- fDocument= sequence;
- fFirst= first;
- fLast= last;
- fIndex= first;
+ fDocument = document;
+ fFirst = first;
+ fLast = last;
+ fIndex = first;
invariant();
}
@@ -129,10 +150,10 @@ public class DocumentCharacterIterator implements CharacterIterator, CharSequenc
*/
public char setIndex(int position) {
if (position >= getBeginIndex() && position <= getEndIndex())
- fIndex= position;
+ fIndex = position;
else
throw new IllegalArgumentException();
-
+
invariant();
return current();
}
@@ -176,25 +197,41 @@ public class DocumentCharacterIterator implements CharacterIterator, CharSequenc
return getEndIndex() - getBeginIndex();
}
- /*
- * @see java.lang.CharSequence#charAt(int)
+ /**
+ * {@inheritDoc}
+ *
+ * Note that, if the document is modified concurrently, this method may + * return {@link CharacterIterator#DONE} if a {@link BadLocationException} + * was thrown when accessing the backing document. + *
+ * + * @param index + * {@inheritDoc} + * @return {@inheritDoc} */ public char charAt(int index) { - if (index >= getBeginIndex() && index <= getEndIndex()) + if (index >= 0 && index < length()) try { - return fDocument.getChar(index); + return fDocument.getChar(getBeginIndex() + index); } catch (BadLocationException e) { // ignore and return DONE return DONE; } else - throw new IllegalArgumentException(); + throw new IndexOutOfBoundsException(); } /* * @see java.lang.CharSequence#subSequence(int, int) */ public CharSequence subSequence(int start, int end) { - return new DocumentCharacterIterator(fDocument, getBeginIndex() + start, getBeginIndex() + start + end); + if (start < 0) + throw new IndexOutOfBoundsException(); + if (end < start) + throw new IndexOutOfBoundsException(); + if (end > length()) + throw new IndexOutOfBoundsException(); + return new DocumentCharacterIterator(fDocument, + getBeginIndex() + start, getBeginIndex() + end); } }