jaroslav@52: /* jaroslav@52: * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved. jaroslav@52: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@52: * jaroslav@52: * This code is free software; you can redistribute it and/or modify it jaroslav@52: * under the terms of the GNU General Public License version 2 only, as jaroslav@52: * published by the Free Software Foundation. Oracle designates this jaroslav@52: * particular file as subject to the "Classpath" exception as provided jaroslav@52: * by Oracle in the LICENSE file that accompanied this code. jaroslav@52: * jaroslav@52: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@52: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@52: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@52: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@52: * accompanied this code). jaroslav@52: * jaroslav@52: * You should have received a copy of the GNU General Public License version jaroslav@52: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@52: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@52: * jaroslav@52: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@52: * or visit www.oracle.com if you need additional information or have any jaroslav@52: * questions. jaroslav@52: */ jaroslav@52: jaroslav@52: package java.lang; jaroslav@52: jaroslav@52: jaroslav@52: /** jaroslav@52: * A mutable sequence of characters. This class provides an API compatible jaroslav@52: * with StringBuffer, but with no guarantee of synchronization. jaroslav@52: * This class is designed for use as a drop-in replacement for jaroslav@52: * StringBuffer in places where the string buffer was being jaroslav@52: * used by a single thread (as is generally the case). Where possible, jaroslav@52: * it is recommended that this class be used in preference to jaroslav@52: * StringBuffer as it will be faster under most implementations. jaroslav@52: * jaroslav@52: *

The principal operations on a StringBuilder are the jaroslav@52: * append and insert methods, which are jaroslav@52: * overloaded so as to accept data of any type. Each effectively jaroslav@52: * converts a given datum to a string and then appends or inserts the jaroslav@52: * characters of that string to the string builder. The jaroslav@52: * append method always adds these characters at the end jaroslav@52: * of the builder; the insert method adds the characters at jaroslav@52: * a specified point. jaroslav@52: *

jaroslav@52: * For example, if z refers to a string builder object jaroslav@52: * whose current contents are "start", then jaroslav@52: * the method call z.append("le") would cause the string jaroslav@52: * builder to contain "startle", whereas jaroslav@52: * z.insert(4, "le") would alter the string builder to jaroslav@52: * contain "starlet". jaroslav@52: *

jaroslav@52: * In general, if sb refers to an instance of a StringBuilder, jaroslav@52: * then sb.append(x) has the same effect as jaroslav@52: * sb.insert(sb.length(), x). jaroslav@52: * jaroslav@52: * Every string builder has a capacity. As long as the length of the jaroslav@52: * character sequence contained in the string builder does not exceed jaroslav@52: * the capacity, it is not necessary to allocate a new internal jaroslav@52: * buffer. If the internal buffer overflows, it is automatically made larger. jaroslav@52: * jaroslav@52: *

Instances of StringBuilder are not safe for jaroslav@52: * use by multiple threads. If such synchronization is required then it is jaroslav@52: * recommended that {@link java.lang.StringBuffer} be used. jaroslav@52: * jaroslav@52: * @author Michael McCloskey jaroslav@52: * @see java.lang.StringBuffer jaroslav@52: * @see java.lang.String jaroslav@52: * @since 1.5 jaroslav@52: */ jaroslav@52: public final class StringBuilder jaroslav@52: extends AbstractStringBuilder jaroslav@52: implements java.io.Serializable, CharSequence jaroslav@52: { jaroslav@52: jaroslav@52: /** use serialVersionUID for interoperability */ jaroslav@52: static final long serialVersionUID = 4383685877147921099L; jaroslav@52: jaroslav@52: /** jaroslav@52: * Constructs a string builder with no characters in it and an jaroslav@52: * initial capacity of 16 characters. jaroslav@52: */ jaroslav@52: public StringBuilder() { jaroslav@52: super(16); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Constructs a string builder with no characters in it and an jaroslav@52: * initial capacity specified by the capacity argument. jaroslav@52: * jaroslav@52: * @param capacity the initial capacity. jaroslav@52: * @throws NegativeArraySizeException if the capacity jaroslav@52: * argument is less than 0. jaroslav@52: */ jaroslav@52: public StringBuilder(int capacity) { jaroslav@52: super(capacity); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Constructs a string builder initialized to the contents of the jaroslav@52: * specified string. The initial capacity of the string builder is jaroslav@52: * 16 plus the length of the string argument. jaroslav@52: * jaroslav@52: * @param str the initial contents of the buffer. jaroslav@52: * @throws NullPointerException if str is null jaroslav@52: */ jaroslav@52: public StringBuilder(String str) { jaroslav@52: super(str.length() + 16); jaroslav@52: append(str); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Constructs a string builder that contains the same characters jaroslav@52: * as the specified CharSequence. The initial capacity of jaroslav@52: * the string builder is 16 plus the length of the jaroslav@52: * CharSequence argument. jaroslav@52: * jaroslav@52: * @param seq the sequence to copy. jaroslav@52: * @throws NullPointerException if seq is null jaroslav@52: */ jaroslav@52: public StringBuilder(CharSequence seq) { jaroslav@52: this(seq.length() + 16); jaroslav@52: append(seq); jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(Object obj) { jaroslav@52: return append(String.valueOf(obj)); jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(String str) { jaroslav@52: super.append(str); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: // Appends the specified string builder to this sequence. jaroslav@52: private StringBuilder append(StringBuilder sb) { jaroslav@52: if (sb == null) jaroslav@52: return append("null"); jaroslav@52: int len = sb.length(); jaroslav@52: int newcount = count + len; jaroslav@52: if (newcount > value.length) jaroslav@52: expandCapacity(newcount); jaroslav@52: sb.getChars(0, len, value, count); jaroslav@52: count = newcount; jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Appends the specified StringBuffer to this sequence. jaroslav@52: *

jaroslav@52: * The characters of the StringBuffer argument are appended, jaroslav@52: * in order, to this sequence, increasing the jaroslav@52: * length of this sequence by the length of the argument. jaroslav@52: * If sb is null, then the four characters jaroslav@52: * "null" are appended to this sequence. jaroslav@52: *

jaroslav@52: * Let n be the length of this character sequence just prior to jaroslav@52: * execution of the append method. Then the character at index jaroslav@52: * k in the new character sequence is equal to the character at jaroslav@52: * index k in the old character sequence, if k is less than jaroslav@52: * n; otherwise, it is equal to the character at index k-n jaroslav@52: * in the argument sb. jaroslav@52: * jaroslav@52: * @param sb the StringBuffer to append. jaroslav@52: * @return a reference to this object. jaroslav@52: */ jaroslav@52: public StringBuilder append(StringBuffer sb) { jaroslav@52: super.append(sb); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: */ jaroslav@52: public StringBuilder append(CharSequence s) { jaroslav@52: if (s == null) jaroslav@52: s = "null"; jaroslav@52: if (s instanceof String) jaroslav@52: return this.append((String)s); jaroslav@52: if (s instanceof StringBuffer) jaroslav@52: return this.append((StringBuffer)s); jaroslav@52: if (s instanceof StringBuilder) jaroslav@52: return this.append((StringBuilder)s); jaroslav@52: return this.append(s, 0, s.length()); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder append(CharSequence s, int start, int end) { jaroslav@52: super.append(s, start, end); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(char[] str) { jaroslav@52: super.append(str); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder append(char[] str, int offset, int len) { jaroslav@52: super.append(str, offset, len); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(boolean b) { jaroslav@52: super.append(b); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(char c) { jaroslav@52: super.append(c); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(int i) { jaroslav@52: super.append(i); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(long lng) { jaroslav@52: super.append(lng); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(float f) { jaroslav@52: super.append(f); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder append(double d) { jaroslav@52: super.append(d); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @since 1.5 jaroslav@52: */ jaroslav@52: public StringBuilder appendCodePoint(int codePoint) { jaroslav@52: super.appendCodePoint(codePoint); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder delete(int start, int end) { jaroslav@52: super.delete(start, end); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder deleteCharAt(int index) { jaroslav@52: super.deleteCharAt(index); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder replace(int start, int end, String str) { jaroslav@52: super.replace(start, end, str); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int index, char[] str, int offset, jaroslav@52: int len) jaroslav@52: { jaroslav@52: super.insert(index, str, offset, len); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, Object obj) { jaroslav@52: return insert(offset, String.valueOf(obj)); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, String str) { jaroslav@52: super.insert(offset, str); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, char[] str) { jaroslav@52: super.insert(offset, str); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int dstOffset, CharSequence s) { jaroslav@52: if (s == null) jaroslav@52: s = "null"; jaroslav@52: if (s instanceof String) jaroslav@52: return this.insert(dstOffset, (String)s); jaroslav@52: return this.insert(dstOffset, s, 0, s.length()); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int dstOffset, CharSequence s, jaroslav@52: int start, int end) jaroslav@52: { jaroslav@52: super.insert(dstOffset, s, start, end); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, boolean b) { jaroslav@52: super.insert(offset, b); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws IndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, char c) { jaroslav@52: super.insert(offset, c); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, int i) { jaroslav@52: return insert(offset, String.valueOf(i)); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, long l) { jaroslav@52: return insert(offset, String.valueOf(l)); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, float f) { jaroslav@52: return insert(offset, String.valueOf(f)); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws StringIndexOutOfBoundsException {@inheritDoc} jaroslav@52: */ jaroslav@52: public StringBuilder insert(int offset, double d) { jaroslav@52: return insert(offset, String.valueOf(d)); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws NullPointerException {@inheritDoc} jaroslav@52: */ jaroslav@52: public int indexOf(String str) { jaroslav@52: return indexOf(str, 0); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws NullPointerException {@inheritDoc} jaroslav@52: */ jaroslav@52: public int indexOf(String str, int fromIndex) { jaroslav@403: return super.indexOf(str, fromIndex); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws NullPointerException {@inheritDoc} jaroslav@52: */ jaroslav@52: public int lastIndexOf(String str) { jaroslav@52: return lastIndexOf(str, count); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * @throws NullPointerException {@inheritDoc} jaroslav@52: */ jaroslav@52: public int lastIndexOf(String str, int fromIndex) { jaroslav@52: return String.lastIndexOf(value, 0, count, jaroslav@52: str.toCharArray(), 0, str.length(), fromIndex); jaroslav@52: } jaroslav@52: jaroslav@52: public StringBuilder reverse() { jaroslav@52: super.reverse(); jaroslav@52: return this; jaroslav@52: } jaroslav@52: jaroslav@52: public String toString() { jaroslav@52: // Create a copy, don't share the array jaroslav@52: return new String(value, 0, count); jaroslav@52: } jaroslav@52: jaroslav@52: /** jaroslav@52: * Save the state of the StringBuilder instance to a stream jaroslav@52: * (that is, serialize it). jaroslav@52: * jaroslav@52: * @serialData the number of characters currently stored in the string jaroslav@52: * builder (int), followed by the characters in the jaroslav@52: * string builder (char[]). The length of the jaroslav@52: * char array may be greater than the number of jaroslav@52: * characters currently stored in the string builder, in which jaroslav@52: * case extra characters are ignored. jaroslav@52: */ jaroslav@79: // private void writeObject(java.io.ObjectOutputStream s) jaroslav@79: // throws java.io.IOException { jaroslav@79: // s.defaultWriteObject(); jaroslav@79: // s.writeInt(count); jaroslav@79: // s.writeObject(value); jaroslav@79: // } jaroslav@52: jaroslav@52: /** jaroslav@52: * readObject is called to restore the state of the StringBuffer from jaroslav@52: * a stream. jaroslav@52: */ jaroslav@79: // private void readObject(java.io.ObjectInputStream s) jaroslav@79: // throws java.io.IOException, ClassNotFoundException { jaroslav@79: // s.defaultReadObject(); jaroslav@79: // count = s.readInt(); jaroslav@79: // value = (char[]) s.readObject(); jaroslav@79: // } jaroslav@52: jaroslav@52: }