001: /*
002: * Created on Sep 16, 2004
003: */
004: package uk.org.ponder.stringutil;
005:
006: import java.util.ArrayList;
007: import java.util.Collection;
008: import java.util.Iterator;
009:
010: import uk.org.ponder.util.UniversalRuntimeException;
011:
012: /**
013: * A convenience wrapper for a type-safe list of String objects.
014: *
015: * @author Antranig Basman (antranig@caret.cam.ac.uk)
016: *
017: */
018: public class StringList extends ArrayList {
019:
020: public StringList() {
021: }
022:
023: public StringList(Collection tocopy) {
024: append(tocopy);
025: }
026:
027: public StringList(String string) {
028: ensureCapacity(2);
029: add(string);
030: }
031:
032: public StringList(String[] strings) {
033: ensureCapacity(strings.length);
034: append(strings);
035: }
036:
037: public String stringAt(int i) {
038: return (String) get(i);
039: }
040:
041: public boolean add(Object o) {
042: if (o != null && !(o instanceof String)) {
043: throw new UniversalRuntimeException("Object " + o + " of "
044: + o.getClass() + " added to StringList");
045: }
046: return super .add(o);
047: }
048:
049: /**
050: * Appends the members of the supplied array to this list.
051: *
052: * @param toappend The array of Strings to be appended, which may be
053: * <code>null</code>.
054: */
055:
056: public void append(String[] toappend) {
057: if (toappend != null) {
058: for (int i = 0; i < toappend.length; ++i) {
059: add(toappend[i]);
060: }
061: }
062: }
063:
064: /**
065: * Appends the members of the supplied list to this list.
066: *
067: * @param toappend The list of Strings to be appended, which may be
068: * <code>null</code>.
069: */
070: public void append(StringList toappend) {
071: if (toappend != null) {
072: for (int i = 0; i < toappend.size(); ++i) {
073: add(toappend.get(i));
074: }
075: }
076: }
077:
078: public void append(Collection toappend) {
079: for (Iterator it = toappend.iterator(); it.hasNext();) {
080: String next = (String) it.next(); // ensure ClassCastException if wrong
081: // type
082: add(next);
083: }
084: }
085:
086: public StringList copy() {
087: StringList togo = new StringList();
088: togo.addAll(this );
089: return togo;
090: }
091:
092: public String pack() {
093: CharWrap togo = new CharWrap();
094: for (int i = 0; i < size(); ++i) {
095: togo.append(stringAt(i));
096: togo.append('\n');
097: }
098: return togo.toString();
099: }
100:
101: public String[] toStringArray() {
102: String[] togo = (String[]) toArray(new String[size()]);
103: return togo;
104: }
105:
106: public String toString() {
107: CharWrap togo = new CharWrap();
108: for (int i = 0; i < size(); ++i) {
109: togo.append(stringAt(i));
110: togo.append(", ");
111: }
112: return togo.toString();
113: }
114:
115: /**
116: * Construct a StringList from a comma separated list of Strings, trimming
117: * whitespace.
118: *
119: * @param commasep
120: * @return
121: */
122: public static StringList fromString(String commasep) {
123: String[] strings = commasep.split(",");
124: StringList togo = new StringList();
125:
126: for (int i = 0; i < strings.length; ++i) {
127: String toadd = strings[i].trim();
128: // ensure that empty string produces empty list
129: if (toadd.length() != 0 || strings.length > 1) {
130: togo.add(toadd);
131: }
132: }
133: return togo;
134: }
135: }
|