2
|
1 |
/*
|
|
2 |
* Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.lang;
|
|
27 |
|
|
28 |
|
|
29 |
/**
|
|
30 |
* A mutable sequence of characters. This class provides an API compatible
|
|
31 |
* with <code>StringBuffer</code>, but with no guarantee of synchronization.
|
|
32 |
* This class is designed for use as a drop-in replacement for
|
|
33 |
* <code>StringBuffer</code> in places where the string buffer was being
|
|
34 |
* used by a single thread (as is generally the case). Where possible,
|
|
35 |
* it is recommended that this class be used in preference to
|
|
36 |
* <code>StringBuffer</code> as it will be faster under most implementations.
|
|
37 |
*
|
|
38 |
* <p>The principal operations on a <code>StringBuilder</code> are the
|
|
39 |
* <code>append</code> and <code>insert</code> methods, which are
|
|
40 |
* overloaded so as to accept data of any type. Each effectively
|
|
41 |
* converts a given datum to a string and then appends or inserts the
|
|
42 |
* characters of that string to the string builder. The
|
|
43 |
* <code>append</code> method always adds these characters at the end
|
|
44 |
* of the builder; the <code>insert</code> method adds the characters at
|
|
45 |
* a specified point.
|
|
46 |
* <p>
|
|
47 |
* For example, if <code>z</code> refers to a string builder object
|
|
48 |
* whose current contents are "<code>start</code>", then
|
|
49 |
* the method call <code>z.append("le")</code> would cause the string
|
|
50 |
* builder to contain "<code>startle</code>", whereas
|
|
51 |
* <code>z.insert(4, "le")</code> would alter the string builder to
|
|
52 |
* contain "<code>starlet</code>".
|
|
53 |
* <p>
|
|
54 |
* In general, if sb refers to an instance of a <code>StringBuilder</code>,
|
|
55 |
* then <code>sb.append(x)</code> has the same effect as
|
|
56 |
* <code>sb.insert(sb.length(), x)</code>.
|
|
57 |
*
|
|
58 |
* Every string builder has a capacity. As long as the length of the
|
|
59 |
* character sequence contained in the string builder does not exceed
|
|
60 |
* the capacity, it is not necessary to allocate a new internal
|
|
61 |
* buffer. If the internal buffer overflows, it is automatically made larger.
|
|
62 |
*
|
|
63 |
* <p>Instances of <code>StringBuilder</code> are not safe for
|
|
64 |
* use by multiple threads. If such synchronization is required then it is
|
|
65 |
* recommended that {@link java.lang.StringBuffer} be used.
|
|
66 |
*
|
|
67 |
* @author Michael McCloskey
|
|
68 |
* @see java.lang.StringBuffer
|
|
69 |
* @see java.lang.String
|
|
70 |
* @since 1.5
|
|
71 |
*/
|
|
72 |
public final class StringBuilder
|
|
73 |
extends AbstractStringBuilder
|
|
74 |
implements java.io.Serializable, CharSequence
|
|
75 |
{
|
|
76 |
|
|
77 |
/** use serialVersionUID for interoperability */
|
|
78 |
static final long serialVersionUID = 4383685877147921099L;
|
|
79 |
|
|
80 |
/**
|
|
81 |
* Constructs a string builder with no characters in it and an
|
|
82 |
* initial capacity of 16 characters.
|
|
83 |
*/
|
|
84 |
public StringBuilder() {
|
|
85 |
super(16);
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Constructs a string builder with no characters in it and an
|
|
90 |
* initial capacity specified by the <code>capacity</code> argument.
|
|
91 |
*
|
|
92 |
* @param capacity the initial capacity.
|
|
93 |
* @throws NegativeArraySizeException if the <code>capacity</code>
|
|
94 |
* argument is less than <code>0</code>.
|
|
95 |
*/
|
|
96 |
public StringBuilder(int capacity) {
|
|
97 |
super(capacity);
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Constructs a string builder initialized to the contents of the
|
|
102 |
* specified string. The initial capacity of the string builder is
|
|
103 |
* <code>16</code> plus the length of the string argument.
|
|
104 |
*
|
|
105 |
* @param str the initial contents of the buffer.
|
|
106 |
* @throws NullPointerException if <code>str</code> is <code>null</code>
|
|
107 |
*/
|
|
108 |
public StringBuilder(String str) {
|
|
109 |
super(str.length() + 16);
|
|
110 |
append(str);
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* Constructs a string builder that contains the same characters
|
|
115 |
* as the specified <code>CharSequence</code>. The initial capacity of
|
|
116 |
* the string builder is <code>16</code> plus the length of the
|
|
117 |
* <code>CharSequence</code> argument.
|
|
118 |
*
|
|
119 |
* @param seq the sequence to copy.
|
|
120 |
* @throws NullPointerException if <code>seq</code> is <code>null</code>
|
|
121 |
*/
|
|
122 |
public StringBuilder(CharSequence seq) {
|
|
123 |
this(seq.length() + 16);
|
|
124 |
append(seq);
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* @see java.lang.String#valueOf(java.lang.Object)
|
|
129 |
* @see #append(java.lang.String)
|
|
130 |
*/
|
|
131 |
public StringBuilder append(Object obj) {
|
|
132 |
return append(String.valueOf(obj));
|
|
133 |
}
|
|
134 |
|
|
135 |
public StringBuilder append(String str) {
|
|
136 |
super.append(str);
|
|
137 |
return this;
|
|
138 |
}
|
|
139 |
|
|
140 |
// Appends the specified string builder to this sequence.
|
|
141 |
private StringBuilder append(StringBuilder sb) {
|
|
142 |
if (sb == null)
|
|
143 |
return append("null");
|
|
144 |
int len = sb.length();
|
|
145 |
int newcount = count + len;
|
|
146 |
if (newcount > value.length)
|
|
147 |
expandCapacity(newcount);
|
|
148 |
sb.getChars(0, len, value, count);
|
|
149 |
count = newcount;
|
|
150 |
return this;
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Appends the specified <tt>StringBuffer</tt> to this sequence.
|
|
155 |
* <p>
|
|
156 |
* The characters of the <tt>StringBuffer</tt> argument are appended,
|
|
157 |
* in order, to this sequence, increasing the
|
|
158 |
* length of this sequence by the length of the argument.
|
|
159 |
* If <tt>sb</tt> is <tt>null</tt>, then the four characters
|
|
160 |
* <tt>"null"</tt> are appended to this sequence.
|
|
161 |
* <p>
|
|
162 |
* Let <i>n</i> be the length of this character sequence just prior to
|
|
163 |
* execution of the <tt>append</tt> method. Then the character at index
|
|
164 |
* <i>k</i> in the new character sequence is equal to the character at
|
|
165 |
* index <i>k</i> in the old character sequence, if <i>k</i> is less than
|
|
166 |
* <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
|
|
167 |
* in the argument <code>sb</code>.
|
|
168 |
*
|
|
169 |
* @param sb the <tt>StringBuffer</tt> to append.
|
|
170 |
* @return a reference to this object.
|
|
171 |
*/
|
|
172 |
public StringBuilder append(StringBuffer sb) {
|
|
173 |
super.append(sb);
|
|
174 |
return this;
|
|
175 |
}
|
|
176 |
|
|
177 |
/**
|
|
178 |
* @throws IndexOutOfBoundsException {@inheritDoc}
|
|
179 |
*/
|
|
180 |
public StringBuilder append(CharSequence s) {
|
|
181 |
if (s == null)
|
|
182 |
s = "null";
|
|
183 |
if (s instanceof String)
|
|
184 |
return this.append((String)s);
|
|
185 |
if (s instanceof StringBuffer)
|
|
186 |
return this.append((StringBuffer)s);
|
|
187 |
if (s instanceof StringBuilder)
|
|
188 |
return this.append((StringBuilder)s);
|
|
189 |
return this.append(s, 0, s.length());
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* @throws IndexOutOfBoundsException {@inheritDoc}
|
|
194 |
*/
|
|
195 |
public StringBuilder append(CharSequence s, int start, int end) {
|
|
196 |
super.append(s, start, end);
|
|
197 |
return this;
|
|
198 |
}
|
|
199 |
|
|
200 |
public StringBuilder append(char str[]) {
|
|
201 |
super.append(str);
|
|
202 |
return this;
|
|
203 |
}
|
|
204 |
|
|
205 |
public StringBuilder append(char str[], int offset, int len) {
|
|
206 |
super.append(str, offset, len);
|
|
207 |
return this;
|
|
208 |
}
|
|
209 |
|
|
210 |
/**
|
|
211 |
* @see java.lang.String#valueOf(boolean)
|
|
212 |
* @see #append(java.lang.String)
|
|
213 |
*/
|
|
214 |
public StringBuilder append(boolean b) {
|
|
215 |
super.append(b);
|
|
216 |
return this;
|
|
217 |
}
|
|
218 |
|
|
219 |
public StringBuilder append(char c) {
|
|
220 |
super.append(c);
|
|
221 |
return this;
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
|
225 |
* @see java.lang.String#valueOf(int)
|
|
226 |
* @see #append(java.lang.String)
|
|
227 |
*/
|
|
228 |
public StringBuilder append(int i) {
|
|
229 |
super.append(i);
|
|
230 |
return this;
|
|
231 |
}
|
|
232 |
|
|
233 |
/**
|
|
234 |
* @see java.lang.String#valueOf(long)
|
|
235 |
* @see #append(java.lang.String)
|
|
236 |
*/
|
|
237 |
public StringBuilder append(long lng) {
|
|
238 |
super.append(lng);
|
|
239 |
return this;
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* @see java.lang.String#valueOf(float)
|
|
244 |
* @see #append(java.lang.String)
|
|
245 |
*/
|
|
246 |
public StringBuilder append(float f) {
|
|
247 |
super.append(f);
|
|
248 |
return this;
|
|
249 |
}
|
|
250 |
|
|
251 |
/**
|
|
252 |
* @see java.lang.String#valueOf(double)
|
|
253 |
* @see #append(java.lang.String)
|
|
254 |
*/
|
|
255 |
public StringBuilder append(double d) {
|
|
256 |
super.append(d);
|
|
257 |
return this;
|
|
258 |
}
|
|
259 |
|
|
260 |
/**
|
|
261 |
* @since 1.5
|
|
262 |
*/
|
|
263 |
public StringBuilder appendCodePoint(int codePoint) {
|
|
264 |
super.appendCodePoint(codePoint);
|
|
265 |
return this;
|
|
266 |
}
|
|
267 |
|
|
268 |
/**
|
|
269 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
270 |
*/
|
|
271 |
public StringBuilder delete(int start, int end) {
|
|
272 |
super.delete(start, end);
|
|
273 |
return this;
|
|
274 |
}
|
|
275 |
|
|
276 |
/**
|
|
277 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
278 |
*/
|
|
279 |
public StringBuilder deleteCharAt(int index) {
|
|
280 |
super.deleteCharAt(index);
|
|
281 |
return this;
|
|
282 |
}
|
|
283 |
|
|
284 |
/**
|
|
285 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
286 |
*/
|
|
287 |
public StringBuilder replace(int start, int end, String str) {
|
|
288 |
super.replace(start, end, str);
|
|
289 |
return this;
|
|
290 |
}
|
|
291 |
|
|
292 |
/**
|
|
293 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
294 |
*/
|
|
295 |
public StringBuilder insert(int index, char str[], int offset,
|
|
296 |
int len)
|
|
297 |
{
|
|
298 |
super.insert(index, str, offset, len);
|
|
299 |
return this;
|
|
300 |
}
|
|
301 |
|
|
302 |
/**
|
|
303 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
304 |
* @see java.lang.String#valueOf(java.lang.Object)
|
|
305 |
* @see #insert(int, java.lang.String)
|
|
306 |
* @see #length()
|
|
307 |
*/
|
|
308 |
public StringBuilder insert(int offset, Object obj) {
|
|
309 |
return insert(offset, String.valueOf(obj));
|
|
310 |
}
|
|
311 |
|
|
312 |
/**
|
|
313 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
314 |
* @see #length()
|
|
315 |
*/
|
|
316 |
public StringBuilder insert(int offset, String str) {
|
|
317 |
super.insert(offset, str);
|
|
318 |
return this;
|
|
319 |
}
|
|
320 |
|
|
321 |
/**
|
|
322 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
323 |
*/
|
|
324 |
public StringBuilder insert(int offset, char str[]) {
|
|
325 |
super.insert(offset, str);
|
|
326 |
return this;
|
|
327 |
}
|
|
328 |
|
|
329 |
/**
|
|
330 |
* @throws IndexOutOfBoundsException {@inheritDoc}
|
|
331 |
*/
|
|
332 |
public StringBuilder insert(int dstOffset, CharSequence s) {
|
|
333 |
if (s == null)
|
|
334 |
s = "null";
|
|
335 |
if (s instanceof String)
|
|
336 |
return this.insert(dstOffset, (String)s);
|
|
337 |
return this.insert(dstOffset, s, 0, s.length());
|
|
338 |
}
|
|
339 |
|
|
340 |
/**
|
|
341 |
* @throws IndexOutOfBoundsException {@inheritDoc}
|
|
342 |
*/
|
|
343 |
public StringBuilder insert(int dstOffset, CharSequence s,
|
|
344 |
int start, int end)
|
|
345 |
{
|
|
346 |
super.insert(dstOffset, s, start, end);
|
|
347 |
return this;
|
|
348 |
}
|
|
349 |
|
|
350 |
/**
|
|
351 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
352 |
* @see java.lang.String#valueOf(boolean)
|
|
353 |
* @see #insert(int, java.lang.String)
|
|
354 |
* @see #length()
|
|
355 |
*/
|
|
356 |
public StringBuilder insert(int offset, boolean b) {
|
|
357 |
super.insert(offset, b);
|
|
358 |
return this;
|
|
359 |
}
|
|
360 |
|
|
361 |
/**
|
|
362 |
* @throws IndexOutOfBoundsException {@inheritDoc}
|
|
363 |
* @see #length()
|
|
364 |
*/
|
|
365 |
public StringBuilder insert(int offset, char c) {
|
|
366 |
super.insert(offset, c);
|
|
367 |
return this;
|
|
368 |
}
|
|
369 |
|
|
370 |
/**
|
|
371 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
372 |
* @see java.lang.String#valueOf(int)
|
|
373 |
* @see #insert(int, java.lang.String)
|
|
374 |
* @see #length()
|
|
375 |
*/
|
|
376 |
public StringBuilder insert(int offset, int i) {
|
|
377 |
return insert(offset, String.valueOf(i));
|
|
378 |
}
|
|
379 |
|
|
380 |
/**
|
|
381 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
382 |
* @see java.lang.String#valueOf(long)
|
|
383 |
* @see #insert(int, java.lang.String)
|
|
384 |
* @see #length()
|
|
385 |
*/
|
|
386 |
public StringBuilder insert(int offset, long l) {
|
|
387 |
return insert(offset, String.valueOf(l));
|
|
388 |
}
|
|
389 |
|
|
390 |
/**
|
|
391 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
392 |
* @see java.lang.String#valueOf(float)
|
|
393 |
* @see #insert(int, java.lang.String)
|
|
394 |
* @see #length()
|
|
395 |
*/
|
|
396 |
public StringBuilder insert(int offset, float f) {
|
|
397 |
return insert(offset, String.valueOf(f));
|
|
398 |
}
|
|
399 |
|
|
400 |
/**
|
|
401 |
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
|
402 |
* @see java.lang.String#valueOf(double)
|
|
403 |
* @see #insert(int, java.lang.String)
|
|
404 |
* @see #length()
|
|
405 |
*/
|
|
406 |
public StringBuilder insert(int offset, double d) {
|
|
407 |
return insert(offset, String.valueOf(d));
|
|
408 |
}
|
|
409 |
|
|
410 |
/**
|
|
411 |
* @throws NullPointerException {@inheritDoc}
|
|
412 |
*/
|
|
413 |
public int indexOf(String str) {
|
|
414 |
return indexOf(str, 0);
|
|
415 |
}
|
|
416 |
|
|
417 |
/**
|
|
418 |
* @throws NullPointerException {@inheritDoc}
|
|
419 |
*/
|
|
420 |
public int indexOf(String str, int fromIndex) {
|
|
421 |
return String.indexOf(value, 0, count,
|
|
422 |
str.toCharArray(), 0, str.length(), fromIndex);
|
|
423 |
}
|
|
424 |
|
|
425 |
/**
|
|
426 |
* @throws NullPointerException {@inheritDoc}
|
|
427 |
*/
|
|
428 |
public int lastIndexOf(String str) {
|
|
429 |
return lastIndexOf(str, count);
|
|
430 |
}
|
|
431 |
|
|
432 |
/**
|
|
433 |
* @throws NullPointerException {@inheritDoc}
|
|
434 |
*/
|
|
435 |
public int lastIndexOf(String str, int fromIndex) {
|
|
436 |
return String.lastIndexOf(value, 0, count,
|
|
437 |
str.toCharArray(), 0, str.length(), fromIndex);
|
|
438 |
}
|
|
439 |
|
|
440 |
public StringBuilder reverse() {
|
|
441 |
super.reverse();
|
|
442 |
return this;
|
|
443 |
}
|
|
444 |
|
|
445 |
public String toString() {
|
|
446 |
// Create a copy, don't share the array
|
|
447 |
return new String(value, 0, count);
|
|
448 |
}
|
|
449 |
|
|
450 |
/**
|
|
451 |
* Save the state of the <tt>StringBuilder</tt> instance to a stream
|
|
452 |
* (that is, serialize it).
|
|
453 |
*
|
|
454 |
* @serialData the number of characters currently stored in the string
|
|
455 |
* builder (<tt>int</tt>), followed by the characters in the
|
|
456 |
* string builder (<tt>char[]</tt>). The length of the
|
|
457 |
* <tt>char</tt> array may be greater than the number of
|
|
458 |
* characters currently stored in the string builder, in which
|
|
459 |
* case extra characters are ignored.
|
|
460 |
*/
|
|
461 |
private void writeObject(java.io.ObjectOutputStream s)
|
|
462 |
throws java.io.IOException {
|
|
463 |
s.defaultWriteObject();
|
|
464 |
s.writeInt(count);
|
|
465 |
s.writeObject(value);
|
|
466 |
}
|
|
467 |
|
|
468 |
/**
|
|
469 |
* readObject is called to restore the state of the StringBuffer from
|
|
470 |
* a stream.
|
|
471 |
*/
|
|
472 |
private void readObject(java.io.ObjectInputStream s)
|
|
473 |
throws java.io.IOException, ClassNotFoundException {
|
|
474 |
s.defaultReadObject();
|
|
475 |
count = s.readInt();
|
|
476 |
value = (char[]) s.readObject();
|
|
477 |
}
|
|
478 |
|
|
479 |
}
|