1 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 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 41 public SequenceCharacterIterator(CharSequence sequence) { 42 this(sequence, 0); 43 } 44 45 52 public SequenceCharacterIterator(CharSequence sequence, int first) throws IllegalArgumentException { 53 this(sequence, first, sequence.length()); 54 } 55 56 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 81 public char first() { 82 return setIndex(getBeginIndex()); 83 } 84 85 88 public char last() { 89 if (fFirst == fLast) 90 return setIndex(getEndIndex()); 91 else 92 return setIndex(getEndIndex() - 1); 93 } 94 95 98 public char current() { 99 if (fIndex >= fFirst && fIndex < fLast) 100 return fSequence.charAt(fIndex); 101 else 102 return DONE; 103 } 104 105 108 public char next() { 109 return setIndex(Math.min(fIndex + 1, getEndIndex())); 110 } 111 112 115 public char previous() { 116 if (fIndex > getBeginIndex()) { 117 return setIndex(fIndex - 1); 118 } else { 119 return DONE; 120 } 121 } 122 123 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 139 public int getBeginIndex() { 140 return fFirst; 141 } 142 143 146 public int getEndIndex() { 147 return fLast; 148 } 149 150 153 public int getIndex() { 154 return fIndex; 155 } 156 157 160 public Object clone() { 161 try { 162 return super.clone(); 163 } catch (CloneNotSupportedException e) { 164 throw new InternalError (); 165 } 166 } 167 } 168 | Popular Tags |