static final class BetterStringBuffer extends AbstractStringBuilder { private transient String toStringCache; *() { super(16); } *(int capacity) { super(capacity); } *(String str) { super(str.length() + 16); append(str); } public StringBuffer(CharSequence seq) { this(seq.length() + 16); append(seq); } /** * Compares two {@code StringBuffer} instances lexicographically. This method * follows the same rules for lexicographical comparison as defined in the * {@linkplain java.lang.CharSequence#compare(java.lang.CharSequence, * java.lang.CharSequence) CharSequence.compare(this, another)} method. * *
* For finer-grained, locale-sensitive String comparison, refer to * {@link java.text.Collator}. * * @implNote * This method synchronizes on {@code this}, the current object, but not * {@code StringBuffer another} with which {@code this StringBuffer} is compared. * * @param another the {@code StringBuffer} to be compared with * * @return the value {@code 0} if this {@code StringBuffer} contains the same * character sequence as that of the argument {@code StringBuffer}; a negative integer * if this {@code StringBuffer} is lexicographically less than the * {@code StringBuffer} argument; or a positive integer if this {@code StringBuffer} * is lexicographically greater than the {@code StringBuffer} argument. * * @since 11 */ @Override public synchronized int compareTo(StringBuffer another) { return super.compareTo(another); } @Override public synchronized int length() { return count; } @Override public synchronized int capacity() { return super.capacity(); } @Override public synchronized void ensureCapacity(int minimumCapacity) { super.ensureCapacity(minimumCapacity); } /** * @since 1.5 */ @Override public synchronized void trimToSize() { super.trimToSize(); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ @Override public synchronized void setLength(int newLength) { toStringCache = null; super.setLength(newLength); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ @Override public synchronized char charAt(int index) { return super.charAt(index); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ @Override public synchronized int codePointAt(int index) { return super.codePointAt(index); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ @Override public synchronized int codePointBefore(int index) { return super.codePointBefore(index); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ @Override public synchronized int codePointCount(int beginIndex, int endIndex) { return super.codePointCount(beginIndex, endIndex); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ @Override public synchronized int offsetByCodePoints(int index, int codePointOffset) { return super.offsetByCodePoints(index, codePointOffset); } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { super.getChars(srcBegin, srcEnd, dst, dstBegin); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @see #length() */ @Override public synchronized void setCharAt(int index, char ch) { toStringCache = null; super.setCharAt(index, ch); } @Override public synchronized StringBuffer append(Object obj) { toStringCache = null; super.append(String.valueOf(obj)); return this; } @Override @HotSpotIntrinsicCandidate public synchronized StringBuffer append(String str) { toStringCache = null; super.append(str); return this; } /** * Appends the specified {@code StringBuffer} to this sequence. *
* The characters of the {@code StringBuffer} argument are appended, * in order, to the contents of this {@code StringBuffer}, increasing the * length of this {@code StringBuffer} by the length of the argument. * If {@code sb} is {@code null}, then the four characters * {@code "null"} are appended to this {@code StringBuffer}. *
* Let n be the length of the old character sequence, the one * contained in the {@code StringBuffer} just prior to execution of the * {@code append} method. Then the character at index k in * the new character sequence is equal to the character at index k * in the old character sequence, if k is less than n; * otherwise, it is equal to the character at index k-n in the * argument {@code sb}. *
* This method synchronizes on {@code this}, the destination * object, but does not synchronize on the source ({@code sb}). * * @param sb the {@code StringBuffer} to append. * @return a reference to this object. * @since 1.4 */ public synchronized StringBuffer append(StringBuffer sb) { toStringCache = null; super.append(sb); return this; } /** * @since 1.8 */ @Override synchronized StringBuffer append(AbstractStringBuilder asb) { toStringCache = null; super.append(asb); return this; } /** * Appends the specified {@code CharSequence} to this * sequence. *
* The characters of the {@code CharSequence} argument are appended, * in order, increasing the length of this sequence by the length of the * argument. * *
The result of this method is exactly the same as if it were an * invocation of this.append(s, 0, s.length()); * *
This method synchronizes on {@code this}, the destination * object, but does not synchronize on the source ({@code s}). * *
If {@code s} is {@code null}, then the four characters * {@code "null"} are appended. * * @param s the {@code CharSequence} to append. * @return a reference to this object. * @since 1.5 */ @Override public synchronized StringBuffer append(CharSequence s) { toStringCache = null; super.append(s); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ @Override public synchronized StringBuffer append(CharSequence s, int start, int end) { toStringCache = null; super.append(s, start, end); return this; } @Override public synchronized StringBuffer append(char[] str) { toStringCache = null; super.append(str); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized StringBuffer append(char[] str, int offset, int len) { toStringCache = null; super.append(str, offset, len); return this; } @Override public synchronized StringBuffer append(boolean b) { toStringCache = null; super.append(b); return this; } @Override @HotSpotIntrinsicCandidate public synchronized StringBuffer append(char c) { toStringCache = null; super.append(c); return this; } @Override @HotSpotIntrinsicCandidate public synchronized StringBuffer append(int i) { toStringCache = null; super.append(i); return this; } public synchronized StringBuffer appendCodePoint(int codePoint) { toStringCache = null; super.appendCodePoint(codePoint); return this; } @Override public synchronized StringBuffer append(long lng) { toStringCache = null; super.append(lng); return this; } @Override public synchronized StringBuffer append(float f) { toStringCache = null; super.append(f); return this; } @Override public synchronized StringBuffer append(double d) { toStringCache = null; super.append(d); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized StringBuffer delete(int start, int end) { toStringCache = null; super.delete(start, end); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized StringBuffer deleteCharAt(int index) { toStringCache = null; super.deleteCharAt(index); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized StringBuffer replace(int start, int end, String str) { toStringCache = null; super.replace(start, end, str); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized String substring(int start) { return substring(start, count); } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.4 */ @Override public synchronized CharSequence subSequence(int start, int end) { return super.substring(start, end); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized String substring(int start, int end) { return super.substring(start, end); } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} * @since 1.2 */ @Override public synchronized StringBuffer insert(int index, char[] str, int offset, int len) { toStringCache = null; super.insert(index, str, offset, len); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized StringBuffer insert(int offset, Object obj) { toStringCache = null; super.insert(offset, String.valueOf(obj)); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized StringBuffer insert(int offset, String str) { toStringCache = null; super.insert(offset, str); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized StringBuffer insert(int offset, char[] str) { toStringCache = null; super.insert(offset, str); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ @Override public StringBuffer insert(int dstOffset, CharSequence s) { // Note, synchronization achieved via invocations of other StringBuffer methods // after narrowing of s to specific type // Ditto for toStringCache clearing super.insert(dstOffset, s); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} * @since 1.5 */ @Override public synchronized StringBuffer insert(int dstOffset, CharSequence s, int start, int end) { toStringCache = null; super.insert(dstOffset, s, start, end); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public StringBuffer insert(int offset, boolean b) { // Note, synchronization achieved via invocation of StringBuffer insert(int, String) // after conversion of b to String by super class method // Ditto for toStringCache clearing super.insert(offset, b); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public synchronized StringBuffer insert(int offset, char c) { toStringCache = null; super.insert(offset, c); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public StringBuffer insert(int offset, int i) { // Note, synchronization achieved via invocation of StringBuffer insert(int, String) // after conversion of i to String by super class method // Ditto for toStringCache clearing super.insert(offset, i); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public StringBuffer insert(int offset, long l) { // Note, synchronization achieved via invocation of StringBuffer insert(int, String) // after conversion of l to String by super class method // Ditto for toStringCache clearing super.insert(offset, l); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public StringBuffer insert(int offset, float f) { // Note, synchronization achieved via invocation of StringBuffer insert(int, String) // after conversion of f to String by super class method // Ditto for toStringCache clearing super.insert(offset, f); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public StringBuffer insert(int offset, double d) { // Note, synchronization achieved via invocation of StringBuffer insert(int, String) // after conversion of d to String by super class method // Ditto for toStringCache clearing super.insert(offset, d); return this; } /** * @since 1.4 */ @Override public int indexOf(String str) { // Note, synchronization achieved via invocations of other StringBuffer methods return super.indexOf(str); } /** * @since 1.4 */ @Override public synchronized int indexOf(String str, int fromIndex) { return super.indexOf(str, fromIndex); } /** * @since 1.4 */ @Override public int lastIndexOf(String str) { // Note, synchronization achieved via invocations of other StringBuffer methods return lastIndexOf(str, count); } public synchronized int lastIndexOf(String str, int fromIndex) { return super.lastIndexOf(str, fromIndex); } public synchronized StringBuffer reverse() { toStringCache = null; super.reverse(); return this; } public synchronized String toString() { if (toStringCache == null) { return toStringCache = isLatin1() ? StringLatin1.newString(value, 0, count) : StringUTF16.newString(value, 0, count); } return new String(toStringCache); } synchronized void getBytes(byte dst[], int dstBegin, byte coder) { super.getBytes(dst, dstBegin, coder); } }