KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > SequenceCharacterIterator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text;
12
13 import java.text.CharacterIterator JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17
18
19 /**
20  * A <code>CharSequence</code> based implementation of <code>CharacterIterator</code>.
21  *
22  * @since 3.0
23  */

24 public class SequenceCharacterIterator implements CharacterIterator JavaDoc {
25
26     private int fIndex= -1;
27     private final CharSequence JavaDoc fSequence;
28     private final int fFirst;
29     private final int fLast;
30
31     private void invariant() {
32         Assert.isTrue(fIndex >= fFirst);
33         Assert.isTrue(fIndex <= fLast);
34     }
35
36     /**
37      * Creates an iterator for the entire sequence.
38      *
39      * @param sequence the sequence backing this iterator
40      */

41     public SequenceCharacterIterator(CharSequence JavaDoc sequence) {
42         this(sequence, 0);
43     }
44
45     /**
46      * Creates an iterator.
47      *
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
51      */

52     public SequenceCharacterIterator(CharSequence JavaDoc sequence, int first) throws IllegalArgumentException JavaDoc {
53         this(sequence, first, sequence.length());
54     }
55
56     /**
57      * Creates an iterator.
58      *
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
63      */

64     public SequenceCharacterIterator(CharSequence JavaDoc sequence, int first, int last) throws IllegalArgumentException JavaDoc {
65         if (sequence == null)
66             throw new NullPointerException JavaDoc();
67         if (first < 0 || first > last)
68             throw new IllegalArgumentException JavaDoc();
69         if (last > sequence.length())
70             throw new IllegalArgumentException JavaDoc();
71         fSequence= sequence;
72         fFirst= first;
73         fLast= last;
74         fIndex= first;
75         invariant();
76     }
77
78     /*
79      * @see java.text.CharacterIterator#first()
80      */

81     public char first() {
82         return setIndex(getBeginIndex());
83     }
84
85     /*
86      * @see java.text.CharacterIterator#last()
87      */

88     public char last() {
89         if (fFirst == fLast)
90             return setIndex(getEndIndex());
91         else
92             return setIndex(getEndIndex() - 1);
93     }
94
95     /*
96      * @see java.text.CharacterIterator#current()
97      */

98     public char current() {
99         if (fIndex >= fFirst && fIndex < fLast)
100             return fSequence.charAt(fIndex);
101         else
102             return DONE;
103     }
104
105     /*
106      * @see java.text.CharacterIterator#next()
107      */

108     public char next() {
109         return setIndex(Math.min(fIndex + 1, getEndIndex()));
110     }
111
112     /*
113      * @see java.text.CharacterIterator#previous()
114      */

115     public char previous() {
116         if (fIndex > getBeginIndex()) {
117             return setIndex(fIndex - 1);
118         } else {
119             return DONE;
120         }
121     }
122
123     /*
124      * @see java.text.CharacterIterator#setIndex(int)
125      */

126     public char setIndex(int position) {
127         if (position >= getBeginIndex() && position <= getEndIndex())
128             fIndex= position;
129         else
130             throw new IllegalArgumentException JavaDoc();
131
132         invariant();
133         return current();
134     }
135
136     /*
137      * @see java.text.CharacterIterator#getBeginIndex()
138      */

139     public int getBeginIndex() {
140         return fFirst;
141     }
142
143     /*
144      * @see java.text.CharacterIterator#getEndIndex()
145      */

146     public int getEndIndex() {
147         return fLast;
148     }
149
150     /*
151      * @see java.text.CharacterIterator#getIndex()
152      */

153     public int getIndex() {
154         return fIndex;
155     }
156
157     /*
158      * @see java.text.CharacterIterator#clone()
159      */

160     public Object JavaDoc clone() {
161         try {
162             return super.clone();
163         } catch (CloneNotSupportedException JavaDoc e) {
164             throw new InternalError JavaDoc();
165         }
166     }
167 }
168
Popular Tags