1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/mini/src/main/java/java/lang/StringBuilder.java Wed Jan 23 20:39:23 2013 +0100
1.3 @@ -0,0 +1,436 @@
1.4 +/*
1.5 + * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang;
1.30 +
1.31 +
1.32 +/**
1.33 + * A mutable sequence of characters. This class provides an API compatible
1.34 + * with <code>StringBuffer</code>, but with no guarantee of synchronization.
1.35 + * This class is designed for use as a drop-in replacement for
1.36 + * <code>StringBuffer</code> in places where the string buffer was being
1.37 + * used by a single thread (as is generally the case). Where possible,
1.38 + * it is recommended that this class be used in preference to
1.39 + * <code>StringBuffer</code> as it will be faster under most implementations.
1.40 + *
1.41 + * <p>The principal operations on a <code>StringBuilder</code> are the
1.42 + * <code>append</code> and <code>insert</code> methods, which are
1.43 + * overloaded so as to accept data of any type. Each effectively
1.44 + * converts a given datum to a string and then appends or inserts the
1.45 + * characters of that string to the string builder. The
1.46 + * <code>append</code> method always adds these characters at the end
1.47 + * of the builder; the <code>insert</code> method adds the characters at
1.48 + * a specified point.
1.49 + * <p>
1.50 + * For example, if <code>z</code> refers to a string builder object
1.51 + * whose current contents are "<code>start</code>", then
1.52 + * the method call <code>z.append("le")</code> would cause the string
1.53 + * builder to contain "<code>startle</code>", whereas
1.54 + * <code>z.insert(4, "le")</code> would alter the string builder to
1.55 + * contain "<code>starlet</code>".
1.56 + * <p>
1.57 + * In general, if sb refers to an instance of a <code>StringBuilder</code>,
1.58 + * then <code>sb.append(x)</code> has the same effect as
1.59 + * <code>sb.insert(sb.length(), x)</code>.
1.60 + *
1.61 + * Every string builder has a capacity. As long as the length of the
1.62 + * character sequence contained in the string builder does not exceed
1.63 + * the capacity, it is not necessary to allocate a new internal
1.64 + * buffer. If the internal buffer overflows, it is automatically made larger.
1.65 + *
1.66 + * <p>Instances of <code>StringBuilder</code> are not safe for
1.67 + * use by multiple threads. If such synchronization is required then it is
1.68 + * recommended that {@link java.lang.StringBuffer} be used.
1.69 + *
1.70 + * @author Michael McCloskey
1.71 + * @see java.lang.StringBuffer
1.72 + * @see java.lang.String
1.73 + * @since 1.5
1.74 + */
1.75 +public final class StringBuilder
1.76 + extends AbstractStringBuilder
1.77 + implements java.io.Serializable, CharSequence
1.78 +{
1.79 +
1.80 + /** use serialVersionUID for interoperability */
1.81 + static final long serialVersionUID = 4383685877147921099L;
1.82 +
1.83 + /**
1.84 + * Constructs a string builder with no characters in it and an
1.85 + * initial capacity of 16 characters.
1.86 + */
1.87 + public StringBuilder() {
1.88 + super(16);
1.89 + }
1.90 +
1.91 + /**
1.92 + * Constructs a string builder with no characters in it and an
1.93 + * initial capacity specified by the <code>capacity</code> argument.
1.94 + *
1.95 + * @param capacity the initial capacity.
1.96 + * @throws NegativeArraySizeException if the <code>capacity</code>
1.97 + * argument is less than <code>0</code>.
1.98 + */
1.99 + public StringBuilder(int capacity) {
1.100 + super(capacity);
1.101 + }
1.102 +
1.103 + /**
1.104 + * Constructs a string builder initialized to the contents of the
1.105 + * specified string. The initial capacity of the string builder is
1.106 + * <code>16</code> plus the length of the string argument.
1.107 + *
1.108 + * @param str the initial contents of the buffer.
1.109 + * @throws NullPointerException if <code>str</code> is <code>null</code>
1.110 + */
1.111 + public StringBuilder(String str) {
1.112 + super(str.length() + 16);
1.113 + append(str);
1.114 + }
1.115 +
1.116 + /**
1.117 + * Constructs a string builder that contains the same characters
1.118 + * as the specified <code>CharSequence</code>. The initial capacity of
1.119 + * the string builder is <code>16</code> plus the length of the
1.120 + * <code>CharSequence</code> argument.
1.121 + *
1.122 + * @param seq the sequence to copy.
1.123 + * @throws NullPointerException if <code>seq</code> is <code>null</code>
1.124 + */
1.125 + public StringBuilder(CharSequence seq) {
1.126 + this(seq.length() + 16);
1.127 + append(seq);
1.128 + }
1.129 +
1.130 + public StringBuilder append(Object obj) {
1.131 + return append(String.valueOf(obj));
1.132 + }
1.133 +
1.134 + public StringBuilder append(String str) {
1.135 + super.append(str);
1.136 + return this;
1.137 + }
1.138 +
1.139 + // Appends the specified string builder to this sequence.
1.140 + private StringBuilder append(StringBuilder sb) {
1.141 + if (sb == null)
1.142 + return append("null");
1.143 + int len = sb.length();
1.144 + int newcount = count + len;
1.145 + if (newcount > value.length)
1.146 + expandCapacity(newcount);
1.147 + sb.getChars(0, len, value, count);
1.148 + count = newcount;
1.149 + return this;
1.150 + }
1.151 +
1.152 + /**
1.153 + * Appends the specified <tt>StringBuffer</tt> to this sequence.
1.154 + * <p>
1.155 + * The characters of the <tt>StringBuffer</tt> argument are appended,
1.156 + * in order, to this sequence, increasing the
1.157 + * length of this sequence by the length of the argument.
1.158 + * If <tt>sb</tt> is <tt>null</tt>, then the four characters
1.159 + * <tt>"null"</tt> are appended to this sequence.
1.160 + * <p>
1.161 + * Let <i>n</i> be the length of this character sequence just prior to
1.162 + * execution of the <tt>append</tt> method. Then the character at index
1.163 + * <i>k</i> in the new character sequence is equal to the character at
1.164 + * index <i>k</i> in the old character sequence, if <i>k</i> is less than
1.165 + * <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
1.166 + * in the argument <code>sb</code>.
1.167 + *
1.168 + * @param sb the <tt>StringBuffer</tt> to append.
1.169 + * @return a reference to this object.
1.170 + */
1.171 + public StringBuilder append(StringBuffer sb) {
1.172 + super.append(sb);
1.173 + return this;
1.174 + }
1.175 +
1.176 + /**
1.177 + */
1.178 + public StringBuilder append(CharSequence s) {
1.179 + if (s == null)
1.180 + s = "null";
1.181 + if (s instanceof String)
1.182 + return this.append((String)s);
1.183 + if (s instanceof StringBuffer)
1.184 + return this.append((StringBuffer)s);
1.185 + if (s instanceof StringBuilder)
1.186 + return this.append((StringBuilder)s);
1.187 + return this.append(s, 0, s.length());
1.188 + }
1.189 +
1.190 + /**
1.191 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.192 + */
1.193 + public StringBuilder append(CharSequence s, int start, int end) {
1.194 + super.append(s, start, end);
1.195 + return this;
1.196 + }
1.197 +
1.198 + public StringBuilder append(char[] str) {
1.199 + super.append(str);
1.200 + return this;
1.201 + }
1.202 +
1.203 + /**
1.204 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.205 + */
1.206 + public StringBuilder append(char[] str, int offset, int len) {
1.207 + super.append(str, offset, len);
1.208 + return this;
1.209 + }
1.210 +
1.211 + public StringBuilder append(boolean b) {
1.212 + super.append(b);
1.213 + return this;
1.214 + }
1.215 +
1.216 + public StringBuilder append(char c) {
1.217 + super.append(c);
1.218 + return this;
1.219 + }
1.220 +
1.221 + public StringBuilder append(int i) {
1.222 + super.append(i);
1.223 + return this;
1.224 + }
1.225 +
1.226 + public StringBuilder append(long lng) {
1.227 + super.append(lng);
1.228 + return this;
1.229 + }
1.230 +
1.231 + public StringBuilder append(float f) {
1.232 + super.append(f);
1.233 + return this;
1.234 + }
1.235 +
1.236 + public StringBuilder append(double d) {
1.237 + super.append(d);
1.238 + return this;
1.239 + }
1.240 +
1.241 + /**
1.242 + * @since 1.5
1.243 + */
1.244 + public StringBuilder appendCodePoint(int codePoint) {
1.245 + super.appendCodePoint(codePoint);
1.246 + return this;
1.247 + }
1.248 +
1.249 + /**
1.250 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.251 + */
1.252 + public StringBuilder delete(int start, int end) {
1.253 + super.delete(start, end);
1.254 + return this;
1.255 + }
1.256 +
1.257 + /**
1.258 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.259 + */
1.260 + public StringBuilder deleteCharAt(int index) {
1.261 + super.deleteCharAt(index);
1.262 + return this;
1.263 + }
1.264 +
1.265 + /**
1.266 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.267 + */
1.268 + public StringBuilder replace(int start, int end, String str) {
1.269 + super.replace(start, end, str);
1.270 + return this;
1.271 + }
1.272 +
1.273 + /**
1.274 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.275 + */
1.276 + public StringBuilder insert(int index, char[] str, int offset,
1.277 + int len)
1.278 + {
1.279 + super.insert(index, str, offset, len);
1.280 + return this;
1.281 + }
1.282 +
1.283 + /**
1.284 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.285 + */
1.286 + public StringBuilder insert(int offset, Object obj) {
1.287 + return insert(offset, String.valueOf(obj));
1.288 + }
1.289 +
1.290 + /**
1.291 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.292 + */
1.293 + public StringBuilder insert(int offset, String str) {
1.294 + super.insert(offset, str);
1.295 + return this;
1.296 + }
1.297 +
1.298 + /**
1.299 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.300 + */
1.301 + public StringBuilder insert(int offset, char[] str) {
1.302 + super.insert(offset, str);
1.303 + return this;
1.304 + }
1.305 +
1.306 + /**
1.307 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.308 + */
1.309 + public StringBuilder insert(int dstOffset, CharSequence s) {
1.310 + if (s == null)
1.311 + s = "null";
1.312 + if (s instanceof String)
1.313 + return this.insert(dstOffset, (String)s);
1.314 + return this.insert(dstOffset, s, 0, s.length());
1.315 + }
1.316 +
1.317 + /**
1.318 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.319 + */
1.320 + public StringBuilder insert(int dstOffset, CharSequence s,
1.321 + int start, int end)
1.322 + {
1.323 + super.insert(dstOffset, s, start, end);
1.324 + return this;
1.325 + }
1.326 +
1.327 + /**
1.328 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.329 + */
1.330 + public StringBuilder insert(int offset, boolean b) {
1.331 + super.insert(offset, b);
1.332 + return this;
1.333 + }
1.334 +
1.335 + /**
1.336 + * @throws IndexOutOfBoundsException {@inheritDoc}
1.337 + */
1.338 + public StringBuilder insert(int offset, char c) {
1.339 + super.insert(offset, c);
1.340 + return this;
1.341 + }
1.342 +
1.343 + /**
1.344 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.345 + */
1.346 + public StringBuilder insert(int offset, int i) {
1.347 + return insert(offset, String.valueOf(i));
1.348 + }
1.349 +
1.350 + /**
1.351 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.352 + */
1.353 + public StringBuilder insert(int offset, long l) {
1.354 + return insert(offset, String.valueOf(l));
1.355 + }
1.356 +
1.357 + /**
1.358 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.359 + */
1.360 + public StringBuilder insert(int offset, float f) {
1.361 + return insert(offset, String.valueOf(f));
1.362 + }
1.363 +
1.364 + /**
1.365 + * @throws StringIndexOutOfBoundsException {@inheritDoc}
1.366 + */
1.367 + public StringBuilder insert(int offset, double d) {
1.368 + return insert(offset, String.valueOf(d));
1.369 + }
1.370 +
1.371 + /**
1.372 + * @throws NullPointerException {@inheritDoc}
1.373 + */
1.374 + public int indexOf(String str) {
1.375 + return indexOf(str, 0);
1.376 + }
1.377 +
1.378 + /**
1.379 + * @throws NullPointerException {@inheritDoc}
1.380 + */
1.381 + public int indexOf(String str, int fromIndex) {
1.382 + return super.indexOf(str, fromIndex);
1.383 + }
1.384 +
1.385 + /**
1.386 + * @throws NullPointerException {@inheritDoc}
1.387 + */
1.388 + public int lastIndexOf(String str) {
1.389 + return lastIndexOf(str, count);
1.390 + }
1.391 +
1.392 + /**
1.393 + * @throws NullPointerException {@inheritDoc}
1.394 + */
1.395 + public int lastIndexOf(String str, int fromIndex) {
1.396 + return String.lastIndexOf(value, 0, count,
1.397 + str.toCharArray(), 0, str.length(), fromIndex);
1.398 + }
1.399 +
1.400 + public StringBuilder reverse() {
1.401 + super.reverse();
1.402 + return this;
1.403 + }
1.404 +
1.405 + public String toString() {
1.406 + // Create a copy, don't share the array
1.407 + return new String(value, 0, count);
1.408 + }
1.409 +
1.410 + /**
1.411 + * Save the state of the <tt>StringBuilder</tt> instance to a stream
1.412 + * (that is, serialize it).
1.413 + *
1.414 + * @serialData the number of characters currently stored in the string
1.415 + * builder (<tt>int</tt>), followed by the characters in the
1.416 + * string builder (<tt>char[]</tt>). The length of the
1.417 + * <tt>char</tt> array may be greater than the number of
1.418 + * characters currently stored in the string builder, in which
1.419 + * case extra characters are ignored.
1.420 + */
1.421 +// private void writeObject(java.io.ObjectOutputStream s)
1.422 +// throws java.io.IOException {
1.423 +// s.defaultWriteObject();
1.424 +// s.writeInt(count);
1.425 +// s.writeObject(value);
1.426 +// }
1.427 +
1.428 + /**
1.429 + * readObject is called to restore the state of the StringBuffer from
1.430 + * a stream.
1.431 + */
1.432 +// private void readObject(java.io.ObjectInputStream s)
1.433 +// throws java.io.IOException, ClassNotFoundException {
1.434 +// s.defaultReadObject();
1.435 +// count = s.readInt();
1.436 +// value = (char[]) s.readObject();
1.437 +// }
1.438 +
1.439 +}