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 import org.eclipse.jface.text.BadLocationException; 18 import org.eclipse.jface.text.IDocument; 19 20 21 33 public class DocumentCharacterIterator implements CharacterIterator , CharSequence { 34 35 private int fIndex= -1; 36 private final IDocument fDocument; 37 private final int fFirst; 38 private final int fLast; 39 40 private void invariant() { 41 Assert.isTrue(fIndex >= fFirst); 42 Assert.isTrue(fIndex <= fLast); 43 } 44 45 50 public DocumentCharacterIterator(IDocument document) { 51 this(document, 0); 52 } 53 54 61 public DocumentCharacterIterator(IDocument document, int first) throws IllegalArgumentException { 62 this(document, first, document.getLength()); 63 } 64 65 74 public DocumentCharacterIterator(IDocument document, int first, int last) throws IllegalArgumentException { 75 if (document == null) 76 throw new NullPointerException (); 77 if (first < 0 || first > last) 78 throw new IllegalArgumentException (); 79 if (last > document.getLength()) 80 throw new IllegalArgumentException (); 81 fDocument= document; 82 fFirst= first; 83 fLast= last; 84 fIndex= first; 85 invariant(); 86 } 87 88 91 public char first() { 92 return setIndex(getBeginIndex()); 93 } 94 95 98 public char last() { 99 if (fFirst == fLast) 100 return setIndex(getEndIndex()); 101 else 102 return setIndex(getEndIndex() - 1); 103 } 104 105 108 public char current() { 109 if (fIndex >= fFirst && fIndex < fLast) 110 try { 111 return fDocument.getChar(fIndex); 112 } catch (BadLocationException e) { 113 } 115 return DONE; 116 } 117 118 121 public char next() { 122 return setIndex(Math.min(fIndex + 1, getEndIndex())); 123 } 124 125 128 public char previous() { 129 if (fIndex > getBeginIndex()) { 130 return setIndex(fIndex - 1); 131 } else { 132 return DONE; 133 } 134 } 135 136 139 public char setIndex(int position) { 140 if (position >= getBeginIndex() && position <= getEndIndex()) 141 fIndex= position; 142 else 143 throw new IllegalArgumentException (); 144 145 invariant(); 146 return current(); 147 } 148 149 152 public int getBeginIndex() { 153 return fFirst; 154 } 155 156 159 public int getEndIndex() { 160 return fLast; 161 } 162 163 166 public int getIndex() { 167 return fIndex; 168 } 169 170 173 public Object clone() { 174 try { 175 return super.clone(); 176 } catch (CloneNotSupportedException e) { 177 throw new InternalError (); 178 } 179 } 180 181 184 public int length() { 185 return getEndIndex() - getBeginIndex(); 186 } 187 188 199 public char charAt(int index) { 200 if (index >= 0 && index < length()) 201 try { 202 return fDocument.getChar(getBeginIndex() + index); 203 } catch (BadLocationException e) { 204 return DONE; 206 } 207 else 208 throw new IndexOutOfBoundsException (); 209 } 210 211 214 public CharSequence subSequence(int start, int end) { 215 if (start < 0) 216 throw new IndexOutOfBoundsException (); 217 if (end < start) 218 throw new IndexOutOfBoundsException (); 219 if (end > length()) 220 throw new IndexOutOfBoundsException (); 221 return new DocumentCharacterIterator(fDocument, getBeginIndex() + start, getBeginIndex() + end); 222 } 223 } 224 | Popular Tags |