]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/text/SequenceCharacterIterator.java
44359fb6350d68920e244c3ee6249ff67f67049f
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / text / SequenceCharacterIterator.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
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 {
25
26         private int fIndex= -1;
27         private final CharSequence 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 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 sequence, int first) throws IllegalArgumentException {
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 sequence, int first, int last) throws IllegalArgumentException {
65                 if (sequence == null)
66                         throw new NullPointerException();
67                 if (first < 0 || first > last)
68                         throw new IllegalArgumentException();
69                 if (last > sequence.length())
70                         throw new IllegalArgumentException();
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();
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         @Override
161         public Object clone() {
162                 try {
163                         return super.clone();
164                 } catch (CloneNotSupportedException e) {
165                         throw new InternalError();
166                 }
167         }
168 }