< prev index next >
src/java.base/share/classes/java/lang/ThreadLocalStringBuilder.java
Print this page
*** 26,117 ****
package java.lang;
/**
* A mutable sequence of characters. This class provides an API compatible
! * with {@code StringBuffer}, but with no guarantee of synchronization.
! * This class is designed for use as a drop-in replacement for
! * {@code StringBuffer} in places where the string buffer was being
! * used by a single thread (as is generally the case). Where possible,
! * it is recommended that this class be used in preference to
! * {@code StringBuffer} as it will be faster under most implementations.
*
! * <p>The principal operations on a {@code StringBuilder} are the
! * {@code append} and {@code insert} methods, which are
! * overloaded so as to accept data of any type. Each effectively
! * converts a given datum to a string and then appends or inserts the
! * characters of that string to the string builder. The
! * {@code append} method always adds these characters at the end
! * of the builder; the {@code insert} method adds the characters at
! * a specified point.
! * <p>
! * For example, if {@code z} refers to a string builder object
! * whose current contents are "{@code start}", then
! * the method call {@code z.append("le")} would cause the string
! * builder to contain "{@code startle}", whereas
! * {@code z.insert(4, "le")} would alter the string builder to
! * contain "{@code starlet}".
! * <p>
! * In general, if sb refers to an instance of a {@code StringBuilder},
! * then {@code sb.append(x)} has the same effect as
! * {@code sb.insert(sb.length(), x)}.
! * <p>
! * Every string builder has a capacity. As long as the length of the
! * character sequence contained in the string builder does not exceed
! * the capacity, it is not necessary to allocate a new internal
! * buffer. If the internal buffer overflows, it is automatically made larger.
! *
! * <p>Instances of {@code StringBuilder} are not safe for
! * use by multiple threads. If such synchronization is required then it is
! * recommended that {@link java.lang.StringBuffer} be used.
! *
! * <p>Unless otherwise noted, passing a {@code null} argument to a constructor
! * or method in this class will cause a {@link NullPointerException} to be
! * thrown.
! *
! * @author Michael McCloskey
! * @see java.lang.StringBuffer
! * @see java.lang.String
! * @since 1.5
*/
! public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
-
/** use serialVersionUID for interoperability */
! static final long serialVersionUID = 4383685877147921099L;
/**
* Constructs a string builder with no characters in it and an
* initial capacity of 16 characters.
*/
! public StringBuilder() {
super(16);
}
/**
* Constructs a string builder with no characters in it and an
* initial capacity specified by the {@code capacity} argument.
*
* @param capacity the initial capacity.
* @throws NegativeArraySizeException if the {@code capacity}
* argument is less than {@code 0}.
*/
! public StringBuilder(int capacity) {
super(capacity);
}
/**
* Constructs a string builder initialized to the contents of the
* specified string. The initial capacity of the string builder is
* {@code 16} plus the length of the string argument.
*
* @param str the initial contents of the buffer.
*/
! public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
/**
* Constructs a string builder that contains the same characters
--- 26,96 ----
package java.lang;
/**
* A mutable sequence of characters. This class provides an API compatible
! * with {@code StringBuilder}, but with some additional constraints:
! * <ul>
! * <li>mutating methods can only be invoked in the same thread that constructed
! * the instance. Attempts to invoke them in other threads throw
! * {@code IllegalStateException}
! * </li>
! * <li>once {@link #toString()} has been called (in constructing thread),
! * no mutating methods can be invoked any more, not even in constructing thread.
! * Attempts to invoke them throw {@code IllegalStateException}
! * </li>
! * </ul>
! * <p>This enables optimization where the {@code String} constructed
! * as the final act can reference the character array that was used for building
! * and no additional copying is needed. This optimization is only effective if
! * the {@link #length()} == {@link #capacity()} when {@code toString()} is called.
*
! * @see StringBuilder
! * @since 1.9
*/
! public final class ThreadLocalStringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence
{
/** use serialVersionUID for interoperability */
! static final long serialVersionUID = 1L;
!
! /** the construction thread until toString() is called, then the String */
! private transient Object constructionThreadOrString;
/**
* Constructs a string builder with no characters in it and an
* initial capacity of 16 characters.
*/
! public ThreadLocalStringBuilder() {
super(16);
+ constructionThreadOrString = Thread.currentThread();
}
/**
* Constructs a string builder with no characters in it and an
* initial capacity specified by the {@code capacity} argument.
*
* @param capacity the initial capacity.
* @throws NegativeArraySizeException if the {@code capacity}
* argument is less than {@code 0}.
*/
! public ThreadLocalStringBuilder(int capacity) {
super(capacity);
+ constructionThreadOrString = Thread.currentThread();
}
/**
* Constructs a string builder initialized to the contents of the
* specified string. The initial capacity of the string builder is
* {@code 16} plus the length of the string argument.
*
* @param str the initial contents of the buffer.
*/
! public ThreadLocalStringBuilder(String str) {
super(str.length() + 16);
+ constructionThreadOrString = Thread.currentThread();
append(str);
}
/**
* Constructs a string builder that contains the same characters
*** 119,140 ****
* the string builder is {@code 16} plus the length of the
* {@code CharSequence} argument.
*
* @param seq the sequence to copy.
*/
! public StringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
@Override
! public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
@Override
! public StringBuilder append(String str) {
super.append(str);
return this;
}
/**
--- 98,143 ----
* the string builder is {@code 16} plus the length of the
* {@code CharSequence} argument.
*
* @param seq the sequence to copy.
*/
! public ThreadLocalStringBuilder(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
+ /**
+ * @throws IllegalStateException if not invoked in thread that constructed
+ * this instance or if toString() has already been called.
+ */
+ private void checkThread() {
+ Object o = constructionThreadOrString;
+ if (o != Thread.currentThread()) {
+ throw new IllegalStateException((o instanceof String)
+ ? "toString() has already been called"
+ : "Illegal mutating thread"
+ );
+ }
+ }
+
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(Object obj) {
! checkThread();
return append(String.valueOf(obj));
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(String str) {
! checkThread();
super.append(str);
return this;
}
/**
*** 153,412 ****
* <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
* in the argument {@code sb}.
*
* @param sb the {@code StringBuffer} to append.
* @return a reference to this object.
*/
! public StringBuilder append(StringBuffer sb) {
super.append(sb);
return this;
}
@Override
! public StringBuilder append(CharSequence s) {
super.append(s);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder append(CharSequence s, int start, int end) {
super.append(s, start, end);
return this;
}
@Override
! public StringBuilder append(char[] str) {
super.append(str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder append(char[] str, int offset, int len) {
super.append(str, offset, len);
return this;
}
@Override
! public StringBuilder append(boolean b) {
super.append(b);
return this;
}
@Override
! public StringBuilder append(char c) {
super.append(c);
return this;
}
@Override
! public StringBuilder append(int i) {
super.append(i);
return this;
}
@Override
! public StringBuilder append(long lng) {
super.append(lng);
return this;
}
@Override
! public StringBuilder append(float f) {
super.append(f);
return this;
}
@Override
! public StringBuilder append(double d) {
super.append(d);
return this;
}
/**
* @since 1.5
*/
@Override
! public StringBuilder appendCodePoint(int codePoint) {
super.appendCodePoint(codePoint);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder delete(int start, int end) {
super.delete(start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder deleteCharAt(int index) {
super.deleteCharAt(index);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder replace(int start, int end, String str) {
super.replace(start, end, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int index, char[] str, int offset,
int len)
{
super.insert(index, str, offset, len);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, Object obj) {
super.insert(offset, obj);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, String str) {
super.insert(offset, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, char[] str) {
super.insert(offset, str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int dstOffset, CharSequence s) {
super.insert(dstOffset, s);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int dstOffset, CharSequence s,
int start, int end)
{
super.insert(dstOffset, s, start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, boolean b) {
super.insert(offset, b);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, char c) {
super.insert(offset, c);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, int i) {
super.insert(offset, i);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, long l) {
super.insert(offset, l);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, float f) {
super.insert(offset, f);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
*/
@Override
! public StringBuilder insert(int offset, double d) {
super.insert(offset, d);
return this;
}
@Override
! public int indexOf(String str) {
! return super.indexOf(str);
}
@Override
! public int indexOf(String str, int fromIndex) {
! return super.indexOf(str, fromIndex);
}
@Override
! public int lastIndexOf(String str) {
! return super.lastIndexOf(str);
}
@Override
! public int lastIndexOf(String str, int fromIndex) {
! return super.lastIndexOf(str, fromIndex);
}
@Override
! public StringBuilder reverse() {
! super.reverse();
! return this;
}
@Override
public String toString() {
! // Create a copy, don't share the array
! return new String(value, 0, count);
}
/**
* Save the state of the {@code StringBuilder} instance to a stream
* (that is, serialize it).
--- 156,552 ----
* <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
* in the argument {@code sb}.
*
* @param sb the {@code StringBuffer} to append.
* @return a reference to this object.
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
! @Override
! public ThreadLocalStringBuilder append(StringBuffer sb) {
! checkThread();
super.append(sb);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(CharSequence s) {
! checkThread();
super.append(s);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder append(CharSequence s, int start, int end) {
! checkThread();
super.append(s, start, end);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(char[] str) {
! checkThread();
super.append(str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder append(char[] str, int offset, int len) {
! checkThread();
super.append(str, offset, len);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(boolean b) {
! checkThread();
super.append(b);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(char c) {
! checkThread();
super.append(c);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(int i) {
! checkThread();
super.append(i);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(long lng) {
! checkThread();
super.append(lng);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(float f) {
! checkThread();
super.append(f);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder append(double d) {
! checkThread();
super.append(d);
return this;
}
/**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
* @since 1.5
*/
@Override
! public ThreadLocalStringBuilder appendCodePoint(int codePoint) {
! checkThread();
super.appendCodePoint(codePoint);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder delete(int start, int end) {
! checkThread();
super.delete(start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder deleteCharAt(int index) {
! checkThread();
super.deleteCharAt(index);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder replace(int start, int end, String str) {
! checkThread();
super.replace(start, end, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int index, char[] str, int offset,
int len)
{
+ checkThread();
super.insert(index, str, offset, len);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, Object obj) {
! checkThread();
super.insert(offset, obj);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, String str) {
! checkThread();
super.insert(offset, str);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, char[] str) {
! checkThread();
super.insert(offset, str);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int dstOffset, CharSequence s) {
! checkThread();
super.insert(dstOffset, s);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int dstOffset, CharSequence s,
int start, int end)
{
+ checkThread();
super.insert(dstOffset, s, start, end);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, boolean b) {
! checkThread();
super.insert(offset, b);
return this;
}
/**
* @throws IndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, char c) {
! checkThread();
super.insert(offset, c);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, int i) {
! checkThread();
super.insert(offset, i);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, long l) {
! checkThread();
super.insert(offset, l);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, float f) {
! checkThread();
super.insert(offset, f);
return this;
}
/**
* @throws StringIndexOutOfBoundsException {@inheritDoc}
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
*/
@Override
! public ThreadLocalStringBuilder insert(int offset, double d) {
! checkThread();
super.insert(offset, d);
return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public ThreadLocalStringBuilder reverse() {
! checkThread();
! super.reverse();
! return this;
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public void ensureCapacity(int minimumCapacity) {
! checkThread();
! super.ensureCapacity(minimumCapacity);
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public void trimToSize() {
! checkThread();
! super.trimToSize();
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public void setLength(int newLength) {
! checkThread();
! super.setLength(newLength);
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread or
+ * {@code toString()} has already been called
+ */
@Override
! public void setCharAt(int index, char ch) {
! checkThread();
! super.setCharAt(index, ch);
}
+ /**
+ * @throws IllegalStateException if called from non-constructing thread
+ * and {@code toString()} has not been called yet
+ */
@Override
public String toString() {
! Object o = constructionThreadOrString;
! if (o == Thread.currentThread()) {
! String s = (value.length == count)
! ? new String(value, true) // share the array if of correct length
! : new String(value, 0, count); // don't share the array otherwise
! constructionThreadOrString = s;
! return s;
! }
! if (o instanceof String) { // allow multiple calls to toString()
! return (String) o;
! }
! throw new IllegalStateException("Illegal invoking thread");
}
/**
* Save the state of the {@code StringBuilder} instance to a stream
* (that is, serialize it).
*** 424,439 ****
s.writeInt(count);
s.writeObject(value);
}
/**
! * readObject is called to restore the state of the StringBuffer from
! * a stream.
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
count = s.readInt();
value = (char[]) s.readObject();
}
-
}
--- 564,579 ----
s.writeInt(count);
s.writeObject(value);
}
/**
! * readObject is called to restore the state of the ThreadLocalStringBuilder
! * from a stream.
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();
count = s.readInt();
value = (char[]) s.readObject();
+ constructionThreadOrString = Thread.currentThread();
}
}
< prev index next >