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  import java.io.IOException;
18  import java.io.Writer;
19  
20  /**
21   * <a href="StringBundler.java.html"><b><i>View Source</i></b></a>
22   *
23   * <p>
24   * See http://issues.liferay.com/browse/LPS-6072.
25   * </p>
26   *
27   * @author Shuyang Zhou
28   * @author Brian Wing Shun Chan
29   */
30  public class StringBundler {
31  
32      public StringBundler() {
33          _array = new String[_DEFAULT_ARRAY_CAPACITY];
34      }
35  
36      public StringBundler(int initialCapacity) {
37          if (initialCapacity <= 0) {
38              throw new IllegalArgumentException();
39          }
40  
41          _array = new String[initialCapacity];
42      }
43  
44      public StringBundler(String s) {
45          _array = new String[_DEFAULT_ARRAY_CAPACITY];
46  
47          _array[0] = s;
48  
49          _arrayIndex = 1;
50      }
51  
52      public StringBundler append(boolean b) {
53          if (b) {
54              return append(_TRUE);
55          }
56          else {
57              return append(_FALSE);
58          }
59      }
60  
61      public StringBundler append(char c) {
62          return append(String.valueOf(c));
63      }
64  
65      public StringBundler append(char[] chars) {
66          return append(new String(chars));
67      }
68  
69      public StringBundler append(double d) {
70          return append(Double.toString(d));
71      }
72  
73      public StringBundler append(float f) {
74          return append(Float.toString(f));
75      }
76  
77      public StringBundler append(int i) {
78          return append(Integer.toString(i));
79      }
80  
81      public StringBundler append(long l) {
82          return append(Long.toString(l));
83      }
84  
85      public StringBundler append(Object obj) {
86          return append(String.valueOf(obj));
87      }
88  
89      public StringBundler append(String s) {
90          if (s == null) {
91              s = StringPool.NULL;
92          }
93  
94          if (s.length() == 0) {
95              return this;
96          }
97  
98          if (_arrayIndex >= _array.length) {
99              expandCapacity(_array.length * 2);
100         }
101 
102         _array[_arrayIndex++] = s;
103 
104         return this;
105     }
106 
107     public StringBundler append(StringBundler sb) {
108         if ((sb == null) || (sb._arrayIndex == 0)) {
109             return this;
110         }
111 
112         if ((_array.length - _arrayIndex) < sb._arrayIndex) {
113             expandCapacity((_array.length + sb._arrayIndex) * 2);
114         }
115 
116         System.arraycopy(sb._array, 0, _array, _arrayIndex, sb._arrayIndex);
117 
118         _arrayIndex += sb._arrayIndex;
119 
120         return this;
121     }
122 
123     public int capacity() {
124         return _array.length;
125     }
126 
127     public int index() {
128         return _arrayIndex;
129     }
130 
131     public int length() {
132         int length = 0;
133 
134         for (int i = 0; i < _arrayIndex; i++) {
135             length += _array[i].length();
136         }
137 
138         return length;
139     }
140 
141     public void setIndex(int newIndex) {
142         if (newIndex < 0) {
143             throw new ArrayIndexOutOfBoundsException(newIndex);
144         }
145 
146         if (newIndex > _array.length) {
147             String[] newArray = new String[newIndex];
148 
149             System.arraycopy(_array, 0, newArray, 0, _arrayIndex);
150 
151             _array = newArray;
152         }
153 
154         if (_arrayIndex < newIndex) {
155             for( int i = _arrayIndex; i < newIndex; i++) {
156                 _array[i] = StringPool.BLANK;
157             }
158         }
159 
160         if (_arrayIndex > newIndex) {
161             for (int i = newIndex; i < _arrayIndex; i++) {
162                 _array[i] = null;
163             }
164         }
165 
166         _arrayIndex = newIndex;
167     }
168 
169     public String stringAt(int index) {
170         if (index >= _arrayIndex) {
171             throw new ArrayIndexOutOfBoundsException();
172         }
173 
174         return _array[index];
175     }
176 
177     public String toString() {
178         if (_arrayIndex == 0) {
179             return StringPool.BLANK;
180         }
181 
182         String s = null;
183 
184         if (_arrayIndex <= 3) {
185             s = _array[0];
186 
187             for (int i = 1; i < _arrayIndex; i++) {
188                 s = s.concat(_array[i]);
189             }
190         }
191         else {
192             int length = 0;
193 
194             for (int i = 0; i < _arrayIndex; i++) {
195                 length += _array[i].length();
196             }
197 
198             StringBuilder sb = new StringBuilder(length);
199 
200             for (int i = 0; i < _arrayIndex; i++) {
201                 sb.append(_array[i]);
202             }
203 
204             s = sb.toString();
205         }
206 
207         return s;
208     }
209 
210     public void writeTo(Writer writer) throws IOException {
211         for(int i = 0; i < _arrayIndex; i++) {
212             writer.write(_array[i]);
213         }
214     }
215 
216     protected void expandCapacity(int newCapacity) {
217         String[] newArray = new String[newCapacity];
218 
219         System.arraycopy(_array, 0, newArray, 0, _arrayIndex);
220 
221         _array = newArray;
222     }
223 
224     private static final int _DEFAULT_ARRAY_CAPACITY = 16;
225 
226     private static final String _FALSE = "false";
227 
228     private static final String _TRUE = "true";
229 
230     private String[] _array;
231     private int _arrayIndex;
232 
233 }