53
54 /**
55 * This no-arg constructor is necessary for serialization of subclasses.
56 */
57 AbstractStringBuilder() {
58 }
59
60 /**
61 * Creates an AbstractStringBuilder of the specified capacity.
62 */
63 AbstractStringBuilder(int capacity) {
64 value = new char[capacity];
65 }
66
67 /**
68 * Returns the length (character count).
69 *
70 * @return the length of the sequence of characters currently
71 * represented by this object
72 */
73 public int length() {
74 return count;
75 }
76
77 /**
78 * Returns the current capacity. The capacity is the amount of storage
79 * available for newly inserted characters, beyond which an allocation
80 * will occur.
81 *
82 * @return the current capacity
83 */
84 public int capacity() {
85 return value.length;
86 }
87
88 /**
89 * Ensures that the capacity is at least equal to the specified minimum.
90 * If the current capacity is less than the argument, then a new internal
91 * array is allocated with greater capacity. The new capacity is the
92 * larger of:
183 }
184 }
185
186 /**
187 * Returns the {@code char} value in this sequence at the specified index.
188 * The first {@code char} value is at index {@code 0}, the next at index
189 * {@code 1}, and so on, as in array indexing.
190 * <p>
191 * The index argument must be greater than or equal to
192 * {@code 0}, and less than the length of this sequence.
193 *
194 * <p>If the {@code char} value specified by the index is a
195 * <a href="Character.html#unicode">surrogate</a>, the surrogate
196 * value is returned.
197 *
198 * @param index the index of the desired {@code char} value.
199 * @return the {@code char} value at the specified index.
200 * @throws IndexOutOfBoundsException if {@code index} is
201 * negative or greater than or equal to {@code length()}.
202 */
203 public char charAt(int index) {
204 if ((index < 0) || (index >= count))
205 throw new StringIndexOutOfBoundsException(index);
206 return value[index];
207 }
208
209 /**
210 * Returns the character (Unicode code point) at the specified
211 * index. The index refers to {@code char} values
212 * (Unicode code units) and ranges from {@code 0} to
213 * {@link #length()}{@code - 1}.
214 *
215 * <p> If the {@code char} value specified at the given index
216 * is in the high-surrogate range, the following index is less
217 * than the length of this sequence, and the
218 * {@code char} value at the following index is in the
219 * low-surrogate range, then the supplementary code point
220 * corresponding to this surrogate pair is returned. Otherwise,
221 * the {@code char} value at the given index is returned.
222 *
414 public AbstractStringBuilder append(String str) {
415 if (str == null) str = "null";
416 int len = str.length();
417 ensureCapacityInternal(count + len);
418 str.getChars(0, len, value, count);
419 count += len;
420 return this;
421 }
422
423 // Documentation in subclasses because of synchro difference
424 public AbstractStringBuilder append(StringBuffer sb) {
425 if (sb == null)
426 return append("null");
427 int len = sb.length();
428 ensureCapacityInternal(count + len);
429 sb.getChars(0, len, value, count);
430 count += len;
431 return this;
432 }
433
434 // Documentation in subclasses because of synchro difference
435 public AbstractStringBuilder append(CharSequence s) {
436 if (s == null)
437 s = "null";
438 if (s instanceof String)
439 return this.append((String)s);
440 if (s instanceof StringBuffer)
441 return this.append((StringBuffer)s);
442 return this.append(s, 0, s.length());
443 }
444
445 /**
446 * Appends a subsequence of the specified {@code CharSequence} to this
447 * sequence.
448 * <p>
449 * Characters of the argument {@code s}, starting at
450 * index {@code start}, are appended, in order, to the contents of
451 * this sequence up to the (exclusive) index {@code end}. The length
452 * of this sequence is increased by the value of {@code end - start}.
453 * <p>
454 * Let <i>n</i> be the length of this character sequence just prior to
455 * execution of the {@code append} method. Then the character at
456 * index <i>k</i> in this character sequence becomes equal to the
457 * character at index <i>k</i> in this sequence, if <i>k</i> is less than
458 * <i>n</i>; otherwise, it is equal to the character at index
459 * <i>k+start-n</i> in the argument {@code s}.
460 * <p>
461 * If {@code s} is {@code null}, then this method appends
462 * characters as if the s parameter was a sequence containing the four
463 * characters {@code "null"}.
464 *
465 * @param s the sequence to append.
466 * @param start the starting index of the subsequence to be appended.
467 * @param end the end index of the subsequence to be appended.
468 * @return a reference to this object.
469 * @throws IndexOutOfBoundsException if
470 * {@code start} is negative, or
471 * {@code start} is greater than {@code end} or
472 * {@code end} is greater than {@code s.length()}
473 */
474 public AbstractStringBuilder append(CharSequence s, int start, int end) {
475 if (s == null)
476 s = "null";
477 if ((start < 0) || (start > end) || (end > s.length()))
478 throw new IndexOutOfBoundsException(
479 "start " + start + ", end " + end + ", s.length() "
480 + s.length());
481 int len = end - start;
482 ensureCapacityInternal(count + len);
483 for (int i = start, j = count; i < end; i++, j++)
484 value[j] = s.charAt(i);
485 count += len;
486 return this;
487 }
488
489 /**
490 * Appends the string representation of the {@code char} array
491 * argument to this sequence.
492 * <p>
493 * The characters of the array argument are appended, in order, to
568 value[count++] = 'e';
569 }
570 return this;
571 }
572
573 /**
574 * Appends the string representation of the {@code char}
575 * argument to this sequence.
576 * <p>
577 * The argument is appended to the contents of this sequence.
578 * The length of this sequence increases by {@code 1}.
579 * <p>
580 * The overall effect is exactly as if the argument were converted
581 * to a string by the method {@link String#valueOf(char)},
582 * and the character in that string were then
583 * {@link #append(String) appended} to this character sequence.
584 *
585 * @param c a {@code char}.
586 * @return a reference to this object.
587 */
588 public AbstractStringBuilder append(char c) {
589 ensureCapacityInternal(count + 1);
590 value[count++] = c;
591 return this;
592 }
593
594 /**
595 * Appends the string representation of the {@code int}
596 * argument to this sequence.
597 * <p>
598 * The overall effect is exactly as if the argument were converted
599 * to a string by the method {@link String#valueOf(int)},
600 * and the characters of that string were then
601 * {@link #append(String) appended} to this character sequence.
602 *
603 * @param i an {@code int}.
604 * @return a reference to this object.
605 */
606 public AbstractStringBuilder append(int i) {
607 if (i == Integer.MIN_VALUE) {
830 * sb.subSequence(begin, end)</pre></blockquote>
831 *
832 * behaves in exactly the same way as the invocation
833 *
834 * <blockquote><pre>
835 * sb.substring(begin, end)</pre></blockquote>
836 *
837 * This method is provided so that this class can
838 * implement the {@link CharSequence} interface. </p>
839 *
840 * @param start the start index, inclusive.
841 * @param end the end index, exclusive.
842 * @return the specified subsequence.
843 *
844 * @throws IndexOutOfBoundsException
845 * if <tt>start</tt> or <tt>end</tt> are negative,
846 * if <tt>end</tt> is greater than <tt>length()</tt>,
847 * or if <tt>start</tt> is greater than <tt>end</tt>
848 * @spec JSR-51
849 */
850 public CharSequence subSequence(int start, int end) {
851 return substring(start, end);
852 }
853
854 /**
855 * Returns a new {@code String} that contains a subsequence of
856 * characters currently contained in this sequence. The
857 * substring begins at the specified {@code start} and
858 * extends to the character at index {@code end - 1}.
859 *
860 * @param start The beginning index, inclusive.
861 * @param end The ending index, exclusive.
862 * @return The new string.
863 * @throws StringIndexOutOfBoundsException if {@code start}
864 * or {@code end} are negative or greater than
865 * {@code length()}, or {@code start} is
866 * greater than {@code end}.
867 */
868 public String substring(int start, int end) {
869 if (start < 0)
1380 if (Character.isHighSurrogate(c1)) {
1381 value[i++] = c1;
1382 value[i] = c2;
1383 }
1384 }
1385 }
1386 }
1387 return this;
1388 }
1389
1390 /**
1391 * Returns a string representing the data in this sequence.
1392 * A new {@code String} object is allocated and initialized to
1393 * contain the character sequence currently represented by this
1394 * object. This {@code String} is then returned. Subsequent
1395 * changes to this sequence do not affect the contents of the
1396 * {@code String}.
1397 *
1398 * @return a string representation of this sequence of characters.
1399 */
1400 public abstract String toString();
1401
1402 /**
1403 * Needed by <tt>String</tt> for the contentEquals method.
1404 */
1405 final char[] getValue() {
1406 return value;
1407 }
1408
1409 }
|
53
54 /**
55 * This no-arg constructor is necessary for serialization of subclasses.
56 */
57 AbstractStringBuilder() {
58 }
59
60 /**
61 * Creates an AbstractStringBuilder of the specified capacity.
62 */
63 AbstractStringBuilder(int capacity) {
64 value = new char[capacity];
65 }
66
67 /**
68 * Returns the length (character count).
69 *
70 * @return the length of the sequence of characters currently
71 * represented by this object
72 */
73 @Override
74 public int length() {
75 return count;
76 }
77
78 /**
79 * Returns the current capacity. The capacity is the amount of storage
80 * available for newly inserted characters, beyond which an allocation
81 * will occur.
82 *
83 * @return the current capacity
84 */
85 public int capacity() {
86 return value.length;
87 }
88
89 /**
90 * Ensures that the capacity is at least equal to the specified minimum.
91 * If the current capacity is less than the argument, then a new internal
92 * array is allocated with greater capacity. The new capacity is the
93 * larger of:
184 }
185 }
186
187 /**
188 * Returns the {@code char} value in this sequence at the specified index.
189 * The first {@code char} value is at index {@code 0}, the next at index
190 * {@code 1}, and so on, as in array indexing.
191 * <p>
192 * The index argument must be greater than or equal to
193 * {@code 0}, and less than the length of this sequence.
194 *
195 * <p>If the {@code char} value specified by the index is a
196 * <a href="Character.html#unicode">surrogate</a>, the surrogate
197 * value is returned.
198 *
199 * @param index the index of the desired {@code char} value.
200 * @return the {@code char} value at the specified index.
201 * @throws IndexOutOfBoundsException if {@code index} is
202 * negative or greater than or equal to {@code length()}.
203 */
204 @Override
205 public char charAt(int index) {
206 if ((index < 0) || (index >= count))
207 throw new StringIndexOutOfBoundsException(index);
208 return value[index];
209 }
210
211 /**
212 * Returns the character (Unicode code point) at the specified
213 * index. The index refers to {@code char} values
214 * (Unicode code units) and ranges from {@code 0} to
215 * {@link #length()}{@code - 1}.
216 *
217 * <p> If the {@code char} value specified at the given index
218 * is in the high-surrogate range, the following index is less
219 * than the length of this sequence, and the
220 * {@code char} value at the following index is in the
221 * low-surrogate range, then the supplementary code point
222 * corresponding to this surrogate pair is returned. Otherwise,
223 * the {@code char} value at the given index is returned.
224 *
416 public AbstractStringBuilder append(String str) {
417 if (str == null) str = "null";
418 int len = str.length();
419 ensureCapacityInternal(count + len);
420 str.getChars(0, len, value, count);
421 count += len;
422 return this;
423 }
424
425 // Documentation in subclasses because of synchro difference
426 public AbstractStringBuilder append(StringBuffer sb) {
427 if (sb == null)
428 return append("null");
429 int len = sb.length();
430 ensureCapacityInternal(count + len);
431 sb.getChars(0, len, value, count);
432 count += len;
433 return this;
434 }
435
436 /**
437 * @since 1.8
438 */
439 AbstractStringBuilder append(AbstractStringBuilder asb) {
440 if (asb == null)
441 return append("null");
442 int len = asb.length();
443 ensureCapacityInternal(count + len);
444 asb.getChars(0, len, value, count);
445 count += len;
446 return this;
447 }
448
449 // Documentation in subclasses because of synchro difference
450 @Override
451 public AbstractStringBuilder append(CharSequence s) {
452 if (s == null)
453 s = "null";
454 if (s instanceof String)
455 return this.append((String)s);
456 if (s instanceof AbstractStringBuilder)
457 return this.append((AbstractStringBuilder)s);
458
459 return this.append(s, 0, s.length());
460 }
461
462 /**
463 * Appends a subsequence of the specified {@code CharSequence} to this
464 * sequence.
465 * <p>
466 * Characters of the argument {@code s}, starting at
467 * index {@code start}, are appended, in order, to the contents of
468 * this sequence up to the (exclusive) index {@code end}. The length
469 * of this sequence is increased by the value of {@code end - start}.
470 * <p>
471 * Let <i>n</i> be the length of this character sequence just prior to
472 * execution of the {@code append} method. Then the character at
473 * index <i>k</i> in this character sequence becomes equal to the
474 * character at index <i>k</i> in this sequence, if <i>k</i> is less than
475 * <i>n</i>; otherwise, it is equal to the character at index
476 * <i>k+start-n</i> in the argument {@code s}.
477 * <p>
478 * If {@code s} is {@code null}, then this method appends
479 * characters as if the s parameter was a sequence containing the four
480 * characters {@code "null"}.
481 *
482 * @param s the sequence to append.
483 * @param start the starting index of the subsequence to be appended.
484 * @param end the end index of the subsequence to be appended.
485 * @return a reference to this object.
486 * @throws IndexOutOfBoundsException if
487 * {@code start} is negative, or
488 * {@code start} is greater than {@code end} or
489 * {@code end} is greater than {@code s.length()}
490 */
491 @Override
492 public AbstractStringBuilder append(CharSequence s, int start, int end) {
493 if (s == null)
494 s = "null";
495 if ((start < 0) || (start > end) || (end > s.length()))
496 throw new IndexOutOfBoundsException(
497 "start " + start + ", end " + end + ", s.length() "
498 + s.length());
499 int len = end - start;
500 ensureCapacityInternal(count + len);
501 for (int i = start, j = count; i < end; i++, j++)
502 value[j] = s.charAt(i);
503 count += len;
504 return this;
505 }
506
507 /**
508 * Appends the string representation of the {@code char} array
509 * argument to this sequence.
510 * <p>
511 * The characters of the array argument are appended, in order, to
586 value[count++] = 'e';
587 }
588 return this;
589 }
590
591 /**
592 * Appends the string representation of the {@code char}
593 * argument to this sequence.
594 * <p>
595 * The argument is appended to the contents of this sequence.
596 * The length of this sequence increases by {@code 1}.
597 * <p>
598 * The overall effect is exactly as if the argument were converted
599 * to a string by the method {@link String#valueOf(char)},
600 * and the character in that string were then
601 * {@link #append(String) appended} to this character sequence.
602 *
603 * @param c a {@code char}.
604 * @return a reference to this object.
605 */
606 @Override
607 public AbstractStringBuilder append(char c) {
608 ensureCapacityInternal(count + 1);
609 value[count++] = c;
610 return this;
611 }
612
613 /**
614 * Appends the string representation of the {@code int}
615 * argument to this sequence.
616 * <p>
617 * The overall effect is exactly as if the argument were converted
618 * to a string by the method {@link String#valueOf(int)},
619 * and the characters of that string were then
620 * {@link #append(String) appended} to this character sequence.
621 *
622 * @param i an {@code int}.
623 * @return a reference to this object.
624 */
625 public AbstractStringBuilder append(int i) {
626 if (i == Integer.MIN_VALUE) {
849 * sb.subSequence(begin, end)</pre></blockquote>
850 *
851 * behaves in exactly the same way as the invocation
852 *
853 * <blockquote><pre>
854 * sb.substring(begin, end)</pre></blockquote>
855 *
856 * This method is provided so that this class can
857 * implement the {@link CharSequence} interface. </p>
858 *
859 * @param start the start index, inclusive.
860 * @param end the end index, exclusive.
861 * @return the specified subsequence.
862 *
863 * @throws IndexOutOfBoundsException
864 * if <tt>start</tt> or <tt>end</tt> are negative,
865 * if <tt>end</tt> is greater than <tt>length()</tt>,
866 * or if <tt>start</tt> is greater than <tt>end</tt>
867 * @spec JSR-51
868 */
869 @Override
870 public CharSequence subSequence(int start, int end) {
871 return substring(start, end);
872 }
873
874 /**
875 * Returns a new {@code String} that contains a subsequence of
876 * characters currently contained in this sequence. The
877 * substring begins at the specified {@code start} and
878 * extends to the character at index {@code end - 1}.
879 *
880 * @param start The beginning index, inclusive.
881 * @param end The ending index, exclusive.
882 * @return The new string.
883 * @throws StringIndexOutOfBoundsException if {@code start}
884 * or {@code end} are negative or greater than
885 * {@code length()}, or {@code start} is
886 * greater than {@code end}.
887 */
888 public String substring(int start, int end) {
889 if (start < 0)
1400 if (Character.isHighSurrogate(c1)) {
1401 value[i++] = c1;
1402 value[i] = c2;
1403 }
1404 }
1405 }
1406 }
1407 return this;
1408 }
1409
1410 /**
1411 * Returns a string representing the data in this sequence.
1412 * A new {@code String} object is allocated and initialized to
1413 * contain the character sequence currently represented by this
1414 * object. This {@code String} is then returned. Subsequent
1415 * changes to this sequence do not affect the contents of the
1416 * {@code String}.
1417 *
1418 * @return a string representation of this sequence of characters.
1419 */
1420 @Override
1421 public abstract String toString();
1422
1423 /**
1424 * Needed by <tt>String</tt> for the contentEquals method.
1425 */
1426 final char[] getValue() {
1427 return value;
1428 }
1429
1430 }
|