class StringBuilder
{
    private static final class State
    {
	char[] value = new char[16];
	int count;
        boolean shared;

        void ensureCapacity(int minimumCapacity)
        {
            if (shared || minimumCapacity > value.length)
            {
                // We don't want to make a larger vector when `shared' is
                // set.  If we do, then setLength becomes very inefficient
                // when repeatedly reusing a StringBuilder in a loop.
                int max = (minimumCapacity > value.length
                    ? value.length * 2 + 2
                    : value.length);
                minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
                char[] nb = new char[minimumCapacity];
                System.arraycopy(value, 0, nb, 0, count);
                value = nb;
                shared = false;
            }
        }
    }

    private volatile State state = new State();

    public StringBuilder append(String str)
    {
        State s = acquire();
        if (str == null)
            str = "null";
        int len = str.count;
        s.ensureCapacity(count + len);
        str.getChars(0, len, s.value, s.count);
        s.count += len;
        release(s);
        return this;
    }

    public String toString()
    {
        State s = acquire();
        String str = new String();
        str.value = s.value;
        str.count = s.count;
        s.shared = true;
        release(s);
        return str;
    }

    // This an inefficient Java implementation, but the JIT should special case
    // this method and turn it into an atomic exchange CPU instruction.
    private synchronized State acquire()
    {
        State s = state;
        state = null;
        return s;
    }

    private void release(State s)
    {
        state = s;
    }
}
