1
22
23 package com.liferay.portal.kernel.util;
24
25
35 public class StringBundler {
36
37 public StringBundler() {
38 _array = new String[_DEFAULT_ARRAY_CAPACITY];
39 }
40
41 public StringBundler(int initialCapacity) {
42 if (initialCapacity <= 0) {
43 throw new IllegalArgumentException();
44 }
45
46 _array = new String[initialCapacity];
47 }
48
49 public StringBundler(String s) {
50 _array = new String[_DEFAULT_ARRAY_CAPACITY];
51
52 _array[0] = s;
53
54 _arrayIndex = 1;
55 }
56
57 public StringBundler append(boolean b) {
58 if (b) {
59 return append(_TRUE);
60 }
61 else {
62 return append(_FALSE);
63 }
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();
93 }
94
95 _array[_arrayIndex++] = s;
96
97 return this;
98 }
99
100 public int capacity() {
101 return _array.length;
102 }
103
104 public int index() {
105 return _arrayIndex;
106 }
107
108 public void setIndex(int newIndex) {
109 if (newIndex < 0) {
110 throw new ArrayIndexOutOfBoundsException(newIndex);
111 }
112
113 if (newIndex > _array.length) {
114 String[] newArray = new String[newIndex];
115
116 System.arraycopy(_array, 0, newArray, 0, _arrayIndex);
117
118 _array = newArray;
119 }
120
121 if (_arrayIndex < newIndex) {
122 for( int i = _arrayIndex; i < newIndex; i++) {
123 _array[i] = StringPool.BLANK;
124 }
125 }
126
127 if (_arrayIndex > newIndex) {
128 for (int i = newIndex; i < _arrayIndex; i++) {
129 _array[i] = null;
130 }
131 }
132
133 _arrayIndex = newIndex;
134 }
135
136 public String stringAt(int index) {
137 if (index >= _arrayIndex) {
138 throw new ArrayIndexOutOfBoundsException();
139 }
140
141 return _array[index];
142 }
143
144 public String toString() {
145 if (_arrayIndex == 0) {
146 return StringPool.BLANK;
147 }
148
149 String s = null;
150
151 if (_arrayIndex <= 3) {
152 s = _array[0];
153
154 for (int i = 1; i < _arrayIndex; i++) {
155 s = s.concat(_array[i]);
156 }
157 }
158 else {
159 int length = 0;
160
161 for (int i = 0; i < _arrayIndex; i++) {
162 length += _array[i].length();
163 }
164
165 StringBuilder sb = new StringBuilder(length);
166
167 for (int i = 0; i < _arrayIndex; i++) {
168 sb.append(_array[i]);
169 }
170
171 s = sb.toString();
172 }
173
174 return s;
175 }
176
177 protected void expandCapacity() {
178 String[] newArray = new String[_array.length << 1];
179
180 System.arraycopy(_array, 0, newArray, 0, _arrayIndex);
181
182 _array = newArray;
183 }
184
185 private static final int _DEFAULT_ARRAY_CAPACITY = 16;
186
187 private static final String _FALSE = "false";
188
189 private static final String _TRUE = "true";
190
191 private String[] _array;
192 private int _arrayIndex;
193
194 }