1 7 package javax.swing.text; 8 9 import java.text.CharacterIterator ; 10 11 26 public class Segment implements Cloneable , CharacterIterator { 27 28 33 public char[] array; 34 35 39 public int offset; 40 41 45 public int count; 46 47 private boolean partialReturn; 48 49 52 public Segment() { 53 this(null, 0, 0); 54 } 55 56 63 public Segment(char[] array, int offset, int count) { 64 this.array = array; 65 this.offset = offset; 66 this.count = count; 67 partialReturn = false; 68 } 69 70 81 public void setPartialReturn(boolean p) { 82 partialReturn = p; 83 } 84 85 91 public boolean isPartialReturn() { 92 return partialReturn; 93 } 94 95 100 public String toString() { 101 if (array != null) { 102 return new String (array, offset, count); 103 } 104 return new String (); 105 } 106 107 109 115 public char first() { 116 pos = offset; 117 if (count != 0) { 118 return array[pos]; 119 } 120 return DONE; 121 } 122 123 129 public char last() { 130 pos = offset + count; 131 if (count != 0) { 132 pos -= 1; 133 return array[pos]; 134 } 135 return DONE; 136 } 137 138 144 public char current() { 145 if (count != 0 && pos < offset + count) { 146 return array[pos]; 147 } 148 return DONE; 149 } 150 151 159 public char next() { 160 pos += 1; 161 int end = offset + count; 162 if (pos >= end) { 163 pos = end; 164 return DONE; 165 } 166 return current(); 167 } 168 169 176 public char previous() { 177 if (pos == offset) { 178 return DONE; 179 } 180 pos -= 1; 181 return current(); 182 } 183 184 192 public char setIndex(int position) { 193 int end = offset + count; 194 if ((position < offset) || (position > end)) { 195 throw new IllegalArgumentException ("bad position: " + position); 196 } 197 pos = position; 198 if ((pos != end) && (count != 0)) { 199 return array[pos]; 200 } 201 return DONE; 202 } 203 204 208 public int getBeginIndex() { 209 return offset; 210 } 211 212 217 public int getEndIndex() { 218 return offset + count; 219 } 220 221 225 public int getIndex() { 226 return pos; 227 } 228 229 234 public Object clone() { 235 Object o; 236 try { 237 o = super.clone(); 238 } catch (CloneNotSupportedException cnse) { 239 o = null; 240 } 241 return o; 242 } 243 244 private int pos; 245 246 247 } 248 249 250 | Popular Tags |