KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > JBossStringBuilder


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 /**
28  * A JBossStringBuilder, providing the same functionality as the
29  * java5 StringBuilder, except no Appendable which is java5 specific.
30  *
31  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
32  * @version $Revision: 1958 $
33  */

34 public class JBossStringBuilder implements Serializable JavaDoc, CharSequence JavaDoc
35 {
36    /** Serialization */
37    private static final long serialVersionUID = 1874946609763446794L;
38
39    /** The characters */
40    protected char[] chars;
41
42    /** The position */
43    protected int pos;
44
45    /**
46     * Create a new StringBuilder with no characters and an intial
47     * size of 16
48     */

49    public JBossStringBuilder()
50    {
51       this(16);
52    }
53
54    /**
55     * Create a new StringBuilder with no characters
56     *
57     * @param capacity the initial capacity
58     */

59    public JBossStringBuilder(int capacity)
60    {
61       chars = new char[capacity];
62    }
63
64
65    /**
66     * Create a new StringBuilder from the given string.
67     * The initial capacity is the length of the string plus 16
68     *
69     * @param string the string
70     */

71    public JBossStringBuilder(String JavaDoc string)
72    {
73       this(string.length() + 16);
74       append(string);
75    }
76
77    /**
78     * Create a new StringBuilder from the given character sequence.
79     * The initial capacity is the length of the sequence plus 16
80     *
81     * @param charSequence the character sequence
82     */

83    public JBossStringBuilder(CharSequence JavaDoc charSequence)
84    {
85       this(charSequence.length() + 16);
86       append(charSequence);
87    }
88
89    /**
90     * Create a new StringBuilder from the given character array
91     *
92     * @param ch the array
93     */

94    public JBossStringBuilder(char[] ch)
95    {
96       this(ch, 0, ch.length);
97    }
98
99    /**
100     * Create a new StringBuilder from the given character array
101     *
102     * @param ch the array
103     * @param start the start of the array from which to take characters
104     * @param length the lengh of the array from which to take characters
105     */

106    public JBossStringBuilder(char[] ch, int start, int length)
107    {
108       this(length + 16);
109       append(ch, start, length);
110    }
111
112    public JBossStringBuilder append(Object JavaDoc object)
113    {
114       return append(String.valueOf(object));
115    }
116
117    public JBossStringBuilder append(String JavaDoc string)
118    {
119       if (string == null)
120          string = "null";
121
122       int length = string.length();
123       if (length == 0)
124          return this;
125
126       int afterAppend = pos + length;
127       if (afterAppend > chars.length)
128          expandCapacity(afterAppend);
129
130       string.getChars(0, length, chars, pos);
131       pos = afterAppend;
132       return this;
133    }
134
135    public JBossStringBuilder append(StringBuffer JavaDoc buffer)
136    {
137       if (buffer == null)
138          return append("null");
139
140       int length = buffer.length();
141       if (length == 0)
142          return this;
143
144       int afterAppend = pos + length;
145       if (afterAppend > chars.length)
146          expandCapacity(afterAppend);
147
148       buffer.getChars(0, length, chars, pos);
149       pos = afterAppend;
150       return this;
151    }
152
153    public JBossStringBuilder append(CharSequence JavaDoc charSequence)
154    {
155       if (charSequence == null)
156          return append("null");
157
158       int length = charSequence.length();
159       if (length == 0)
160          return this;
161
162       return append(charSequence, 0, charSequence.length());
163    }
164
165    public JBossStringBuilder append(CharSequence JavaDoc charSequence, int start, int end)
166    {
167       if (charSequence == null)
168          return append("null");
169
170       if (start < 0 || end < 0 || start > end || start > charSequence.length())
171          throw new IndexOutOfBoundsException JavaDoc("Invalid start=" + start + " end=" + end + " length=" + charSequence.length());
172
173       int length = end - start;
174       if (length == 0)
175          return this;
176
177       int afterAppend = pos + length;
178       if (afterAppend > chars.length)
179          expandCapacity(afterAppend);
180
181       for (int i = start; i < end; ++i)
182          chars[pos++] = charSequence.charAt(i);
183       pos = afterAppend;
184       return this;
185    }
186
187    public JBossStringBuilder append(char[] array)
188    {
189       if (array == null)
190          return append("null");
191
192       if (array.length == 0)
193          return this;
194
195       String JavaDoc string = String.valueOf(array);
196       return append(string);
197    }
198
199    public JBossStringBuilder append(char[] array, int offset, int length)
200    {
201       if (array == null)
202          return append("null");
203
204       int arrayLength = array.length;
205       if (offset < 0 || length < 0 || offset + length > arrayLength)
206          throw new IndexOutOfBoundsException JavaDoc("Invalid offset=" + offset + " length=" + length + " array.length=" + arrayLength);
207
208       if (length == 0 || arrayLength == 0)
209          return this;
210
211       String JavaDoc string = String.valueOf(array, offset, length);
212       return append(string);
213    }
214
215    public JBossStringBuilder append(boolean primitive)
216    {
217       String JavaDoc string = String.valueOf(primitive);
218       return append(string);
219    }
220
221    public JBossStringBuilder append(char primitive)
222    {
223       String JavaDoc string = String.valueOf(primitive);
224       return append(string);
225    }
226
227    public JBossStringBuilder append(int primitive)
228    {
229       String JavaDoc string = String.valueOf(primitive);
230       return append(string);
231    }
232
233    public JBossStringBuilder append(long primitive)
234    {
235       String JavaDoc string = String.valueOf(primitive);
236       return append(string);
237    }
238
239    public JBossStringBuilder append(float primitive)
240    {
241       String JavaDoc string = String.valueOf(primitive);
242       return append(string);
243    }
244
245    public JBossStringBuilder append(double primitive)
246    {
247       String JavaDoc string = String.valueOf(primitive);
248       return append(string);
249    }
250
251    public JBossStringBuilder delete(int start, int end)
252    {
253       if (start < 0 || start > pos || start > end || end > pos)
254          throw new IndexOutOfBoundsException JavaDoc("Invalid start=" + start + " end=" + end + " length=" + pos);
255
256       if (start == end)
257          return this;
258
259       int removed = end - start;
260       System.arraycopy(chars, start + removed, chars, start, pos - end);
261       pos -= removed;
262       return this;
263    }
264
265    public JBossStringBuilder deleteCharAt(int index)
266    {
267       return delete(index, 1);
268    }
269
270    public JBossStringBuilder replace(int start, int end, String JavaDoc string)
271    {
272       delete(start, end);
273       return insert(start, string);
274    }
275
276    public JBossStringBuilder insert(int index, char[] string)
277    {
278       return insert(index, string, 0, string.length);
279    }
280
281    public JBossStringBuilder insert(int index, char[] string, int offset, int len)
282    {
283       int stringLength = string.length;
284       if (index < 0 || index > pos || offset < 0 || len < 0 || (offset + len) > string.length)
285          throw new IndexOutOfBoundsException JavaDoc("Invalid index=" + index + " offset=" + offset + " len=" + len + " string.length=" + stringLength + " length=" + pos);
286
287       if (len == 0)
288          return this;
289
290       int afterAppend = pos + len;
291       if (afterAppend > chars.length)
292          expandCapacity(afterAppend);
293
294       System.arraycopy(chars, index, chars, index + stringLength, pos - index);
295       System.arraycopy(string, offset, chars, index, len);
296       pos = afterAppend;
297       return this;
298    }
299
300    public JBossStringBuilder insert(int offset, Object JavaDoc object)
301    {
302       if (object == null)
303          return insert(offset, "null");
304       else
305          return insert(offset, String.valueOf(object));
306    }
307
308    public JBossStringBuilder insert(int offset, String JavaDoc string)
309    {
310       if (offset < 0 || offset > pos)
311          throw new IndexOutOfBoundsException JavaDoc("Invalid offset=" + offset + " length=" + pos);
312
313       if (string == null)
314          string = "null";
315
316       int stringLength = string.length();
317
318       int afterAppend = pos + stringLength;
319       if (afterAppend > chars.length)
320          expandCapacity(afterAppend);
321
322       System.arraycopy(chars, offset, chars, offset + stringLength, pos - offset);
323       string.getChars(0, stringLength, chars, offset);
324       pos = afterAppend;
325       return this;
326    }
327
328    public JBossStringBuilder insert(int offset, CharSequence JavaDoc charSequence)
329    {
330       if (charSequence == null)
331          return insert(offset, "null");
332       else
333          return insert(offset, charSequence, 0, charSequence.length());
334    }
335
336    public JBossStringBuilder insert(int offset, CharSequence JavaDoc charSequence, int start, int end)
337    {
338       if (charSequence == null)
339          charSequence = "null";
340
341       int sequenceLength = charSequence.length();
342       if (offset < 0 || offset > pos || start < 0 || end < 0 || start > sequenceLength || end > sequenceLength || start > end)
343          throw new IndexOutOfBoundsException JavaDoc("Invalid offset=" + offset + " start=" + start + " end=" + end + " sequence.length()=" + sequenceLength + " length=" + pos);
344
345       int len = end - start;
346       if (len == 0)
347          return this;
348
349       int afterAppend = pos + len;
350       if (afterAppend > chars.length)
351          expandCapacity(afterAppend);
352
353       System.arraycopy(chars, offset, chars, offset + sequenceLength, pos - offset);
354       for (int i = start; i < end; ++i)
355          chars[offset++] = charSequence.charAt(i);
356       pos = afterAppend;
357       return this;
358    }
359
360    public JBossStringBuilder insert(int offset, boolean primitive)
361    {
362       return insert(offset, String.valueOf(primitive));
363    }
364
365    public JBossStringBuilder insert(int offset, char primitive)
366    {
367       return insert(offset, String.valueOf(primitive));
368    }
369
370    public JBossStringBuilder insert(int offset, int primitive)
371    {
372       return insert(offset, String.valueOf(primitive));
373    }
374
375    public JBossStringBuilder insert(int offset, long primitive)
376    {
377       return insert(offset, String.valueOf(primitive));
378    }
379
380    public JBossStringBuilder insert(int offset, float primitive)
381    {
382       return insert(offset, String.valueOf(primitive));
383    }
384
385    public JBossStringBuilder insert(int offset, double primitive)
386    {
387       return insert(offset, String.valueOf(primitive));
388    }
389
390    public int indexOf(String JavaDoc string)
391    {
392       return indexOf(string, 0);
393    }
394
395    public int indexOf(String JavaDoc string, int fromIndex)
396    {
397       // FIXME
398
return toString().indexOf(string, fromIndex);
399    }
400
401    public int indexOf(char ch)
402    {
403       return indexOf(ch, 0);
404    }
405
406    public int indexOf(char ch, int fromIndex)
407    {
408       // FIXME
409
return toString().indexOf(ch, fromIndex);
410    }
411
412    public int lastIndexOf(String JavaDoc string)
413    {
414       return lastIndexOf(string, pos);
415    }
416
417    public int lastIndexOf(String JavaDoc string, int fromIndex)
418    {
419       // FIXME
420
return toString().lastIndexOf(string, fromIndex);
421    }
422
423    public int lastIndexOf(char ch)
424    {
425       return lastIndexOf(ch, pos);
426    }
427
428    public int lastIndexOf(char ch, int fromIndex)
429    {
430       // FIXME
431
return toString().lastIndexOf(ch, fromIndex);
432    }
433
434    public JBossStringBuilder reverse()
435    {
436       char[] tmp = new char[pos];
437       for(int n = 0; n < pos; n ++)
438          tmp[n] = chars[pos-n-1];
439       chars = tmp;
440       return this;
441    }
442
443    public String JavaDoc toString()
444    {
445       return new String JavaDoc(chars, 0, pos);
446    }
447
448    public int length()
449    {
450       return pos;
451    }
452
453    public int capacity()
454    {
455       return chars.length;
456    }
457
458    public void ensureCapacity(int minimum)
459    {
460       if (minimum < 0 || minimum < chars.length)
461          return;
462       expandCapacity(minimum);
463    }
464
465    public void trimToSize()
466    {
467       char[] trimmed = new char[pos];
468       System.arraycopy(chars, 0, trimmed, 0, pos);
469       chars = trimmed;
470    }
471
472    public void setLength(int newLength)
473    {
474       if (newLength < 0)
475           throw new StringIndexOutOfBoundsException JavaDoc(newLength);
476       if (newLength > chars.length)
477           expandCapacity(newLength);
478       Arrays.fill(chars, newLength, chars.length, '\0');
479       pos = newLength;
480    }
481
482    public char charAt(int index)
483    {
484       return chars[index];
485    }
486
487    public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
488    {
489       if (srcBegin < 0 || dstBegin < 0 || srcBegin > srcEnd || srcEnd > pos || (dstBegin + srcEnd - srcBegin) > dst.length)
490          throw new IndexOutOfBoundsException JavaDoc("Invalid srcBegin=" + srcBegin + " srcEnd=" + srcEnd + " dstBegin=" + dstBegin + " dst.length=" + dst.length + " length=" + pos);
491
492       int len = srcEnd - srcBegin;
493       if (len == 0)
494          return;
495
496       System.arraycopy(chars, srcBegin, dst, dstBegin, len);
497    }
498
499    public void setCharAt(int index, char ch)
500    {
501       if (index < 0 || index > pos)
502          throw new IndexOutOfBoundsException JavaDoc("Invalid index=" + index + " length=" + pos);
503
504       chars[index] = ch;
505    }
506
507    public String JavaDoc substring(int start)
508    {
509       return substring(start, pos);
510    }
511
512    public CharSequence JavaDoc subSequence(int start, int end)
513    {
514       return substring(start, end);
515    }
516
517    public String JavaDoc substring(int start, int end)
518    {
519       if (start < 0 || end < 0 || start > end || end > pos)
520          throw new IndexOutOfBoundsException JavaDoc("Invalid start=" + start + " end=" + end + " length=" + pos);
521
522       return new String JavaDoc(chars, start, end - start);
523    }
524
525    /**
526     * Expand the capacity to the greater of the minimum
527     * or twice the current size
528     *
529     * @param minimum the minimum
530     */

531    protected void expandCapacity(int minimum)
532    {
533       int newSize = chars.length * 2;
534       if (minimum > newSize)
535          newSize = minimum;
536
537       char[] newChars = new char[newSize];
538       System.arraycopy(chars, 0, newChars, 0, pos);
539       chars = newChars;
540    }
541 }
542
Popular Tags