001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: /**
024: * <a href="StringMaker.java.html"><b><i>View Source</i></b></a>
025: *
026: * @author Harry Mark
027: *
028: */
029: public class StringMaker {
030:
031: static boolean collect = false;
032:
033: static {
034: String collectString = System.getProperty(MakerStats.class
035: .getName());
036:
037: if (collectString != null) {
038: if (collectString.equals("true")) {
039: collect = true;
040: }
041: }
042: }
043:
044: static MakerStats stats = null;
045:
046: static {
047: if (collect) {
048: stats = new MakerStats(StringMaker.class.toString());
049: }
050: }
051:
052: static int defaultInitSize = 128;
053:
054: static {
055: String defaultInitSizeString = System
056: .getProperty(StringMaker.class.getName()
057: + ".initial.size");
058:
059: if (defaultInitSizeString != null) {
060: try {
061: defaultInitSize = Integer
062: .parseInt(defaultInitSizeString);
063: } catch (Exception e) {
064: e.printStackTrace();
065: }
066: }
067: }
068:
069: public static MakerStats getStatistics() {
070: return stats;
071: }
072:
073: public StringMaker() {
074: _sb = new StringBuffer(defaultInitSize);
075:
076: if (collect) {
077: _getInfo(new Throwable());
078: }
079: }
080:
081: public StringMaker(int capacity) throws NegativeArraySizeException {
082: _sb = new StringBuffer(capacity);
083:
084: if (collect) {
085: _getInfo(new Throwable());
086: }
087: }
088:
089: public StringMaker(String s) throws NullPointerException {
090: if (s == null) {
091: throw new NullPointerException();
092: }
093:
094: _sb = new StringBuffer(s.length() + defaultInitSize);
095:
096: if (collect) {
097: _getInfo(new Throwable());
098: }
099:
100: _sb.append(s);
101: }
102:
103: public StringMaker(StringBuffer sb) throws NullPointerException {
104: if (sb == null) {
105: _sb = new StringBuffer(defaultInitSize);
106: } else {
107: _sb = sb;
108: }
109:
110: if (collect) {
111: _getInfo(new Throwable());
112: }
113: }
114:
115: public StringMaker append(Object obj) {
116: _sb.append(obj);
117:
118: return this ;
119: }
120:
121: public StringMaker append(String s) {
122: _sb.append(s);
123:
124: return this ;
125: }
126:
127: public StringMaker append(StringBuffer sb) {
128: sb.append(sb);
129:
130: return this ;
131: }
132:
133: public StringMaker append(boolean b) {
134: _sb.append(b);
135:
136: return this ;
137: }
138:
139: public StringMaker append(char c) {
140: _sb.append(c);
141:
142: return this ;
143: }
144:
145: public StringMaker append(char[] array) {
146: _sb.append(array);
147:
148: return this ;
149: }
150:
151: public StringMaker append(char[] array, int offset, int len) {
152: _sb.append(array, offset, len);
153:
154: return this ;
155: }
156:
157: public StringMaker append(double d) {
158: _sb.append(d);
159:
160: return this ;
161: }
162:
163: public StringMaker append(float f) {
164: _sb.append(f);
165:
166: return this ;
167: }
168:
169: public StringMaker append(int i) {
170: _sb.append(i);
171:
172: return this ;
173: }
174:
175: public StringMaker append(long l) {
176: _sb.append(l);
177:
178: return this ;
179: }
180:
181: public int capacity() {
182: return _sb.capacity();
183: }
184:
185: public char charAt(int index) {
186: return _sb.charAt(index);
187: }
188:
189: public StringMaker delete(int start, int end) {
190: _sb.delete(start, end);
191:
192: return this ;
193: }
194:
195: public StringMaker deleteCharAt(int index) {
196: _sb.deleteCharAt(index);
197:
198: return this ;
199: }
200:
201: public void ensureCapacity(int minimumCapacity) {
202: _sb.ensureCapacity(minimumCapacity);
203: }
204:
205: public void getChars(int srcBegin, int srcEnd, char[] dst,
206: 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,
253: int len) {
254: _sb.insert(index, array, offset, len);
255:
256: return this ;
257: }
258:
259: public StringMaker insert(int offset, Object obj) {
260: _sb.insert(offset, obj);
261:
262: return this ;
263: }
264:
265: public StringMaker insert(int offset, String s) {
266: _sb.insert(offset, s);
267:
268: return this ;
269: }
270:
271: public StringMaker insert(int offset, char c) {
272: _sb.insert(offset, c);
273:
274: return this ;
275: }
276:
277: public StringMaker insert(int offset, char[] array) {
278: _sb.insert(offset, array);
279:
280: return this ;
281: }
282:
283: public int lastIndexOf(String s) {
284: return _sb.lastIndexOf(s);
285: }
286:
287: public int lastIndexOf(String s, int fromIndex) {
288: return _sb.lastIndexOf(s, fromIndex);
289: }
290:
291: public int length() {
292: return _sb.length();
293: }
294:
295: public StringMaker replace(int start, int end, String s) {
296: _sb.replace(start, end, s);
297:
298: return this ;
299: }
300:
301: public StringMaker reverse() {
302: _sb.reverse();
303:
304: return this ;
305: }
306:
307: public void setCharAt(int index, char ch) {
308: _sb.setCharAt(index, ch);
309: }
310:
311: public void setLength(int len) {
312: _sb.setLength(len);
313: }
314:
315: public String substring(int start) {
316: return _sb.substring(start);
317: }
318:
319: public String substring(int start, int end) {
320: return _sb.substring(start, end);
321: }
322:
323: public String toString() {
324: if (collect) {
325: stats.add(_caller, _initSize, _sb.length());
326: }
327:
328: return _sb.toString();
329: }
330:
331: private void _getInfo(Throwable t) {
332: _initSize = _sb.capacity();
333:
334: StackTraceElement[] elements = t.getStackTrace();
335:
336: if (elements.length > 1) {
337: StackTraceElement el = elements[1];
338:
339: _caller = el.getClassName() + StringPool.PERIOD
340: + el.getMethodName() + StringPool.COLON
341: + el.getLineNumber();
342: }
343: }
344:
345: private StringBuffer _sb;
346: private int _initSize;
347: private String _caller;
348:
349: }
|