1 9 package javolution.text; 10 11 import j2me.lang.CharSequence; 12 import j2me.lang.Comparable; 13 import javolution.util.FastComparator; 14 15 38 public final class CharArray implements CharSequence , Comparable { 39 40 43 private char[] _array; 44 45 48 private int _offset; 49 50 53 private int _length; 54 55 58 private String _asString; 59 60 63 public CharArray() { 64 } 65 66 71 public CharArray(String string) { 72 _array = string.toCharArray(); 73 _length = string.length(); 74 _asString = string; 75 } 76 77 82 public CharArray(CharSequence csq) { 83 _length = csq.length(); 84 _array = new char[_length]; 85 for (int i=0; i < _length;) { 86 _array[i] = csq.charAt(i++); 87 } 88 } 89 90 97 public char[] array() { 98 return _array; 99 } 100 101 107 public int length() { 108 return _length; 109 } 110 111 116 public int offset() { 117 return _offset; 118 } 119 120 128 public CharArray setArray(char[] array, int offset, int length) { 129 _array = array; 130 _offset = offset; 131 _length = length; 132 _asString = null; 133 return this; 134 } 135 136 144 public char charAt(int index) { 145 if ((index < 0) || (index >= _length)) 146 throw new IndexOutOfBoundsException ("index: " + index); 147 return _array[_offset + index]; 148 } 149 150 161 public CharSequence subSequence(int start, int end) { 162 if ((start < 0) || (end < 0) || (start > end) || (end > this.length())) 163 throw new IndexOutOfBoundsException (); 164 CharArray chars = new CharArray(); 165 chars._array = _array; 166 chars._offset = _offset + start; 167 chars._length = end - start; 168 return chars; 169 } 170 171 181 public final int offsetOf(CharSequence csq) { 182 final char c = csq.charAt(0); 183 final int csqLength = csq.length(); 184 for (int i=_offset, end = _offset + _length - csqLength + 1; i < end; i++) { 185 if (_array[i] == c) { boolean match = true; 187 for (int j = 1; j < csqLength; j++) { 188 if (_array[i + j] != csq.charAt(j)) { 189 match = false; 190 break; 191 } 192 } 193 if (match) { 194 return i; 195 } 196 } 197 } 198 return -1; 199 } 200 201 211 public final int offsetOf(char c) { 212 for (int i=_offset, end = _offset + _length; i < end; i++) { 213 if (_array[i] == c) return i; 214 } 215 return -1; 216 } 217 218 225 public String toString() { 226 if (_asString == null) { 227 _asString = new String (_array, _offset, _length); 228 } 229 return _asString; 230 } 231 232 239 public int hashCode() { 240 if (_asString != null) return _asString.hashCode(); 241 int h = 0; 242 for (int i = 0, j = _offset; i < _length; i++) { 243 h = 31 * h + _array[j++]; 244 } 245 return h; 246 } 247 248 256 public boolean equals(Object that) { 257 if (that instanceof String ) { 258 return equals((String ) that); 259 } else if (that instanceof CharArray) { 260 return equals((CharArray) that); 261 } else if (that instanceof CharSequence ) { 262 return equals((CharSequence ) that); 263 } else { 264 return false; 265 } 266 } 267 268 private boolean equals(CharSequence chars) { 270 if (chars == null) 271 return false; 272 if (this._length != chars.length()) 273 return false; 274 for (int i = _length, j = _offset + _length; --i >= 0;) { 275 if (_array[--j] != chars.charAt(i)) 276 return false; 277 } 278 return true; 279 } 280 281 288 public boolean equals(CharArray that) { 289 if (this == that) 290 return true; 291 if (that == null) 292 return false; 293 if (this._length != that._length) 294 return false; 295 final char[] thatData = that._array; 296 for (int i = that._offset + _length, j = _offset + _length; --j >= _offset;) { 297 if (_array[j] != thatData[--i]) 298 return false; 299 } 300 return true; 301 } 302 303 312 public boolean equals(String str) { 313 if (_asString != null) 314 return (_asString == str) ? true : _asString.equals(str) ? 315 (_asString = str) == str : false; 316 if (str == null) 317 return false; 318 if (_length != str.length()) 319 return false; 320 for (int i = _length, j = _offset + _length; --i >= 0;) { 321 if (_array[--j] != str.charAt(i)) 322 return false; 323 } 324 _asString = str; 325 return true; 326 } 327 328 329 338 public int compareTo(Object seq) { 339 return ((FastComparator)FastComparator.LEXICAL).compare(this, seq); 340 } 341 342 349 public boolean toBoolean() { 350 int i = _offset; 351 if ((_length == 4) && (_array[i] == 't' || _array[i] == 'T') 352 && (_array[++i] == 'r' || _array[i] == 'R') 353 && (_array[++i] == 'u' || _array[i] == 'U') 354 && (_array[++i] == 'e' || _array[i] == 'E')) 355 return true; 356 if ((_length == 5) && (_array[i] == 'f' || _array[i] == 'F') 357 && (_array[++i] == 'a' || _array[i] == 'A') 358 && (_array[++i] == 'l' || _array[i] == 'L') 359 && (_array[++i] == 's' || _array[i] == 'S') 360 && (_array[++i] == 'e' || _array[i] == 'E')) 361 return false; 362 throw new IllegalArgumentException ("Cannot parse " + this 363 + " as boolean"); 364 } 365 366 373 public int toInt() { 374 return TypeFormat.parseInt(this); 375 } 376 377 386 public int toInt(int radix) { 387 return TypeFormat.parseInt(this, radix); 388 } 389 390 398 public long toLong() { 399 return TypeFormat.parseLong(this); 400 } 401 402 411 public long toLong(int radix) { 412 return TypeFormat.parseLong(this, radix); 413 } 414 415 427 428 439 440 } | Popular Tags |