1 10 28 29 package org.mmbase.util; 30 31 58 59 public final class StringObject { 60 61 private char value[]; 62 63 64 private int count; 65 66 67 private boolean shared; 68 69 72 public StringObject() { 73 this(16); 74 } 75 76 80 public StringObject(int length) { 81 value = new char[length]; 82 shared = false; 83 } 84 85 89 public StringObject(String str) { 90 this(str.length() + 16); 91 append(str); 92 } 93 94 97 public int length() { 98 return count; 99 } 100 101 106 public int capacity() { 107 return value.length; 108 } 109 110 113 private final void copyWhenShared() { 114 if (shared) { 115 char newValue[] = new char[value.length]; 116 System.arraycopy(value, 0, newValue, 0, count); 117 value = newValue; 118 shared = false; 119 } 120 } 121 122 127 public synchronized void ensureCapacity(int minimumCapacity) { 128 int maxCapacity = value.length; 129 130 if (minimumCapacity > maxCapacity) { 131 int newCapacity = (maxCapacity + 1) * 2; 132 if (minimumCapacity > newCapacity) { 133 newCapacity = minimumCapacity; 134 } 135 136 char newValue[] = new char[newCapacity]; 137 System.arraycopy(value, 0, newValue, 0, count); 138 value = newValue; 139 shared = false; 140 } 141 } 142 143 150 public synchronized void setLength(int newLength) { 151 if (newLength < 0) { 152 throw new StringIndexOutOfBoundsException (newLength); 153 } 154 ensureCapacity(newLength); 155 156 if (count < newLength) { 157 copyWhenShared(); 158 for (; count < newLength; count++) { 159 value[count] = '\0'; 160 } 161 } 162 count = newLength; 163 } 164 165 171 public synchronized char charAt(int index) { 172 if ((index < 0) || (index >= count)) { 173 throw new StringIndexOutOfBoundsException (index); 174 } 175 return value[index]; 176 } 177 178 189 public synchronized void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { 190 if ((srcBegin < 0) || (srcBegin >= count)) { 191 throw new StringIndexOutOfBoundsException (srcBegin); 192 } 193 if ((srcEnd < 0) || (srcEnd > count)) { 194 throw new StringIndexOutOfBoundsException (srcEnd); 195 } 196 if (srcBegin < srcEnd) { 197 System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); 198 } 199 } 200 201 207 public synchronized void setCharAt(int index, char ch) { 208 if ((index < 0) || (index >= count)) { 209 throw new StringIndexOutOfBoundsException (index); 210 } 211 copyWhenShared(); 212 value[index] = ch; 213 } 214 215 220 public synchronized StringObject append(Object obj) { 221 return append(String.valueOf(obj)); 222 } 223 224 229 public synchronized StringObject append(String str) { 230 if (str == null) { 231 str = String.valueOf(str); 232 } 233 234 int len = str.length(); 235 ensureCapacity(count + len); 236 copyWhenShared(); 237 str.getChars(0, len, value, count); 238 count += len; 239 return this; 240 } 241 242 247 public synchronized StringObject append(char str[]) { 248 int len = str.length; 249 ensureCapacity(count + len); 250 copyWhenShared(); 251 System.arraycopy(str, 0, value, count, len); 252 count += len; 253 return this; 254 } 255 256 263 public synchronized StringObject append(char str[], int offset, int len) { 264 ensureCapacity(count + len); 265 copyWhenShared(); 266 System.arraycopy(str, offset, value, count, len); 267 count += len; 268 return this; 269 } 270 271 276 public StringObject append(boolean b) { 277 return append(String.valueOf(b)); 278 } 279 280 285 public synchronized StringObject append(char c) { 286 ensureCapacity(count + 1); 287 copyWhenShared(); 288 value[count++] = c; 289 return this; 290 } 291 292 297 public StringObject append(int i) { 298 return append(String.valueOf(i)); 299 } 300 301 306 public StringObject append(long l) { 307 return append(String.valueOf(l)); 308 } 309 310 315 public StringObject append(float f) { 316 return append(String.valueOf(f)); 317 } 318 319 324 public StringObject append(double d) { 325 return append(String.valueOf(d)); 326 } 327 328 335 public synchronized StringObject insert(int offset, Object obj) { 336 return insert(offset, String.valueOf(obj)); 337 } 338 339 346 public synchronized StringObject insert(int offset, String str) { 347 if ((offset < 0) || (offset > count)) { 348 throw new StringIndexOutOfBoundsException (); 349 } 350 int len = str.length(); 351 ensureCapacity(count + len); 352 copyWhenShared(); 353 System.arraycopy(value, offset, value, offset + len, count - offset); 354 str.getChars(0, len, value, offset); 355 count += len; 356 return this; 357 } 358 359 366 public synchronized StringObject insert(int offset, char str[]) { 367 if ((offset < 0) || (offset > count)) { 368 throw new StringIndexOutOfBoundsException (); 369 } 370 int len = str.length; 371 ensureCapacity(count + len); 372 copyWhenShared(); 373 System.arraycopy(value, offset, value, offset + len, count - offset); 374 System.arraycopy(str, 0, value, offset, len); 375 count += len; 376 return this; 377 } 378 379 386 public StringObject insert(int offset, boolean b) { 387 return insert(offset, String.valueOf(b)); 388 } 389 390 397 public synchronized StringObject insert(int offset, char c) { 398 ensureCapacity(count + 1); 399 copyWhenShared(); 400 System.arraycopy(value, offset, value, offset + 1, count - offset); 401 value[offset] = c; 402 count += 1; 403 return this; 404 } 405 406 413 public StringObject insert(int offset, int i) { 414 return insert(offset, String.valueOf(i)); 415 } 416 417 424 public StringObject insert(int offset, long l) { 425 return insert(offset, String.valueOf(l)); 426 } 427 428 435 public StringObject insert(int offset, float f) { 436 return insert(offset, String.valueOf(f)); 437 } 438 439 446 public StringObject insert(int offset, double d) { 447 return insert(offset, String.valueOf(d)); 448 } 449 450 453 public synchronized StringObject reverse() { 454 copyWhenShared(); 455 int n = count - 1; 456 for (int j = (n-1) >> 1; j >= 0; --j) { 457 char temp = value[j]; 458 value[j] = value[n - j]; 459 value[n - j] = temp; 460 } 461 return this; 462 } 463 464 465 468 public String toString() { 469 return new String (value,0,count); 470 } 471 472 477 final void setShared() { 478 shared = true; 479 } 480 481 final char[] getValue() { 482 return value; 483 } 484 485 488 public synchronized StringObject delete(int offset,int len) { 489 490 if ((offset < 0) || (offset > count)) { 491 throw new StringIndexOutOfBoundsException (); 492 } 493 copyWhenShared(); 494 System.arraycopy(value, offset+len, value, offset, count-(offset+len)); 495 count -= len; 496 return this; 497 } 498 499 500 503 public synchronized StringObject replace(int offset,int len,String str) { 504 delete(offset,len); 505 insert(offset,str); 506 return this; 507 } 508 509 510 513 public synchronized StringObject replaceFirst(String oldstr,String newstr) { 514 int pos=indexOf(oldstr,0); 515 if (pos!=-1) { 516 delete(pos,oldstr.length()); 517 insert(pos,newstr); 518 } 519 return this; 520 } 521 522 523 526 public synchronized StringObject replace(String oldstr,String newstr) { 527 int strlen = oldstr.length(); 528 int pos=indexOf(oldstr,0,strlen); 529 while (pos!=-1) { 530 delete(pos,strlen); 531 insert(pos,newstr); 532 pos=indexOf(oldstr,pos+newstr.length(),strlen); 533 } 534 return this; 535 } 536 537 538 542 public synchronized StringObject replace(String oldstart,String oldend,String newstart, String newend) { 543 int pos2; 544 int pos=indexOf(oldstart,0); 545 546 while (pos!=-1) { 547 delete(pos,oldstart.length()); 548 insert(pos,newstart); 549 pos2=indexOf(oldend,pos+newstart.length()); 550 if (pos2!=-1) { 551 delete(pos2,oldend.length()); 552 insert(pos2,newend); 553 } 554 pos=indexOf(oldstart,pos2+newend.length()); 555 } 556 return this; 557 } 558 559 562 public synchronized StringObject insertLinks(String oldstart,String oldend,String newstart,String newend, String startend) { 563 int pos2; 564 int pos=indexOf(oldstart,0); 565 String link=""; 566 while (pos!=-1) { 567 delete(pos,oldstart.length()); 568 insert(pos,newstart); 569 pos2=indexOf(oldend,pos+newstart.length()); 570 if (pos2!=-1) { 571 link=new String (value,pos+newstart.length(),pos2-(pos+newstart.length())); 572 delete(pos2,oldend.length()); 573 insert(pos2,newend+link+startend); 574 } 575 pos=indexOf(oldstart,pos2+newend.length()); 576 } 577 return this; 578 } 579 580 public int indexOf(String str) { 581 return indexOf(str,0); 582 } 583 584 public int indexOf(String str, int fromIndex) { 585 return indexOf(str, fromIndex, str.length()); 586 } 587 588 private int indexOf(String str, int fromIndex, int strlen) { 589 char v1[] = value; 590 char v2[] = str.toCharArray(); 591 int max = (count - strlen); 592 test: 593 for (int i = ((fromIndex < 0) ? 0 : fromIndex); i <= max ; i++) { 594 int n = strlen; 595 int j = i; 596 int k = 0; 597 while (n-- != 0) { 598 if (v1[j++] != v2[k++]) { 599 continue test; 600 } 601 } 602 return i; 603 } 604 return -1; 605 } 606 607 608 610 public byte[] getBytes() { 611 return toString().getBytes(); 612 } 613 } 614 | Popular Tags |