1 22 23 package com.sosnoski.util; 24 25 import com.sosnoski.util.array.CharArray; 26 27 46 47 public class CharBuffer extends CharArray 48 { 49 50 protected static final int KEY_MULTIPLIER = 517; 51 52 58 59 public CharBuffer(int size, int growth) { 60 super(size, growth); 61 } 62 63 68 69 public CharBuffer(int size) { 70 super(size); 71 } 72 73 76 77 public CharBuffer() { 78 this(DEFAULT_SIZE); 79 } 80 81 84 85 public CharBuffer(String base) { 86 this(base.length()); 87 append(base); 88 } 89 90 93 94 public CharBuffer(char[] base) { 95 this(base.length); 96 append(base); 97 } 98 99 104 105 public CharBuffer(CharArray base) { 106 super(base); 107 } 108 109 116 117 public int append(String text) { 118 119 int length = text.length(); 121 ensureCapacity(m_countPresent + length); 122 123 int start = m_countPresent; 125 for (int i = 0; i < length; i++) { 126 m_baseArray[m_countPresent++] = text.charAt(i); 127 } 128 return start; 129 } 130 131 140 141 public int append(char[] text, int offset, int length) { 142 143 ensureCapacity(m_countPresent + length); 145 146 int start = m_countPresent; 148 System.arraycopy(text, offset, m_baseArray, m_countPresent, length); 149 m_countPresent += length; 150 return start; 151 } 152 153 160 161 public int append(char[] text) { 162 return append(text, 0, text.length); 163 } 164 165 172 173 public int append(CharArray text) { 174 return append((char[])getArray(text), 0, text.size()); 175 } 176 177 187 188 protected void adjust(int from, int to, int length) { 189 if (from >= 0 && to < m_countPresent && from <= to) { 190 int change = from + length - to; 191 if (change > 0) { 192 ensureCapacity(m_countPresent+change); 193 } 194 if (to < m_countPresent){ 195 System.arraycopy(m_baseArray, to, m_baseArray, to + change, 196 m_countPresent - to); 197 m_countPresent += change; 198 } 199 } else { 200 throw new ArrayIndexOutOfBoundsException ("Invalid remove range"); 201 } 202 } 203 204 212 213 public void replace(int from, int to, String text) { 214 adjust(from, to, text.length()); 215 text.getChars(0, text.length(), m_baseArray, from); 216 } 217 218 226 227 public void replace(int from, int to, char[] text) { 228 adjust(from, to, text.length); 229 System.arraycopy(text, 0, m_baseArray, from, text.length); 230 } 231 232 238 239 public void insert(int offset, String text) { 240 adjust(offset, offset, text.length()); 241 text.getChars(0, text.length(), m_baseArray, offset); 242 } 243 244 250 251 public void insert(int offset, char[] text) { 252 adjust(offset, offset, text.length); 253 System.arraycopy(text, 0, m_baseArray, offset, text.length); 254 } 255 256 263 264 public boolean equals(String comp) { 265 266 if (comp != null && m_countPresent == comp.length()) { 268 269 for (int index = 0; index < m_countPresent; index++) { 271 if (m_baseArray[index] != comp.charAt(index)) { 272 return false; 273 } 274 } 275 276 return true; 278 } 279 280 return false; 282 } 283 284 294 295 public boolean equals(char[] comp, int offset, int length) { 296 297 if (comp != null && m_countPresent == length) { 299 300 for (int index = 0; index < m_countPresent; index++) { 302 if (m_baseArray[index] != comp[index+offset]) { 303 return false; 304 } 305 } 306 307 return true; 309 } 310 311 return false; 313 } 314 315 323 324 public boolean equals(char[] comp) { 325 return equals(comp, 0, comp.length); 326 } 327 328 336 337 public boolean equals(CharArray comp) { 338 return equals((char[])getArray(comp), 0, comp.size()); 339 } 340 341 349 350 public static boolean equals(char[] a, char[] b) { 351 352 if (b != null && a.length == b.length) { 354 355 for (int i = 0; i < a.length; i++) { 357 if (a[i] != b[i]) { 358 return false; 359 } 360 } 361 return true; 362 363 } else { 364 return false; 365 } 366 } 367 368 378 379 public static boolean equals(char[] a, String b) { 380 381 if (b != null && a.length == b.length()) { 383 384 for (int i = 0; i < a.length; i++) { 386 if (a[i] != b.charAt(i)) { 387 return false; 388 } 389 } 390 return true; 391 392 } else { 393 return false; 394 } 395 } 396 397 405 406 public boolean equalsNoCase(String comp) { 407 408 if (comp != null && m_countPresent == comp.length()) { 410 411 int index = 0; 413 for (; index < m_countPresent; index++) { 414 if (Character.toLowerCase(m_baseArray[index]) != 415 Character.toLowerCase(comp.charAt(index))) { 416 return false; 417 } 418 } 419 420 return true; 422 } 423 424 return false; 426 } 427 428 438 439 public boolean equalsNoCase(char[] comp, int offset, int length) { 440 441 if (comp != null && m_countPresent == length) { 443 444 for (int index = 0; index < m_countPresent; index++) { 446 if (Character.toLowerCase(m_baseArray[index]) != 447 Character.toLowerCase(comp[index+offset])) { 448 return false; 449 } 450 } 451 452 return true; 454 } 455 456 return false; 458 } 459 460 468 469 public boolean equalsNoCase(char[] comp) { 470 return equalsNoCase(comp, 0, comp.length); 471 } 472 473 481 482 public boolean equalsNoCase(CharArray comp) { 483 return equalsNoCase((char[])getArray(comp), 0, comp.size()); 484 } 485 486 495 496 public static boolean equalsNoCase(char[] a, char[] b) { 497 498 if (b != null && a.length == b.length) { 500 501 for (int i = 0; i < a.length; i++) { 503 if (Character.toUpperCase(a[i]) != 504 Character.toUpperCase(b[i])) { 505 return false; 506 } 507 } 508 return true; 509 510 } else { 511 return false; 512 } 513 } 514 515 525 526 public static boolean equalsNoCase(char[] a, String b) { 527 528 if (b != null && a.length == b.length()) { 530 531 for (int i = 0; i < a.length; i++) { 533 if (Character.toLowerCase(a[i]) != 534 Character.toLowerCase(b.charAt(i))) { 535 return false; 536 } 537 } 538 return true; 539 540 } else { 541 return false; 542 } 543 } 544 545 552 553 public static int hashCode(char[] text) { 554 int hash = 0; 555 for (int i = 0; i < text.length; i++) { 556 hash = (hash * KEY_MULTIPLIER) + text[i]; 557 } 558 return hash; 559 } 560 561 568 569 public static int hashCode(String text) { 570 int hash = 0; 571 for (int i = 0; i < text.length(); i++) { 572 hash = (hash * KEY_MULTIPLIER) + text.charAt(i); 573 } 574 return hash; 575 } 576 577 582 583 public int hashCode() { 584 int hash = 0; 585 for (int i = 0; i < size(); i++) { 586 hash = (hash * KEY_MULTIPLIER) + m_baseArray[i]; 587 } 588 return hash; 589 } 590 591 596 597 public Object clone() { 598 return new CharBuffer(this); 599 } 600 601 606 607 public String toString() { 608 return new String (m_baseArray, 0, m_countPresent); 609 } 610 611 619 620 public String toString(int offset, int length) { 621 if (offset + length <= m_countPresent) { 622 return new String (m_baseArray, offset, length); 623 } else { 624 throw new ArrayIndexOutOfBoundsException (); 625 } 626 } 627 } 628 | Popular Tags |