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