/* * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package java.lang; import java.util.stream.IntStream; /** * A mutable sequence of characters. This class provides an API compatible * with {@code StringBuilder}, with no need for synchronization. It allows * building at most one {@code String} and only in a single thread. * If any of the methods of this class is invoked * in a thread distinct from the thread that constructed the string builder, * an {@code IllegalStateException} is thrown. * If any of the methods of this class is invoked after the {@link #toString()} * method has already been invoked to construct a {@code String}, * an {@code IllegalStateException} is thrown. * These constraints allow this class to be more optimal than {@code StringBuilder} * in case initial capacity of the builder is estimated to the exact * length of the built String as no defensive copying of internal array is required. * * @since 9 */ public final class OneShotStringBuilder extends AbstractStringBuilder implements CharSequence { /** * Constructs a string builder with no characters in it and an * initial capacity of 16 characters. */ public OneShotStringBuilder() { 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 OneShotStringBuilder(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 OneShotStringBuilder(String str) { super(str.length() + 16); append(str); } /** * Constructs a string builder that contains the same characters * as the specified {@code CharSequence}. The initial capacity of * the string builder is {@code 16} plus the length of the * {@code CharSequence} argument. * * @param seq the sequence to copy. */ public OneShotStringBuilder(CharSequence seq) { this(seq.length() + 16); append(seq); } @Override public OneShotStringBuilder append(Object obj) { return append(String.valueOf(obj)); } @Override public OneShotStringBuilder append(String str) { checkUse(); 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 this sequence, increasing the * length of this sequence by the length of the argument. * If {@code sb} is {@code null}, then the four characters * {@code "null"} are appended to this sequence. *

* Let n be the length of this character sequence 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}. * * @param sb the {@code StringBuffer} to append. * @return a reference to this object. */ public OneShotStringBuilder append(StringBuffer sb) { checkUse(); super.append(sb); return this; } @Override public OneShotStringBuilder append(CharSequence s) { checkUse(); super.append(s); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder append(CharSequence s, int start, int end) { checkUse(); super.append(s, start, end); return this; } @Override public OneShotStringBuilder append(char[] str) { checkUse(); super.append(str); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder append(char[] str, int offset, int len) { checkUse(); super.append(str, offset, len); return this; } @Override public OneShotStringBuilder append(boolean b) { checkUse(); super.append(b); return this; } @Override public OneShotStringBuilder append(char c) { checkUse(); super.append(c); return this; } @Override public OneShotStringBuilder append(int i) { checkUse(); super.append(i); return this; } @Override public OneShotStringBuilder append(long lng) { checkUse(); super.append(lng); return this; } @Override public OneShotStringBuilder append(float f) { checkUse(); super.append(f); return this; } @Override public OneShotStringBuilder append(double d) { checkUse(); super.append(d); return this; } @Override public OneShotStringBuilder appendCodePoint(int codePoint) { checkUse(); super.appendCodePoint(codePoint); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder delete(int start, int end) { checkUse(); super.delete(start, end); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder deleteCharAt(int index) { checkUse(); super.deleteCharAt(index); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder replace(int start, int end, String str) { checkUse(); super.replace(start, end, str); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int index, char[] str, int offset, int len) { checkUse(); super.insert(index, str, offset, len); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, Object obj) { checkUse(); super.insert(offset, obj); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, String str) { checkUse(); super.insert(offset, str); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, char[] str) { checkUse(); super.insert(offset, str); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int dstOffset, CharSequence s) { checkUse(); super.insert(dstOffset, s); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int dstOffset, CharSequence s, int start, int end) { checkUse(); super.insert(dstOffset, s, start, end); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, boolean b) { checkUse(); super.insert(offset, b); return this; } /** * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, char c) { checkUse(); super.insert(offset, c); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, int i) { checkUse(); super.insert(offset, i); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, long l) { checkUse(); super.insert(offset, l); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, float f) { checkUse(); super.insert(offset, f); return this; } /** * @throws StringIndexOutOfBoundsException {@inheritDoc} */ @Override public OneShotStringBuilder insert(int offset, double d) { checkUse(); super.insert(offset, d); return this; } @Override public int indexOf(String str) { checkUse(); return super.indexOf(str); } @Override public int indexOf(String str, int fromIndex) { checkUse(); return super.indexOf(str, fromIndex); } @Override public int lastIndexOf(String str) { checkUse(); return super.lastIndexOf(str); } @Override public int lastIndexOf(String str, int fromIndex) { checkUse(); return super.lastIndexOf(str, fromIndex); } @Override public OneShotStringBuilder reverse() { checkUse(); super.reverse(); return this; } @Override public int length() { checkUse(); return super.length(); } @Override public int capacity() { checkUse(); return super.capacity(); } @Override public void ensureCapacity(int minimumCapacity) { checkUse(); super.ensureCapacity(minimumCapacity); } @Override public void trimToSize() { checkUse(); super.trimToSize(); } @Override public void setLength(int newLength) { checkUse(); super.setLength(newLength); } @Override public char charAt(int index) { checkUse(); return super.charAt(index); } @Override public int codePointAt(int index) { checkUse(); return super.codePointAt(index); } @Override public int codePointBefore(int index) { checkUse(); return super.codePointBefore(index); } @Override public int codePointCount(int beginIndex, int endIndex) { checkUse(); return super.codePointCount(beginIndex, endIndex); } @Override public int offsetByCodePoints(int index, int codePointOffset) { checkUse(); return super.offsetByCodePoints(index, codePointOffset); } @Override public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { checkUse(); super.getChars(srcBegin, srcEnd, dst, dstBegin); } @Override public void setCharAt(int index, char ch) { checkUse(); super.setCharAt(index, ch); } @Override public String substring(int start) { checkUse(); return super.substring(start); } @Override public CharSequence subSequence(int start, int end) { checkUse(); return super.subSequence(start, end); } @Override public String substring(int start, int end) { checkUse(); return super.substring(start, end); } @Override public IntStream chars() { checkUse(); return super.chars(); } @Override public IntStream codePoints() { checkUse(); return super.codePoints(); } @Override public String toString() { checkUse(); // take the value array and reset the value to null to make this // builder unusable... byte[] val = value; value = null; if (super.length() == super.capacity()) { // just pass the array by reference to String constructor if // capacity matches desired String length return new String(val, coder); } // else create a copy, don't share the array return isLatin1() ? StringLatin1.newString(val, 0, count) : StringUTF16.newString(val, 0, count); } /** * A thread that constructed this instance; all operations are constrained * to that thread. */ private final Thread constructingThread = Thread.currentThread(); private void checkUse() { if (constructingThread != Thread.currentThread()) { throw new IllegalStateException("Wrong thread"); } if (value == null) { throw new IllegalStateException( "This builder has already been used once to construct a String"); } } }