33a9906e40e5a51fc1c0ae8660f5e8fedac09048
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / SequenceCharacterIterator.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text;
12
13 import java.text.CharacterIterator;
14
15 import org.eclipse.jface.text.Assert;
16
17 /**
18  * A <code>CharSequence</code> based implementation of
19  * <code>CharacterIterator</code>.
20  * 
21  * @since 3.0
22  */
23 public class SequenceCharacterIterator implements CharacterIterator {
24
25         private int fIndex = -1;
26
27         private final CharSequence fSequence;
28
29         private final int fFirst;
30
31         private final int fLast;
32
33         private void invariant() {
34                 Assert.isTrue(fIndex >= fFirst);
35                 Assert.isTrue(fIndex <= fLast);
36         }
37
38         /**
39          * Creates an iterator for the entire sequence.
40          * 
41          * @param sequence
42          *            the sequence backing this iterator
43          */
44         public SequenceCharacterIterator(CharSequence sequence) {
45                 this(sequence, 0);
46         }
47
48         /**
49          * Creates an iterator.
50          * 
51          * @param sequence
52          *            the sequence backing this iterator
53          * @param first
54          *            the first character to consider
55          * @throws IllegalArgumentException
56          *             if the indices are out of bounds
57          */
58         public SequenceCharacterIterator(CharSequence sequence, int first)
59                         throws IllegalArgumentException {
60                 this(sequence, first, sequence.length());
61         }
62
63         /**
64          * Creates an iterator.
65          * 
66          * @param sequence
67          *            the sequence backing this iterator
68          * @param first
69          *            the first character to consider
70          * @param last
71          *            the last character index to consider
72          * @throws IllegalArgumentException
73          *             if the indices are out of bounds
74          */
75         public SequenceCharacterIterator(CharSequence sequence, int first, int last)
76                         throws IllegalArgumentException {
77                 if (sequence == null)
78                         throw new NullPointerException();
79                 if (first < 0 || first > last)
80                         throw new IllegalArgumentException();
81                 if (last > sequence.length())
82                         throw new IllegalArgumentException();
83                 fSequence = sequence;
84                 fFirst = first;
85                 fLast = last;
86                 fIndex = first;
87                 invariant();
88         }
89
90         /*
91          * @see java.text.CharacterIterator#first()
92          */
93         public char first() {
94                 return setIndex(getBeginIndex());
95         }
96
97         /*
98          * @see java.text.CharacterIterator#last()
99          */
100         public char last() {
101                 if (fFirst == fLast)
102                         return setIndex(getEndIndex());
103                 else
104                         return setIndex(getEndIndex() - 1);
105         }
106
107         /*
108          * @see java.text.CharacterIterator#current()
109          */
110         public char current() {
111                 if (fIndex >= fFirst && fIndex < fLast)
112                         return fSequence.charAt(fIndex);
113                 else
114                         return DONE;
115         }
116
117         /*
118          * @see java.text.CharacterIterator#next()
119          */
120         public char next() {
121                 return setIndex(Math.min(fIndex + 1, getEndIndex()));
122         }
123
124         /*
125          * @see java.text.CharacterIterator#previous()
126          */
127         public char previous() {
128                 if (fIndex > getBeginIndex()) {
129                         return setIndex(fIndex - 1);
130                 } else {
131                         return DONE;
132                 }
133         }
134
135         /*
136          * @see java.text.CharacterIterator#setIndex(int)
137          */
138         public char setIndex(int position) {
139                 if (position >= getBeginIndex() && position <= getEndIndex())
140                         fIndex = position;
141                 else
142                         throw new IllegalArgumentException();
143
144                 invariant();
145                 return current();
146         }
147
148         /*
149          * @see java.text.CharacterIterator#getBeginIndex()
150          */
151         public int getBeginIndex() {
152                 return fFirst;
153         }
154
155         /*
156          * @see java.text.CharacterIterator#getEndIndex()
157          */
158         public int getEndIndex() {
159                 return fLast;
160         }
161
162         /*
163          * @see java.text.CharacterIterator#getIndex()
164          */
165         public int getIndex() {
166                 return fIndex;
167         }
168
169         /*
170          * @see java.text.CharacterIterator#clone()
171          */
172         public Object clone() {
173                 try {
174                         return super.clone();
175                 } catch (CloneNotSupportedException e) {
176                         throw new InternalError();
177                 }
178         }
179 }