PageRenderTime 62ms CodeModel.GetById 53ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/bundles/plugins-trunk/InfoViewer/testcase/StringList.java

#
Java | 125 lines | 73 code | 18 blank | 34 comment | 10 complexity | 6022acad0e6d9a24dba20612ec6253f9 MD5 | raw file
  1/*
  2 * StringList.java - Helper functions for 
  3    perl-like string lists.
  4 *
  5 * :tabSize=8:indentSize=8:noTabs=false:
  6 * :folding=explicit:collapseFolds=1:
  7 *
  8 * Copyright (C) 2005 Alan Ezust
  9 *
 10 * This program is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU General Public License
 12 * as published by the Free Software Foundation; either version 2
 13 * of the License, or any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 23 */
 24
 25package testcase;
 26
 27// {{{ imports
 28import java.util.ArrayList;
 29import java.util.Collection;
 30// }}}
 31
 32public class StringList extends ArrayList<String>
 33{
 34
 35    // {{{ StringList()
 36    	public StringList()
 37	{
 38	}
 39    
 40	public StringList(Object[] array)
 41	{
 42		addAll(array);
 43    } // }}}
 44
 45    // {{{ addAll() 
 46	public void addAll(Object[] array)
 47	{
 48		for (int i = 0; i < array.length; ++i)
 49		{
 50			add(array[i].toString());
 51		}
 52	}   // }}}
 53    
 54    // {{{ split()     
 55	public static StringList split(String orig, Object delim)
 56	{
 57		if ((orig == null) || (orig.length() == 0))
 58			return new StringList();
 59		return new StringList(orig.split(delim.toString()));
 60	} // }}} 
 61
 62    // {{{ toString() 
 63	public String toString()
 64	{
 65		return join("\n");
 66	}
 67    // }}}
 68    
 69	public String[] toArray() {
 70		int siz = size();
 71		String[] result = new String[siz];
 72		System.arraycopy(super.toArray(), 0, result, 0, siz);
 73		return result;
 74	}
 75	
 76    // {{{ join() 
 77	public static String join(Collection c, String delim)
 78	{
 79		StringList sl = new StringList();
 80		for (Object o: c) {
 81			String s = o.toString();
 82			sl.add(s);
 83		}
 84		return sl.join(delim);
 85	}
 86
 87	public static String join(Object[] arr, String delim) {
 88		StringList sl = new StringList();
 89		sl.addAll(arr);
 90		return sl.join(delim);
 91	}
 92		
 93
 94	public String join(String delim)
 95	{
 96		int s = size();
 97		if (s < 1)
 98			return "";
 99		if (s == 1)
100			return get(0).toString();
101		else
102		{
103			StringBuffer retval = new StringBuffer();
104			retval.append(get(0));
105			for (int i = 1; i < s; ++i)
106				retval.append(delim + get(i));
107			return retval.toString();
108		}
109
110	}
111    // }}}
112    
113    // {{{ main() 
114	public static void main(String args[])
115	{
116		String teststr = "a,b,c,d,e,f";
117		StringList sl = StringList.split(teststr, ",");
118		String joinstr = sl.join(",");
119		// assert(teststr.equals(joinstr));
120		System.out.println("Test Passed");
121
122    }// }}} 
123
124}
125