001: /* {{{ StringList.java - a List of Strings with split() and join() methods
002: * :tabSize=8:indentSize=8:noTabs=false:
003: * :folding=explicit:collapseFolds=1:
004: *
005: * Copyright (C) 2005 Alan Ezust
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: * }}} */
021:
022: package org.gjt.sp.util;
023:
024: //{{{ imports
025: import java.util.ArrayList;
026: import java.util.Collection;
027:
028: //}}}
029:
030: // {{{ StringList class
031: /**
032: * A List<String> with some perl-like convenience functions (split/join primarily),
033: * and easy conversion to/from arrays.
034: * @since jEdit 4.3pre7
035: */
036: public class StringList extends ArrayList<String> {
037:
038: // {{{ Constructors
039: public StringList() {
040: }
041:
042: public StringList(Object[] array) {
043: addAll(array);
044: } // }}}
045:
046: // {{{ addAll()
047: public void addAll(Object[] array) {
048: for (int i = 0; i < array.length; ++i) {
049: add(array[i].toString());
050: }
051: } // }}}
052:
053: // {{{ split()
054: /**
055: * @param orig the original string
056: * @param delim a delimiter to use for splitting
057: * @return a new StringList containing the split strings.
058: */
059: public static StringList split(String orig, Object delim) {
060: if ((orig == null) || (orig.length() == 0))
061: return new StringList();
062: return new StringList(orig.split(delim.toString()));
063: } // }}}
064:
065: // {{{ toString()
066: /**
067: * Joins each string in the list with a newline.
068: * @return a joined string representation of this,
069: * with the newline (\n) as delimiter.
070: */
071: public String toString() {
072: return join("\n");
073: } // }}}
074:
075: // {{{ toArray()
076: /** @return an array of String */
077: public String[] toArray() {
078: int siz = size();
079: String[] result = new String[siz];
080: System.arraycopy(super .toArray(), 0, result, 0, siz);
081: return result;
082: }
083:
084: // }}}
085:
086: // {{{ join() methods
087: /**
088: * The reverse of split - given a collection, takes each element
089: * and places it in a string, joined by a delimiter.
090: */
091: public static String join(Collection c, String delim) {
092: StringList sl = new StringList();
093: for (Object o : c) {
094: String s = o.toString();
095: sl.add(s);
096: }
097: return sl.join(delim);
098: }
099:
100: /**
101: *
102: * @param arr array of objects
103: * @param delim delimiter to separate strings
104: * @return a single string with each element in arr converted to a string and concatenated,
105: * separated by delim.
106: */
107: public static String join(Object[] arr, String delim) {
108: StringList sl = new StringList();
109: sl.addAll(arr);
110: return sl.join(delim);
111: }
112:
113: /**
114: * Non-static version, that joins "this" StringList.
115: * @param delim the delimiter
116: * @return a joined string with delim inbetween each element
117: */
118: public String join(String delim) {
119: int s = size();
120: if (s < 1)
121: return "";
122: if (s == 1)
123: return get(0).toString();
124: else {
125: StringBuffer retval = new StringBuffer();
126: retval.append(get(0));
127: for (int i = 1; i < s; ++i)
128: retval.append(delim + get(i));
129: return retval.toString();
130: }
131:
132: } // }}}
133:
134: // {{{ main()
135: public static void main(String args[]) {
136: String teststr = "a,b,c,d,e,f";
137: StringList sl = StringList.split(teststr, ",");
138: String joinstr = sl.join(",");
139: // assert(teststr.equals(joinstr));
140: System.out.println("Test Passed");
141:
142: }// }}}
143:
144: private static final long serialVersionUID = -6408080298368668262L;
145: } // }}}
|