View Javadoc

1   /*
2    * $Id: StringAppender.java,v 1.9 2010/03/19 14:53:29 oeuillot Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.lang;
6   
7   import java.io.IOException;
8   import java.io.UnsupportedEncodingException;
9   import java.io.Writer;
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  
14  /**
15   * 
16   * @author Java Team (latest modification by $Author: oeuillot $)
17   * @version $Revision: 1.9 $ $Date: 2010/03/19 14:53:29 $
18   */
19  public final class StringAppender implements CharSequence {
20      private static final String REVISION = "$Revision: 1.9 $";
21  
22      private static final Log LOG = LogFactory.getLog(StringAppender.class);
23  
24      private char value[];
25  
26      private int count;
27  
28      public StringAppender() {
29          this(16);
30      }
31  
32      public StringAppender(int length) {
33          value = new char[length];
34      }
35  
36      public StringAppender(String str) {
37          this(str, 16);
38      }
39  
40      public StringAppender(String str, int length) {
41          this(str.length() + length);
42          append(str);
43      }
44  
45      public StringAppender(char ch) {
46          this(4);
47          value[count++] = ch;
48      }
49  
50      public int length() {
51          return count;
52      }
53  
54      private void expandCapacity(int minimumCapacity) {
55          int newCapacity = (value.length + 1) * 2;
56          if (newCapacity < 0) {
57              newCapacity = Integer.MAX_VALUE;
58  
59          } else if (minimumCapacity > newCapacity) {
60              newCapacity = minimumCapacity;
61          }
62  
63          if (LOG.isTraceEnabled()) {
64              LOG.trace("Expand capacity from '" + value.length + "' to '"
65                      + newCapacity + "'.");
66          }
67  
68          char newValue[] = new char[newCapacity];
69          System.arraycopy(value, 0, newValue, 0, count);
70          value = newValue;
71      }
72  
73      public void setLength(int newLength) {
74          if (newLength < 0) {
75              throw new StringIndexOutOfBoundsException(newLength);
76          }
77  
78          if (newLength > value.length) {
79              expandCapacity(newLength);
80          }
81  
82          if (count < newLength) {
83              for (; count < newLength; count++) {
84                  value[count] = '\0';
85              }
86              return;
87          }
88  
89          count = newLength;
90      }
91  
92      public char charAt(int index) {
93          if ((index < 0) || (index >= count)) {
94              throw new StringIndexOutOfBoundsException(index);
95          }
96          return value[index];
97      }
98  
99      private void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
100         if (srcBegin < 0) {
101             throw new StringIndexOutOfBoundsException(srcBegin);
102         }
103         if ((srcEnd < 0) || (srcEnd > count)) {
104             throw new StringIndexOutOfBoundsException(srcEnd);
105         }
106         if (srcBegin > srcEnd) {
107             throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
108         }
109         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
110     }
111 
112     /*
113      * public void setCharAt(int index, char ch) { if ((index < 0) || (index >=
114      * count)) { throw new StringIndexOutOfBoundsException(index); }
115      * value[index] = ch; }
116      */
117     public StringAppender append(String str) {
118         if (str == null) {
119             str = String.valueOf(str);
120         }
121 
122         int len = str.length();
123         if (len == 0) {
124             return this;
125         }
126 
127         int newcount = count + len;
128         if (newcount > value.length) {
129             expandCapacity(newcount);
130         }
131 
132         str.getChars(0, len, value, count);
133         count = newcount;
134 
135         return this;
136     }
137 
138     public StringAppender append(StringAppender sb) {
139         if (sb == null) {
140             return append((String) null);
141         }
142 
143         int len = sb.length();
144         int newcount = count + len;
145         if (newcount > value.length) {
146             expandCapacity(newcount);
147         }
148 
149         sb.getChars(0, len, value, count);
150         count = newcount;
151         return this;
152     }
153 
154     public StringAppender append(StringAppender sb, int offset, int len) {
155         if (sb == null) {
156             return append((String) null);
157         }
158 
159         int newcount = count + len;
160         if (newcount > value.length) {
161             expandCapacity(newcount);
162         }
163 
164         sb.getChars(offset, offset + len, value, count);
165         count = newcount;
166         return this;
167     }
168 
169     public StringAppender append(char str[]) {
170         return append(str, 0, str.length);
171     }
172 
173     public StringAppender append(char str[], int offset, int len) {
174         int newcount = count + len;
175         if (newcount > value.length) {
176             expandCapacity(newcount);
177         }
178 
179         System.arraycopy(str, offset, value, count, len);
180         count = newcount;
181 
182         return this;
183     }
184 
185     public StringAppender append(String text, int offset, int len) {
186         int newcount = count + len;
187         if (newcount > value.length) {
188             expandCapacity(newcount);
189         }
190 
191         text.getChars(offset, offset + len, value, count);
192         count = newcount;
193 
194         return this;
195     }
196 
197     public StringAppender append(boolean b) {
198         if (b) {
199             return append("true");
200         }
201 
202         return append("false");
203     }
204 
205     public StringAppender append(char c) {
206         return append(c, 1);
207     }
208 
209     public StringAppender append(char c, int nb) {
210         if (nb < 1) {
211             return this;
212         }
213         int newcount = count + nb;
214         if (newcount > value.length) {
215             expandCapacity(newcount);
216         }
217 
218         for (; nb > 0; nb--) {
219             value[count++] = c;
220         }
221         return this;
222     }
223 
224     public StringAppender append(int i) {
225         return append(String.valueOf(i));
226     }
227 
228     public StringAppender append(long l) {
229         return append(String.valueOf(l));
230     }
231 
232     public StringAppender append(float f) {
233         return append(String.valueOf(f));
234     }
235 
236     public String toString() {
237         if (count == 0) {
238             return "";
239         }
240         return new String(value, 0, count);
241     }
242 
243     public void copyInto(Writer writer) throws IOException {
244         if (count < 1) {
245             return;
246         }
247 
248         writer.write(value, 0, count);
249     }
250 
251     public void ensure(int length) {
252         int newcount = count + length;
253         if (newcount <= value.length) {
254             return;
255         }
256         expandCapacity(newcount);
257     }
258 
259     public StringAppender insert(int offset, String str) {
260         if ((offset < 0) || (offset > count)) {
261             throw new StringIndexOutOfBoundsException();
262         }
263 
264         if (str == null) {
265             str = String.valueOf(str);
266         }
267         int len = str.length();
268         int newcount = count + len;
269         if (newcount > value.length) {
270             expandCapacity(newcount);
271         }
272 
273         System.arraycopy(value, offset, value, offset + len, count - offset);
274         str.getChars(0, len, value, offset);
275         count = newcount;
276         return this;
277     }
278 
279     public StringAppender insert(int offset, char c) {
280         return insert(offset, c, 1);
281     }
282 
283     public StringAppender insert(int offset, char c, int nb) {
284         if (nb < 1) {
285             return this;
286         }
287         int newcount = count + nb;
288         if (newcount > value.length) {
289             expandCapacity(newcount);
290         }
291 
292         System.arraycopy(value, offset, value, offset + nb, count - offset);
293         for (; nb > 0; nb--) {
294             value[offset++] = c;
295         }
296         count = newcount;
297         return this;
298     }
299 
300     public Writer createWriter() {
301         return new Writer() {
302             private static final String REVISION = "$Revision: 1.9 $";
303 
304             public void close() {
305             }
306 
307             public void flush() {
308             }
309 
310             public void write(char[] cbuf, int off, int len) {
311                 StringAppender.this.append(cbuf, off, len);
312             }
313 
314             public void write(char c) {
315                 StringAppender.this.append(c);
316             }
317 
318             public void write(String s, int off, int len) {
319                 StringAppender.this.append(s, off, len);
320             }
321 
322             public void write(String s) {
323                 StringAppender.this.append(s);
324             }
325         };
326     }
327 
328     public CharSequence subSequence(int start, int end) {
329         StringAppender sa = new StringAppender(end - start);
330 
331         sa.append(value, start, end - start);
332 
333         return sa;
334     }
335 
336     public byte[] getBytes(String charsetName)
337             throws UnsupportedEncodingException {
338         return toString().getBytes(charsetName);
339     }
340 }