1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  /**
26   * <a href="StringMaker.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Harry Mark
29   *
30   */
31  public class StringMaker {
32  
33      static boolean collect = false;
34  
35      static {
36          String collectString = System.getProperty(MakerStats.class.getName());
37  
38          if (collectString != null) {
39              if (collectString.equals("true")) {
40                  collect = true;
41              }
42          }
43      }
44  
45      static MakerStats stats = null;
46  
47      static {
48          if (collect) {
49              stats = new MakerStats(StringMaker.class.toString());
50          }
51      }
52  
53      static int defaultInitSize = 128;
54  
55      static {
56          String defaultInitSizeString = System.getProperty(
57              StringMaker.class.getName() + ".initial.size");
58  
59          if (defaultInitSizeString != null) {
60              try {
61                  defaultInitSize = Integer.parseInt(defaultInitSizeString);
62              }
63              catch (Exception e) {
64                  e.printStackTrace();
65              }
66          }
67      }
68  
69      public static MakerStats getStatistics() {
70          return stats;
71      }
72  
73      public StringMaker() {
74          _sb = new StringBuffer(defaultInitSize);
75  
76          if (collect) {
77              _getInfo(new Throwable());
78          }
79      }
80  
81      public StringMaker(int capacity) throws NegativeArraySizeException {
82          _sb = new StringBuffer(capacity);
83  
84          if (collect) {
85              _getInfo(new Throwable());
86          }
87      }
88  
89      public StringMaker(String s) throws NullPointerException {
90          if (s == null) {
91              throw new NullPointerException();
92          }
93  
94          _sb = new StringBuffer(s.length() + defaultInitSize);
95  
96          if (collect) {
97              _getInfo(new Throwable());
98          }
99  
100         _sb.append(s);
101     }
102 
103     public StringMaker(StringBuffer sb) throws NullPointerException {
104         if (sb == null) {
105             _sb = new StringBuffer(defaultInitSize);
106         }
107         else {
108             _sb = sb;
109         }
110 
111         if (collect) {
112             _getInfo(new Throwable());
113         }
114     }
115 
116     public StringMaker append(Object obj) {
117         _sb.append(obj);
118 
119         return this;
120     }
121 
122     public StringMaker append(String s) {
123         _sb.append(s);
124 
125         return this;
126     }
127 
128     public StringMaker append(StringBuffer sb) {
129         sb.append(sb);
130 
131         return this;
132     }
133 
134     public StringMaker append(boolean b) {
135         _sb.append(b);
136 
137         return this;
138     }
139 
140     public StringMaker append(char c) {
141         _sb.append(c);
142 
143         return this;
144     }
145 
146     public StringMaker append(char[] array) {
147         _sb.append(array);
148 
149         return this;
150     }
151 
152     public StringMaker append(char[] array, int offset, int len) {
153         _sb.append(array, offset, len);
154 
155         return this;
156     }
157 
158     public StringMaker append(double d) {
159         _sb.append(d);
160 
161         return this;
162     }
163 
164     public StringMaker append(float f) {
165         _sb.append(f);
166 
167         return this;
168     }
169 
170     public StringMaker append(int i) {
171         _sb.append(i);
172 
173         return this;
174     }
175 
176     public StringMaker append(long l) {
177         _sb.append(l);
178 
179         return this;
180     }
181 
182     public int capacity() {
183         return _sb.capacity();
184     }
185 
186     public char charAt(int index) {
187         return _sb.charAt(index);
188     }
189 
190     public StringMaker delete(int start, int end) {
191         _sb.delete(start, end);
192 
193         return this;
194     }
195 
196     public StringMaker deleteCharAt(int index) {
197         _sb.deleteCharAt(index);
198 
199         return this;
200     }
201 
202     public void ensureCapacity(int minimumCapacity) {
203         _sb.ensureCapacity(minimumCapacity);
204     }
205 
206     public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
207         _sb.getChars(srcBegin, srcEnd, dst, dstBegin);
208     }
209 
210     public StringBuffer getStringBuffer() {
211         return _sb;
212     }
213 
214     public int indexOf(String s) {
215         return _sb.indexOf(s);
216     }
217 
218     public int indexOf(String s, int fromIndex) {
219         return _sb.indexOf(s, fromIndex);
220     }
221 
222     public StringMaker insert(int offset, boolean b) {
223         _sb.insert(offset, b);
224 
225         return this;
226     }
227 
228     public StringMaker insert(int offset, double d) {
229         _sb.insert(offset, d);
230 
231         return this;
232     }
233 
234     public StringMaker insert(int offset, float f) {
235         _sb.insert(offset, f);
236 
237         return this;
238     }
239 
240     public StringMaker insert(int offset, int i) {
241         _sb.insert(offset, i);
242 
243         return this;
244     }
245 
246     public StringMaker insert(int offset, long l) {
247         _sb.insert(offset, l);
248 
249         return this;
250     }
251 
252     public StringMaker insert(int index, char[] array, int offset, int len) {
253         _sb.insert(index, array, offset, len);
254 
255         return this;
256     }
257 
258     public StringMaker insert(int offset, Object obj) {
259         _sb.insert(offset, obj);
260 
261         return this;
262     }
263 
264     public StringMaker insert(int offset, String s) {
265         _sb.insert(offset, s);
266 
267         return this;
268     }
269 
270     public StringMaker insert(int offset, char c) {
271         _sb.insert(offset, c);
272 
273         return this;
274     }
275 
276     public StringMaker insert(int offset, char[] array) {
277         _sb.insert(offset, array);
278 
279         return this;
280     }
281 
282     public int lastIndexOf(String s) {
283         return _sb.lastIndexOf(s);
284     }
285 
286     public int lastIndexOf(String s, int fromIndex) {
287         return _sb.lastIndexOf(s, fromIndex);
288     }
289 
290     public int length() {
291         return _sb.length();
292     }
293 
294     public StringMaker replace(int start, int end, String s) {
295         _sb.replace(start, end, s);
296 
297         return this;
298     }
299 
300     public StringMaker reverse() {
301         _sb.reverse();
302 
303         return this;
304     }
305 
306     public void setCharAt(int index, char ch) {
307         _sb.setCharAt(index, ch);
308     }
309 
310     public void setLength(int len) {
311         _sb.setLength(len);
312     }
313 
314     public String substring(int start) {
315         return _sb.substring(start);
316     }
317 
318     public String substring(int start, int end) {
319         return _sb.substring(start, end);
320     }
321 
322     public String toString() {
323         if (collect) {
324             stats.add(_caller, _initSize, _sb.length());
325         }
326 
327         return _sb.toString();
328     }
329 
330     private void _getInfo(Throwable t) {
331         _initSize = _sb.capacity();
332 
333         StackTraceElement[] elements = t.getStackTrace();
334 
335         if (elements.length > 1) {
336             StackTraceElement el = elements[1];
337 
338             _caller =
339                 el.getClassName() + StringPool.PERIOD + el.getMethodName() +
340                     StringPool.COLON + el.getLineNumber();
341         }
342     }
343 
344     private StringBuffer _sb;
345     private int _initSize;
346     private String _caller;
347 
348 }