001: /*
002: * $Id: AbstractStringList.java 458818 2006-01-25 16:18:39Z jcompagner $
003: * $Revision: 458818 $ $Date: 2006-01-25 17:18:39 +0100 (Wed, 25 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.io.Serializable;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: /**
025: * An abstract base class for string list implementations. Besides having an
026: * implementation for IStringSequence (iterator(), get(int index) and size()),
027: * an AbstractStringList can be converted to a String array or a List of
028: * Strings.
029: * <p>
030: * The total length of all Strings in the list can be determined by calling
031: * totalLength().
032: * <p>
033: * Strings or a subset of Strings in the list can be formatted using three
034: * join() methods:
035: * <p>
036: * <ul>
037: * <li>join(String) Joins strings together using a given separator
038: * <li>join() Joins Strings using comma as a separator
039: * <li>join(int first, int last, String) Joins a sublist of strings using a
040: * given separator
041: * </ul>
042: *
043: * @author Jonathan Locke
044: */
045: public abstract class AbstractStringList implements IStringSequence,
046: Serializable {
047: /**
048: * @return String iterator
049: * @see wicket.util.string.IStringSequence#iterator()
050: */
051: public abstract IStringIterator iterator();
052:
053: /**
054: * @return Number of strings in this string list
055: * @see wicket.util.string.IStringSequence#size()
056: */
057: public abstract int size();
058:
059: /**
060: * @param index
061: * The index into this string list
062: * @return The string at the given index
063: * @see wicket.util.string.IStringSequence#get(int)
064: */
065: public abstract String get(int index);
066:
067: /**
068: * Returns this String sequence as an array of Strings. Subclasses may
069: * provide a more efficient implementation than the one provided here.
070: *
071: * @return An array containing exactly this sequence of Strings
072: */
073: public String[] toArray() {
074: // Get number of Strings
075: final int size = size();
076:
077: // Allocate array
078: final String[] strings = new String[size];
079:
080: // Copy string references
081: for (int i = 0; i < size; i++) {
082: strings[i] = get(i);
083: }
084:
085: return strings;
086: }
087:
088: /**
089: * Returns this String sequence as an array of Strings. Subclasses may
090: * provide a more efficient implementation than the one provided here.
091: *
092: * @return An array containing exactly this sequence of Strings
093: */
094: public final List toList() {
095: // Get number of Strings
096: final int size = size();
097:
098: // Allocate list of exactly the right size
099: final List strings = new ArrayList(size);
100:
101: // Add strings to list
102: for (int i = 0; i < size; i++) {
103: strings.add(get(i));
104: }
105:
106: return strings;
107: }
108:
109: /**
110: * @return The total length of all Strings in this sequence.
111: */
112: public int totalLength() {
113: // Get number of Strings
114: final int size = size();
115:
116: // Add strings to list
117: int totalLength = 0;
118:
119: for (int i = 0; i < size; i++) {
120: totalLength += get(i).length();
121: }
122:
123: return totalLength;
124: }
125:
126: /**
127: * Joins this sequence of strings using a comma separator. For example, if
128: * this sequence contains [1 2 3], the result of calling this method will be
129: * "1, 2, 3".
130: *
131: * @return The joined String
132: */
133: public final String join() {
134: return join(", ");
135: }
136:
137: /**
138: * Joins this sequence of strings using a separator
139: *
140: * @param separator
141: * The separator to use
142: * @return The joined String
143: */
144: public final String join(final String separator) {
145: return join(0, size(), separator);
146: }
147:
148: /**
149: * Joins this sequence of strings from first index to last using a separator
150: *
151: * @param first
152: * The first index to use, inclusive
153: * @param last
154: * The last index to use, exclusive
155: * @param separator
156: * The separator to use
157: * @return The joined String
158: */
159: public final String join(final int first, final int last,
160: final String separator) {
161: // Allocate buffer of exactly the right length
162: final int length = totalLength()
163: + (separator.length() * (Math.max(0, last - first - 1)));
164: final AppendingStringBuffer buf = new AppendingStringBuffer(
165: length);
166:
167: // Loop through indexes requested
168: for (int i = first; i < last; i++) {
169: // Add next string
170: buf.append(get(i));
171:
172: // Add separator?
173: if (i != (last - 1)) {
174: buf.append(separator);
175: }
176: }
177:
178: return buf.toString();
179: }
180:
181: /**
182: * Converts this object to a string representation
183: *
184: * @return String version of this object
185: */
186: public String toString() {
187: return "[" + join() + "]";
188: }
189: }
|