0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
0N/A
0N/A/**
0N/A * A mutable sequence of characters. This class provides an API compatible
0N/A * with <code>StringBuffer</code>, but with no guarantee of synchronization.
0N/A * This class is designed for use as a drop-in replacement for
0N/A * <code>StringBuffer</code> in places where the string buffer was being
0N/A * used by a single thread (as is generally the case). Where possible,
0N/A * it is recommended that this class be used in preference to
0N/A * <code>StringBuffer</code> as it will be faster under most implementations.
0N/A *
0N/A * <p>The principal operations on a <code>StringBuilder</code> are the
0N/A * <code>append</code> and <code>insert</code> methods, which are
0N/A * overloaded so as to accept data of any type. Each effectively
0N/A * converts a given datum to a string and then appends or inserts the
0N/A * characters of that string to the string builder. The
0N/A * <code>append</code> method always adds these characters at the end
0N/A * of the builder; the <code>insert</code> method adds the characters at
0N/A * a specified point.
0N/A * <p>
0N/A * For example, if <code>z</code> refers to a string builder object
0N/A * whose current contents are "<code>start</code>", then
0N/A * the method call <code>z.append("le")</code> would cause the string
0N/A * builder to contain "<code>startle</code>", whereas
0N/A * <code>z.insert(4, "le")</code> would alter the string builder to
0N/A * contain "<code>starlet</code>".
0N/A * <p>
0N/A * In general, if sb refers to an instance of a <code>StringBuilder</code>,
0N/A * then <code>sb.append(x)</code> has the same effect as
0N/A * <code>sb.insert(sb.length(),&nbsp;x)</code>.
0N/A *
0N/A * Every string builder has a capacity. As long as the length of the
0N/A * character sequence contained in the string builder does not exceed
0N/A * the capacity, it is not necessary to allocate a new internal
0N/A * buffer. If the internal buffer overflows, it is automatically made larger.
0N/A *
0N/A * <p>Instances of <code>StringBuilder</code> are not safe for
0N/A * use by multiple threads. If such synchronization is required then it is
0N/A * recommended that {@link java.lang.StringBuffer} be used.
0N/A *
0N/A * @author Michael McCloskey
0N/A * @see java.lang.StringBuffer
0N/A * @see java.lang.String
0N/A * @since 1.5
0N/A */
0N/Apublic final class StringBuilder
0N/A extends AbstractStringBuilder
0N/A implements java.io.Serializable, CharSequence
0N/A{
0N/A
0N/A /** use serialVersionUID for interoperability */
0N/A static final long serialVersionUID = 4383685877147921099L;
0N/A
0N/A /**
0N/A * Constructs a string builder with no characters in it and an
0N/A * initial capacity of 16 characters.
0N/A */
0N/A public StringBuilder() {
0N/A super(16);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string builder with no characters in it and an
0N/A * initial capacity specified by the <code>capacity</code> argument.
0N/A *
0N/A * @param capacity the initial capacity.
0N/A * @throws NegativeArraySizeException if the <code>capacity</code>
0N/A * argument is less than <code>0</code>.
0N/A */
0N/A public StringBuilder(int capacity) {
0N/A super(capacity);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string builder initialized to the contents of the
0N/A * specified string. The initial capacity of the string builder is
0N/A * <code>16</code> plus the length of the string argument.
0N/A *
0N/A * @param str the initial contents of the buffer.
0N/A * @throws NullPointerException if <code>str</code> is <code>null</code>
0N/A */
0N/A public StringBuilder(String str) {
0N/A super(str.length() + 16);
0N/A append(str);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a string builder that contains the same characters
0N/A * as the specified <code>CharSequence</code>. The initial capacity of
0N/A * the string builder is <code>16</code> plus the length of the
0N/A * <code>CharSequence</code> argument.
0N/A *
0N/A * @param seq the sequence to copy.
0N/A * @throws NullPointerException if <code>seq</code> is <code>null</code>
0N/A */
0N/A public StringBuilder(CharSequence seq) {
0N/A this(seq.length() + 16);
0N/A append(seq);
0N/A }
0N/A
0N/A public StringBuilder append(Object obj) {
0N/A return append(String.valueOf(obj));
0N/A }
0N/A
0N/A public StringBuilder append(String str) {
0N/A super.append(str);
0N/A return this;
0N/A }
0N/A
0N/A // Appends the specified string builder to this sequence.
0N/A private StringBuilder append(StringBuilder sb) {
0N/A if (sb == null)
0N/A return append("null");
0N/A int len = sb.length();
0N/A int newcount = count + len;
0N/A if (newcount > value.length)
0N/A expandCapacity(newcount);
0N/A sb.getChars(0, len, value, count);
0N/A count = newcount;
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified <tt>StringBuffer</tt> to this sequence.
0N/A * <p>
0N/A * The characters of the <tt>StringBuffer</tt> argument are appended,
0N/A * in order, to this sequence, increasing the
0N/A * length of this sequence by the length of the argument.
0N/A * If <tt>sb</tt> is <tt>null</tt>, then the four characters
0N/A * <tt>"null"</tt> are appended to this sequence.
0N/A * <p>
0N/A * Let <i>n</i> be the length of this character sequence just prior to
0N/A * execution of the <tt>append</tt> method. Then the character at index
0N/A * <i>k</i> in the new character sequence is equal to the character at
0N/A * index <i>k</i> in the old character sequence, if <i>k</i> is less than
0N/A * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
0N/A * in the argument <code>sb</code>.
0N/A *
0N/A * @param sb the <tt>StringBuffer</tt> to append.
0N/A * @return a reference to this object.
0N/A */
0N/A public StringBuilder append(StringBuffer sb) {
0N/A super.append(sb);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A */
0N/A public StringBuilder append(CharSequence s) {
0N/A if (s == null)
0N/A s = "null";
0N/A if (s instanceof String)
0N/A return this.append((String)s);
0N/A if (s instanceof StringBuffer)
0N/A return this.append((StringBuffer)s);
0N/A if (s instanceof StringBuilder)
0N/A return this.append((StringBuilder)s);
0N/A return this.append(s, 0, s.length());
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder append(CharSequence s, int start, int end) {
0N/A super.append(s, start, end);
0N/A return this;
0N/A }
0N/A
570N/A public StringBuilder append(char[] str) {
0N/A super.append(str);
0N/A return this;
0N/A }
0N/A
570N/A /**
570N/A * @throws IndexOutOfBoundsException {@inheritDoc}
570N/A */
570N/A public StringBuilder append(char[] str, int offset, int len) {
0N/A super.append(str, offset, len);
0N/A return this;
0N/A }
0N/A
0N/A public StringBuilder append(boolean b) {
0N/A super.append(b);
0N/A return this;
0N/A }
0N/A
0N/A public StringBuilder append(char c) {
0N/A super.append(c);
0N/A return this;
0N/A }
0N/A
0N/A public StringBuilder append(int i) {
0N/A super.append(i);
0N/A return this;
0N/A }
0N/A
0N/A public StringBuilder append(long lng) {
0N/A super.append(lng);
0N/A return this;
0N/A }
0N/A
0N/A public StringBuilder append(float f) {
0N/A super.append(f);
0N/A return this;
0N/A }
0N/A
0N/A public StringBuilder append(double d) {
0N/A super.append(d);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @since 1.5
0N/A */
0N/A public StringBuilder appendCodePoint(int codePoint) {
0N/A super.appendCodePoint(codePoint);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder delete(int start, int end) {
0N/A super.delete(start, end);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder deleteCharAt(int index) {
0N/A super.deleteCharAt(index);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder replace(int start, int end, String str) {
0N/A super.replace(start, end, str);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
570N/A public StringBuilder insert(int index, char[] str, int offset,
0N/A int len)
0N/A {
0N/A super.insert(index, str, offset, len);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int offset, Object obj) {
0N/A return insert(offset, String.valueOf(obj));
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int offset, String str) {
0N/A super.insert(offset, str);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
570N/A public StringBuilder insert(int offset, char[] str) {
0N/A super.insert(offset, str);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int dstOffset, CharSequence s) {
0N/A if (s == null)
0N/A s = "null";
0N/A if (s instanceof String)
0N/A return this.insert(dstOffset, (String)s);
0N/A return this.insert(dstOffset, s, 0, s.length());
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int dstOffset, CharSequence s,
0N/A int start, int end)
0N/A {
0N/A super.insert(dstOffset, s, start, end);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int offset, boolean b) {
0N/A super.insert(offset, b);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws IndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int offset, char c) {
0N/A super.insert(offset, c);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int offset, int i) {
0N/A return insert(offset, String.valueOf(i));
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int offset, long l) {
0N/A return insert(offset, String.valueOf(l));
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int offset, float f) {
0N/A return insert(offset, String.valueOf(f));
0N/A }
0N/A
0N/A /**
0N/A * @throws StringIndexOutOfBoundsException {@inheritDoc}
0N/A */
0N/A public StringBuilder insert(int offset, double d) {
0N/A return insert(offset, String.valueOf(d));
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public int indexOf(String str) {
0N/A return indexOf(str, 0);
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public int indexOf(String str, int fromIndex) {
0N/A return String.indexOf(value, 0, count,
0N/A str.toCharArray(), 0, str.length(), fromIndex);
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public int lastIndexOf(String str) {
0N/A return lastIndexOf(str, count);
0N/A }
0N/A
0N/A /**
0N/A * @throws NullPointerException {@inheritDoc}
0N/A */
0N/A public int lastIndexOf(String str, int fromIndex) {
0N/A return String.lastIndexOf(value, 0, count,
0N/A str.toCharArray(), 0, str.length(), fromIndex);
0N/A }
0N/A
0N/A public StringBuilder reverse() {
0N/A super.reverse();
0N/A return this;
0N/A }
0N/A
0N/A public String toString() {
0N/A // Create a copy, don't share the array
0N/A return new String(value, 0, count);
0N/A }
0N/A
0N/A /**
0N/A * Save the state of the <tt>StringBuilder</tt> instance to a stream
0N/A * (that is, serialize it).
0N/A *
0N/A * @serialData the number of characters currently stored in the string
0N/A * builder (<tt>int</tt>), followed by the characters in the
0N/A * string builder (<tt>char[]</tt>). The length of the
0N/A * <tt>char</tt> array may be greater than the number of
0N/A * characters currently stored in the string builder, in which
0N/A * case extra characters are ignored.
0N/A */
0N/A private void writeObject(java.io.ObjectOutputStream s)
0N/A throws java.io.IOException {
0N/A s.defaultWriteObject();
0N/A s.writeInt(count);
0N/A s.writeObject(value);
0N/A }
0N/A
0N/A /**
0N/A * readObject is called to restore the state of the StringBuffer from
0N/A * a stream.
0N/A */
0N/A private void readObject(java.io.ObjectInputStream s)
0N/A throws java.io.IOException, ClassNotFoundException {
0N/A s.defaultReadObject();
0N/A count = s.readInt();
0N/A value = (char[]) s.readObject();
0N/A }
0N/A
0N/A}