001: /*
002: * $Id: StringList.java 458670 2006-01-15 10:53:49Z jonl $ $Revision: 458670 $
003: * $Date: 2006-01-15 11:53:49 +0100 (Sun, 15 Jan 2006) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.util.string;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.StringTokenizer;
026:
027: /**
028: * A typesafe, mutable list of strings supporting a variety of convenient
029: * operations as well as expected operations from List such as add(), size(),
030: * remove(), iterator(), get(int index) and toArray(). Instances of the class
031: * are not threadsafe.
032: * <p>
033: * StringList objects can be constructed empty or they can be created using any
034: * of several static factory methods:
035: * <ul>
036: * <li>valueOf(String[])
037: * <li>valueOf(String)
038: * <li>valueOf(Collection)
039: * <li>valueOf(Object[])
040: * </ul>
041: * In the case of the Collection and Object[] factory methods, each Object in
042: * the collection or array is converted to a String via toString() before being
043: * added to the StringList.
044: * <p>
045: * The tokenize() factory methods allow easy creation of StringLists via
046: * StringTokenizer. The repeat() static factory method creates a StringList that
047: * repeats a given String a given number of times.
048: * <p>
049: * The prepend() method adds a String to the beginning of the StringList. The
050: * removeLast() method pops a String off the end of the list. The sort() method
051: * sorts strings in the List using Collections.sort(). The class also inherits
052: * useful methods from AbstractStringList that include join() methods ala Perl
053: * and a toString() method which joins the list of strings with comma separators
054: * for easy viewing.
055: *
056: * @author Jonathan Locke
057: */
058: public final class StringList extends AbstractStringList {
059: private static final long serialVersionUID = 1L;
060:
061: // The underlying list of strings
062: private final List strings;
063:
064: // The total length of all strings in the list
065: private int totalLength;
066:
067: /**
068: * Returns a list of a string repeated a given number of times.
069: *
070: * @param count
071: * The number of times to repeat the string
072: * @param string
073: * The string to repeat
074: * @return The list of strings
075: */
076: public static StringList repeat(final int count, final String string) {
077: final StringList list = new StringList(count);
078:
079: for (int i = 0; i < count; i++) {
080: list.add(string);
081: }
082:
083: return list;
084: }
085:
086: /**
087: * Extracts tokens from a comma and space delimited string.
088: *
089: * @param string
090: * The string
091: * @return The string tokens as a list
092: */
093: public static StringList tokenize(final String string) {
094: return tokenize(string, ", ");
095: }
096:
097: /**
098: * Extracts tokens from a delimited string.
099: *
100: * @param string
101: * The string
102: * @param delimiters
103: * The delimiters
104: * @return The string tokens as a list
105: */
106: public static StringList tokenize(final String string,
107: final String delimiters) {
108: final StringTokenizer tokenizer = new StringTokenizer(string,
109: delimiters);
110: final StringList strings = new StringList();
111:
112: while (tokenizer.hasMoreTokens()) {
113: strings.add(tokenizer.nextToken());
114: }
115:
116: return strings;
117: }
118:
119: /**
120: * Converts a collection of objects into a list of string values by using
121: * the conversion methods of the StringValue class.
122: *
123: * @param collection
124: * The collection to add as strings
125: * @return The list
126: */
127: public static StringList valueOf(final Collection collection) {
128: if (collection != null) {
129: final StringList strings = new StringList(collection.size());
130:
131: for (final Iterator iterator = collection.iterator(); iterator
132: .hasNext();) {
133: strings.add(StringValue.valueOf(iterator.next()));
134: }
135:
136: return strings;
137: } else {
138: return new StringList();
139: }
140: }
141:
142: /**
143: * Converts an array of objects into a list of strings by using the object
144: * to string conversion method of the StringValue class.
145: *
146: * @param objects
147: * The objects to convert
148: * @return The list of strings
149: */
150: public static StringList valueOf(final Object[] objects) {
151: // check for null parameter
152: int length = (objects == null) ? 0 : objects.length;
153: final StringList strings = new StringList(length);
154:
155: for (int i = 0; i < length; i++) {
156: strings.add(StringValue.valueOf(objects[i]));
157: }
158:
159: return strings;
160: }
161:
162: /**
163: * Returns a string list with just one string in it.
164: *
165: * @param string
166: * The string
167: * @return The list of one string
168: */
169: public static StringList valueOf(final String string) {
170: final StringList strings = new StringList();
171:
172: if (string != null) {
173: strings.add(string);
174: }
175:
176: return strings;
177: }
178:
179: /**
180: * Converts a string array to a string list.
181: *
182: * @param array
183: * The array
184: * @return The list
185: */
186: public static StringList valueOf(final String[] array) {
187: int length = (array == null) ? 0 : array.length;
188: final StringList strings = new StringList(length);
189:
190: for (int i = 0; i < length; i++) {
191: strings.add(array[i]);
192: }
193:
194: return strings;
195: }
196:
197: /**
198: * Constructor.
199: */
200: public StringList() {
201: this .strings = new ArrayList();
202: }
203:
204: /**
205: * Constructor.
206: *
207: * @param size
208: * Number of elements to preallocate
209: */
210: public StringList(final int size) {
211: this .strings = new ArrayList(size);
212: }
213:
214: /**
215: * Adds a string to the back of this list.
216: *
217: * @param string
218: * String to add
219: */
220: public void add(final String string) {
221: // Add to list
222: add(size(), string);
223: }
224:
225: /**
226: * Adds the string to the stringlist at position pos.
227: *
228: * @param pos
229: * the position to add the string at
230: * @param string
231: * the string to add.
232: */
233: public void add(final int pos, final String string) {
234: strings.add(pos, string == null ? "" : string);
235:
236: // Increase total length
237: totalLength += string == null ? 0 : string.length();
238: }
239:
240: /**
241: * Adds a string value to this list as a string.
242: *
243: * @param value
244: * The value to add
245: */
246: public void add(final StringValue value) {
247: add(value.toString());
248: }
249:
250: /**
251: * @param string
252: * The string to look for
253: * @return True if the list contains the string
254: */
255: public boolean contains(final String string) {
256: return strings.contains(string);
257: }
258:
259: /**
260: * Gets the string at the given index.
261: *
262: * @param index
263: * The index
264: * @return The string at the index
265: * @throws IndexOutOfBoundsException
266: */
267: public String get(final int index) {
268: return (String) strings.get(index);
269: }
270:
271: /**
272: * @return List value (not a copy of this list)
273: */
274: public List getList() {
275: return strings;
276: }
277:
278: /**
279: * Returns a typesafe iterator over this collection of strings.
280: *
281: * @return Typesafe string iterator
282: */
283: public IStringIterator iterator() {
284: return new IStringIterator() {
285: private final Iterator iterator = strings.iterator();
286:
287: public boolean hasNext() {
288: return iterator.hasNext();
289: }
290:
291: public String next() {
292: return (String) iterator.next();
293: }
294: };
295: }
296:
297: /**
298: * Adds the given string to the front of the list.
299: *
300: * @param string
301: * The string to add
302: */
303: public void prepend(final String string) {
304: add(0, string);
305: }
306:
307: /**
308: * Removes the string at the given index.
309: *
310: * @param index
311: * The index
312: */
313: public void remove(final int index) {
314: String string = (String) strings.remove(index);
315: totalLength = totalLength - string.length();
316: }
317:
318: /**
319: * Removes the last string in this list.
320: */
321: public void removeLast() {
322: remove(size() - 1);
323: }
324:
325: /**
326: * @return The number of strings in this list.
327: */
328: public int size() {
329: return strings.size();
330: }
331:
332: /**
333: * Sorts this string list alphabetically.
334: */
335: public void sort() {
336: Collections.sort(strings);
337: }
338:
339: /**
340: * Converts this string list to a string array.
341: *
342: * @return The string array
343: */
344: public String[] toArray() {
345: return (String[]) strings.toArray(new String[size()]);
346: }
347:
348: /**
349: * @return The total length of all strings in this list.
350: */
351: public int totalLength() {
352: return totalLength;
353: }
354: }
|