1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  /**
18   * <a href="StringBundler.java.html"><b><i>View Source</i></b></a>
19   *
20   * <p>
21   * See http://issues.liferay.com/browse/LPS-6072.
22   * </p>
23   *
24   * @author Shuyang Zhou
25   * @author Brian Wing Shun Chan
26   */
27  public class StringBundler {
28  
29      public StringBundler() {
30          _array = new String[_DEFAULT_ARRAY_CAPACITY];
31      }
32  
33      public StringBundler(int initialCapacity) {
34          if (initialCapacity <= 0) {
35              throw new IllegalArgumentException();
36          }
37  
38          _array = new String[initialCapacity];
39      }
40  
41      public StringBundler(String s) {
42          _array = new String[_DEFAULT_ARRAY_CAPACITY];
43  
44          _array[0] = s;
45  
46          _arrayIndex = 1;
47      }
48  
49      public StringBundler append(boolean b) {
50          if (b) {
51              return append(_TRUE);
52          }
53          else {
54              return append(_FALSE);
55          }
56      }
57  
58      public StringBundler append(char c) {
59          return append(String.valueOf(c));
60      }
61  
62      public StringBundler append(char[] chars) {
63          return append(new String(chars));
64      }
65  
66      public StringBundler append(double d) {
67          return append(Double.toString(d));
68      }
69  
70      public StringBundler append(float f) {
71          return append(Float.toString(f));
72      }
73  
74      public StringBundler append(int i) {
75          return append(Integer.toString(i));
76      }
77  
78      public StringBundler append(long l) {
79          return append(Long.toString(l));
80      }
81  
82      public StringBundler append(Object obj) {
83          return append(String.valueOf(obj));
84      }
85  
86      public StringBundler append(String s) {
87          if (s == null) {
88              s = StringPool.NULL;
89          }
90  
91          if (_arrayIndex >= _array.length) {
92              expandCapacity(_array.length * 2);
93          }
94  
95          _array[_arrayIndex++] = s;
96  
97          return this;
98      }
99  
100     public StringBundler append(StringBundler sb) {
101         if (sb == null) {
102             return this;
103         }
104 
105         if ((_array.length - _arrayIndex) < sb._arrayIndex) {
106             expandCapacity((_array.length + sb._arrayIndex) * 2);
107         }
108 
109         System.arraycopy(sb._array, 0, _array, _arrayIndex, sb._arrayIndex);
110 
111         _arrayIndex += sb._arrayIndex;
112 
113         return this;
114     }
115 
116     public int capacity() {
117         return _array.length;
118     }
119 
120     public int index() {
121         return _arrayIndex;
122     }
123 
124     public int length() {
125         int length = 0;
126 
127         for (int i = 0; i < _arrayIndex; i++) {
128             length += _array[i].length();
129         }
130 
131         return length;
132     }
133 
134     public void setIndex(int newIndex) {
135         if (newIndex < 0) {
136             throw new ArrayIndexOutOfBoundsException(newIndex);
137         }
138 
139         if (newIndex > _array.length) {
140             String[] newArray = new String[newIndex];
141 
142             System.arraycopy(_array, 0, newArray, 0, _arrayIndex);
143 
144             _array = newArray;
145         }
146 
147         if (_arrayIndex < newIndex) {
148             for( int i = _arrayIndex; i < newIndex; i++) {
149                 _array[i] = StringPool.BLANK;
150             }
151         }
152 
153         if (_arrayIndex > newIndex) {
154             for (int i = newIndex; i < _arrayIndex; i++) {
155                 _array[i] = null;
156             }
157         }
158 
159         _arrayIndex = newIndex;
160     }
161 
162     public String stringAt(int index) {
163         if (index >= _arrayIndex) {
164             throw new ArrayIndexOutOfBoundsException();
165         }
166 
167         return _array[index];
168     }
169 
170     public String toString() {
171         if (_arrayIndex == 0) {
172             return StringPool.BLANK;
173         }
174 
175         String s = null;
176 
177         if (_arrayIndex <= 3) {
178             s = _array[0];
179 
180             for (int i = 1; i < _arrayIndex; i++) {
181                 s = s.concat(_array[i]);
182             }
183         }
184         else {
185             int length = 0;
186 
187             for (int i = 0; i < _arrayIndex; i++) {
188                 length += _array[i].length();
189             }
190 
191             StringBuilder sb = new StringBuilder(length);
192 
193             for (int i = 0; i < _arrayIndex; i++) {
194                 sb.append(_array[i]);
195             }
196 
197             s = sb.toString();
198         }
199 
200         return s;
201     }
202 
203     protected void expandCapacity(int newCapacity) {
204         String[] newArray = new String[newCapacity];
205 
206         System.arraycopy(_array, 0, newArray, 0, _arrayIndex);
207 
208         _array = newArray;
209     }
210 
211     private static final int _DEFAULT_ARRAY_CAPACITY = 16;
212 
213     private static final String _FALSE = "false";
214 
215     private static final String _TRUE = "true";
216 
217     private String[] _array;
218     private int _arrayIndex;
219 
220 }