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