1 22 package org.jboss.util; 23 24 import java.io.Serializable ; 25 import java.util.Arrays ; 26 27 34 public class JBossStringBuilder implements Serializable , CharSequence 35 { 36 37 private static final long serialVersionUID = 1874946609763446794L; 38 39 40 protected char[] chars; 41 42 43 protected int pos; 44 45 49 public JBossStringBuilder() 50 { 51 this(16); 52 } 53 54 59 public JBossStringBuilder(int capacity) 60 { 61 chars = new char[capacity]; 62 } 63 64 65 71 public JBossStringBuilder(String string) 72 { 73 this(string.length() + 16); 74 append(string); 75 } 76 77 83 public JBossStringBuilder(CharSequence charSequence) 84 { 85 this(charSequence.length() + 16); 86 append(charSequence); 87 } 88 89 94 public JBossStringBuilder(char[] ch) 95 { 96 this(ch, 0, ch.length); 97 } 98 99 106 public JBossStringBuilder(char[] ch, int start, int length) 107 { 108 this(length + 16); 109 append(ch, start, length); 110 } 111 112 public JBossStringBuilder append(Object object) 113 { 114 return append(String.valueOf(object)); 115 } 116 117 public JBossStringBuilder append(String string) 118 { 119 if (string == null) 120 string = "null"; 121 122 int length = string.length(); 123 if (length == 0) 124 return this; 125 126 int afterAppend = pos + length; 127 if (afterAppend > chars.length) 128 expandCapacity(afterAppend); 129 130 string.getChars(0, length, chars, pos); 131 pos = afterAppend; 132 return this; 133 } 134 135 public JBossStringBuilder append(StringBuffer buffer) 136 { 137 if (buffer == null) 138 return append("null"); 139 140 int length = buffer.length(); 141 if (length == 0) 142 return this; 143 144 int afterAppend = pos + length; 145 if (afterAppend > chars.length) 146 expandCapacity(afterAppend); 147 148 buffer.getChars(0, length, chars, pos); 149 pos = afterAppend; 150 return this; 151 } 152 153 public JBossStringBuilder append(CharSequence charSequence) 154 { 155 if (charSequence == null) 156 return append("null"); 157 158 int length = charSequence.length(); 159 if (length == 0) 160 return this; 161 162 return append(charSequence, 0, charSequence.length()); 163 } 164 165 public JBossStringBuilder append(CharSequence charSequence, int start, int end) 166 { 167 if (charSequence == null) 168 return append("null"); 169 170 if (start < 0 || end < 0 || start > end || start > charSequence.length()) 171 throw new IndexOutOfBoundsException ("Invalid start=" + start + " end=" + end + " length=" + charSequence.length()); 172 173 int length = end - start; 174 if (length == 0) 175 return this; 176 177 int afterAppend = pos + length; 178 if (afterAppend > chars.length) 179 expandCapacity(afterAppend); 180 181 for (int i = start; i < end; ++i) 182 chars[pos++] = charSequence.charAt(i); 183 pos = afterAppend; 184 return this; 185 } 186 187 public JBossStringBuilder append(char[] array) 188 { 189 if (array == null) 190 return append("null"); 191 192 if (array.length == 0) 193 return this; 194 195 String string = String.valueOf(array); 196 return append(string); 197 } 198 199 public JBossStringBuilder append(char[] array, int offset, int length) 200 { 201 if (array == null) 202 return append("null"); 203 204 int arrayLength = array.length; 205 if (offset < 0 || length < 0 || offset + length > arrayLength) 206 throw new IndexOutOfBoundsException ("Invalid offset=" + offset + " length=" + length + " array.length=" + arrayLength); 207 208 if (length == 0 || arrayLength == 0) 209 return this; 210 211 String string = String.valueOf(array, offset, length); 212 return append(string); 213 } 214 215 public JBossStringBuilder append(boolean primitive) 216 { 217 String string = String.valueOf(primitive); 218 return append(string); 219 } 220 221 public JBossStringBuilder append(char primitive) 222 { 223 String string = String.valueOf(primitive); 224 return append(string); 225 } 226 227 public JBossStringBuilder append(int primitive) 228 { 229 String string = String.valueOf(primitive); 230 return append(string); 231 } 232 233 public JBossStringBuilder append(long primitive) 234 { 235 String string = String.valueOf(primitive); 236 return append(string); 237 } 238 239 public JBossStringBuilder append(float primitive) 240 { 241 String string = String.valueOf(primitive); 242 return append(string); 243 } 244 245 public JBossStringBuilder append(double primitive) 246 { 247 String string = String.valueOf(primitive); 248 return append(string); 249 } 250 251 public JBossStringBuilder delete(int start, int end) 252 { 253 if (start < 0 || start > pos || start > end || end > pos) 254 throw new IndexOutOfBoundsException ("Invalid start=" + start + " end=" + end + " length=" + pos); 255 256 if (start == end) 257 return this; 258 259 int removed = end - start; 260 System.arraycopy(chars, start + removed, chars, start, pos - end); 261 pos -= removed; 262 return this; 263 } 264 265 public JBossStringBuilder deleteCharAt(int index) 266 { 267 return delete(index, 1); 268 } 269 270 public JBossStringBuilder replace(int start, int end, String string) 271 { 272 delete(start, end); 273 return insert(start, string); 274 } 275 276 public JBossStringBuilder insert(int index, char[] string) 277 { 278 return insert(index, string, 0, string.length); 279 } 280 281 public JBossStringBuilder insert(int index, char[] string, int offset, int len) 282 { 283 int stringLength = string.length; 284 if (index < 0 || index > pos || offset < 0 || len < 0 || (offset + len) > string.length) 285 throw new IndexOutOfBoundsException ("Invalid index=" + index + " offset=" + offset + " len=" + len + " string.length=" + stringLength + " length=" + pos); 286 287 if (len == 0) 288 return this; 289 290 int afterAppend = pos + len; 291 if (afterAppend > chars.length) 292 expandCapacity(afterAppend); 293 294 System.arraycopy(chars, index, chars, index + stringLength, pos - index); 295 System.arraycopy(string, offset, chars, index, len); 296 pos = afterAppend; 297 return this; 298 } 299 300 public JBossStringBuilder insert(int offset, Object object) 301 { 302 if (object == null) 303 return insert(offset, "null"); 304 else 305 return insert(offset, String.valueOf(object)); 306 } 307 308 public JBossStringBuilder insert(int offset, String string) 309 { 310 if (offset < 0 || offset > pos) 311 throw new IndexOutOfBoundsException ("Invalid offset=" + offset + " length=" + pos); 312 313 if (string == null) 314 string = "null"; 315 316 int stringLength = string.length(); 317 318 int afterAppend = pos + stringLength; 319 if (afterAppend > chars.length) 320 expandCapacity(afterAppend); 321 322 System.arraycopy(chars, offset, chars, offset + stringLength, pos - offset); 323 string.getChars(0, stringLength, chars, offset); 324 pos = afterAppend; 325 return this; 326 } 327 328 public JBossStringBuilder insert(int offset, CharSequence charSequence) 329 { 330 if (charSequence == null) 331 return insert(offset, "null"); 332 else 333 return insert(offset, charSequence, 0, charSequence.length()); 334 } 335 336 public JBossStringBuilder insert(int offset, CharSequence charSequence, int start, int end) 337 { 338 if (charSequence == null) 339 charSequence = "null"; 340 341 int sequenceLength = charSequence.length(); 342 if (offset < 0 || offset > pos || start < 0 || end < 0 || start > sequenceLength || end > sequenceLength || start > end) 343 throw new IndexOutOfBoundsException ("Invalid offset=" + offset + " start=" + start + " end=" + end + " sequence.length()=" + sequenceLength + " length=" + pos); 344 345 int len = end - start; 346 if (len == 0) 347 return this; 348 349 int afterAppend = pos + len; 350 if (afterAppend > chars.length) 351 expandCapacity(afterAppend); 352 353 System.arraycopy(chars, offset, chars, offset + sequenceLength, pos - offset); 354 for (int i = start; i < end; ++i) 355 chars[offset++] = charSequence.charAt(i); 356 pos = afterAppend; 357 return this; 358 } 359 360 public JBossStringBuilder insert(int offset, boolean primitive) 361 { 362 return insert(offset, String.valueOf(primitive)); 363 } 364 365 public JBossStringBuilder insert(int offset, char primitive) 366 { 367 return insert(offset, String.valueOf(primitive)); 368 } 369 370 public JBossStringBuilder insert(int offset, int primitive) 371 { 372 return insert(offset, String.valueOf(primitive)); 373 } 374 375 public JBossStringBuilder insert(int offset, long primitive) 376 { 377 return insert(offset, String.valueOf(primitive)); 378 } 379 380 public JBossStringBuilder insert(int offset, float primitive) 381 { 382 return insert(offset, String.valueOf(primitive)); 383 } 384 385 public JBossStringBuilder insert(int offset, double primitive) 386 { 387 return insert(offset, String.valueOf(primitive)); 388 } 389 390 public int indexOf(String string) 391 { 392 return indexOf(string, 0); 393 } 394 395 public int indexOf(String string, int fromIndex) 396 { 397 return toString().indexOf(string, fromIndex); 399 } 400 401 public int indexOf(char ch) 402 { 403 return indexOf(ch, 0); 404 } 405 406 public int indexOf(char ch, int fromIndex) 407 { 408 return toString().indexOf(ch, fromIndex); 410 } 411 412 public int lastIndexOf(String string) 413 { 414 return lastIndexOf(string, pos); 415 } 416 417 public int lastIndexOf(String string, int fromIndex) 418 { 419 return toString().lastIndexOf(string, fromIndex); 421 } 422 423 public int lastIndexOf(char ch) 424 { 425 return lastIndexOf(ch, pos); 426 } 427 428 public int lastIndexOf(char ch, int fromIndex) 429 { 430 return toString().lastIndexOf(ch, fromIndex); 432 } 433 434 public JBossStringBuilder reverse() 435 { 436 char[] tmp = new char[pos]; 437 for(int n = 0; n < pos; n ++) 438 tmp[n] = chars[pos-n-1]; 439 chars = tmp; 440 return this; 441 } 442 443 public String toString() 444 { 445 return new String (chars, 0, pos); 446 } 447 448 public int length() 449 { 450 return pos; 451 } 452 453 public int capacity() 454 { 455 return chars.length; 456 } 457 458 public void ensureCapacity(int minimum) 459 { 460 if (minimum < 0 || minimum < chars.length) 461 return; 462 expandCapacity(minimum); 463 } 464 465 public void trimToSize() 466 { 467 char[] trimmed = new char[pos]; 468 System.arraycopy(chars, 0, trimmed, 0, pos); 469 chars = trimmed; 470 } 471 472 public void setLength(int newLength) 473 { 474 if (newLength < 0) 475 throw new StringIndexOutOfBoundsException (newLength); 476 if (newLength > chars.length) 477 expandCapacity(newLength); 478 Arrays.fill(chars, newLength, chars.length, '\0'); 479 pos = newLength; 480 } 481 482 public char charAt(int index) 483 { 484 return chars[index]; 485 } 486 487 public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 488 { 489 if (srcBegin < 0 || dstBegin < 0 || srcBegin > srcEnd || srcEnd > pos || (dstBegin + srcEnd - srcBegin) > dst.length) 490 throw new IndexOutOfBoundsException ("Invalid srcBegin=" + srcBegin + " srcEnd=" + srcEnd + " dstBegin=" + dstBegin + " dst.length=" + dst.length + " length=" + pos); 491 492 int len = srcEnd - srcBegin; 493 if (len == 0) 494 return; 495 496 System.arraycopy(chars, srcBegin, dst, dstBegin, len); 497 } 498 499 public void setCharAt(int index, char ch) 500 { 501 if (index < 0 || index > pos) 502 throw new IndexOutOfBoundsException ("Invalid index=" + index + " length=" + pos); 503 504 chars[index] = ch; 505 } 506 507 public String substring(int start) 508 { 509 return substring(start, pos); 510 } 511 512 public CharSequence subSequence(int start, int end) 513 { 514 return substring(start, end); 515 } 516 517 public String substring(int start, int end) 518 { 519 if (start < 0 || end < 0 || start > end || end > pos) 520 throw new IndexOutOfBoundsException ("Invalid start=" + start + " end=" + end + " length=" + pos); 521 522 return new String (chars, start, end - start); 523 } 524 525 531 protected void expandCapacity(int minimum) 532 { 533 int newSize = chars.length * 2; 534 if (minimum > newSize) 535 newSize = minimum; 536 537 char[] newChars = new char[newSize]; 538 System.arraycopy(chars, 0, newChars, 0, pos); 539 chars = newChars; 540 } 541 } 542 | Popular Tags |