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