KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > util > StringList


1 /* {{{ StringList.java - a List of Strings
2  * with split() and join() methods
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2005 Alan Ezust
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  * }}} */

22
23 package org.gjt.sp.util;
24
25 //{{{ imports
26
import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 //}}}
29

30 // {{{ StringList class
31
/**
32  * A List<String> with some perl-like convenience functions (split/join primarily),
33  * and easy conversion to/from arrays.
34  * @since jEdit 4.3pre7
35  */

36 public class StringList extends ArrayList JavaDoc<String JavaDoc>
37 {
38
39     // {{{ Constructors
40
public StringList()
41     {
42     }
43
44
45     public StringList(Object JavaDoc[] array)
46     {
47         addAll(array);
48     } // }}}
49

50     // {{{ addAll()
51
public void addAll(Object JavaDoc[] array)
52     {
53         for (int i = 0; i < array.length; ++i)
54         {
55             add(array[i].toString());
56         }
57     } // }}}
58

59     // {{{ split()
60
/**
61      * Splits a string on a delimiter, returning a StringList.
62      */

63     public static StringList split(String JavaDoc orig, Object JavaDoc delim)
64     {
65         if ((orig == null) || (orig.length() == 0))
66             return new StringList();
67         return new StringList(orig.split(delim.toString()));
68     } // }}}
69

70     // {{{ toString()
71
/**
72      * Joins each string in the list with a newline.
73      */

74     public String JavaDoc toString()
75     {
76         return join("\n");
77     } // }}}
78

79     // {{{ toArray()
80
public String JavaDoc[] toArray() {
81         int siz = size();
82         String JavaDoc[] result = new String JavaDoc[siz];
83         System.arraycopy(super.toArray(), 0, result, 0, siz);
84         return result;
85     }
86     // }}}
87

88     // {{{ join() methods
89
/**
90      * The reverse of split - given a collection, takes each element
91      * and places it in a string, joined by a delimiter.
92      */

93     public static String JavaDoc join(Collection JavaDoc c, String JavaDoc delim)
94     {
95         StringList sl = new StringList();
96         for (Object JavaDoc o: c) {
97             String JavaDoc s = o.toString();
98             sl.add(s);
99         }
100         return sl.join(delim);
101     }
102
103     /**
104      *
105      * @param arr array of objects
106      * @param delim delimiter to separate strings
107      * @return a single string with each element in arr converted to a string and concatenated,
108      * separated by delim.
109      */

110     public static String JavaDoc join(Object JavaDoc[] arr, String JavaDoc delim) {
111         StringList sl = new StringList();
112         sl.addAll(arr);
113         return sl.join(delim);
114     }
115
116
117     /**
118      * Non-static version, that joins "this" StringList.
119      * @param delim
120      * @return
121      */

122     public String JavaDoc join(String JavaDoc delim)
123     {
124         int s = size();
125         if (s < 1)
126             return "";
127         if (s == 1)
128             return get(0).toString();
129         else
130         {
131             StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
132             retval.append(get(0));
133             for (int i = 1; i < s; ++i)
134                 retval.append(delim + get(i));
135             return retval.toString();
136         }
137
138     } // }}}
139

140     // {{{ main()
141
public static void main(String JavaDoc args[])
142     {
143         String JavaDoc teststr = "a,b,c,d,e,f";
144         StringList sl = StringList.split(teststr, ",");
145         String JavaDoc joinstr = sl.join(",");
146         // assert(teststr.equals(joinstr));
147
System.out.println("Test Passed");
148
149     }// }}}
150
private static final long serialVersionUID = -6408080298368668262L;
151 } // }}}
152
Popular Tags