KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > filter > FilterHelper


1 /*
2  * This file is subject to the licence found in LICENCE.TXT in the root directory of the project.
3  * Copyright Jayasoft 2005 - All rights reserved
4  *
5  * #SNAPSHOT#
6  */

7 package fr.jayasoft.ivy.filter;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 public class FilterHelper {
15     public static Filter NO_FILTER = NoFilter.INSTANCE;
16     
17     public static Filter getArtifactTypeFilter(String JavaDoc types) {
18         if (types == null || types.trim().equals("*")) {
19             return NO_FILTER;
20         }
21         String JavaDoc[] t = types.split(",");
22         List JavaDoc acceptedTypes = new ArrayList JavaDoc(types.length());
23         for (int i = 0; i < t.length; i++) {
24             acceptedTypes.add(t[i].trim());
25         }
26         return new ArtifactTypeFilter(acceptedTypes);
27     }
28
29     /**
30      * we could have used commons-collections facility for this...
31      * if we accepted to add dependencies on third party jars
32      * @param col
33      * @param filter
34      * @return
35      */

36     public static Collection JavaDoc filter(Collection JavaDoc col, Filter filter) {
37         if (filter == null) {
38             return col;
39         }
40         Collection JavaDoc ret = new ArrayList JavaDoc(col);
41         for (Iterator JavaDoc iter = ret.iterator(); iter.hasNext();) {
42             Object JavaDoc element = (Object JavaDoc)iter.next();
43             if (!filter.accept(element)) {
44                 iter.remove();
45             }
46         }
47         return ret;
48     }
49 }
50
Popular Tags