KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > collection > Collections


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
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 (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25
26 package org.snipsnap.util.collection;
27
28 import java.util.*;
29
30 /**
31  * Collects some useful methods on collections.
32  * Original author <a HREF="mailto:mgrosze@web.de">Michael Gro&szlig;e</a>
33  *
34  * @author stephan
35  * @version $Id: Collections.java 645 2003-01-09 09:49:12Z stephan $
36  */

37 public class Collections {
38
39   /**
40    * Filters the list to a new ArrayList with the filter.
41    *
42    * @param aList a list to filter, is copied before any operation
43    * @param aFilter the applied filter
44    * @return an ArrayList containing all elements not filtered
45    */

46   public static List filter(List aList, Filterator aFilter) {
47     List filteredList = new ArrayList();
48     filteredList.addAll(aList);
49
50     Iterator iterator = filteredList.iterator();
51     while (iterator.hasNext()) {
52       if (aFilter.filter(iterator.next())) {
53         // also removes the element in the collection
54
iterator.remove();
55       }
56     }
57     return filteredList;
58   }
59
60
61   /**
62    * Transforms a list to a new ArrayList with the transformer.
63    *
64    * @param aList a list to transformed, is copied before any operation
65    * @param aTransformer Description of Parameter
66    * @return an ArrayList containing all transformed elements
67    */

68   public static List transform(List aList, Transformer aTransformer) {
69     List transformedList = new ArrayList();
70     transformedList.addAll(aList);
71
72     ListIterator iterator = transformedList.listIterator();
73     while (iterator.hasNext()) {
74       iterator.set(aTransformer.transform(iterator.next()));
75     }
76     return transformedList;
77   }
78
79
80   /**
81    * Joins all entries of a collection with a delimiter to a string. The function
82    * works like the perl-function join.
83    *
84    * @param aCollection a collection to be joined
85    * @param delimiter a delimiter dividing the entries
86    * @return an ArrayList a String with all entries of the list
87    * separated by the delimiter
88    */

89   public static String JavaDoc join(Collection aCollection, String JavaDoc delimiter) {
90     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91
92     Iterator iterator = aCollection.iterator();
93     while (iterator.hasNext()) {
94       sb.append(iterator.next().toString());
95       if (iterator.hasNext()) {
96         sb.append(delimiter);
97       }
98     }
99     return sb.toString();
100   }
101
102
103   /**
104    * Splits a String on a delimiter to a List. The function works like
105    * the perl-function split.
106    *
107    * @param aString a String to split
108    * @param delimiter a delimiter dividing the entries
109    * @return a String with all entries of the list
110    * separated by the delimiter
111    */

112   public static List split(String JavaDoc aString, String JavaDoc delimiter) {
113     List list = new ArrayList();
114
115     StringTokenizer st = new StringTokenizer(aString, delimiter);
116     while (st.hasMoreTokens()) {
117       list.add(st.nextToken());
118     }
119     return list;
120   }
121 }
122
Popular Tags