01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.ivy.util.filter;
19:
20: import java.util.ArrayList;
21: import java.util.Collection;
22: import java.util.Iterator;
23: import java.util.List;
24:
25: public final class FilterHelper {
26: private FilterHelper() {
27: }
28:
29: public static final Filter NO_FILTER = NoFilter.INSTANCE;
30:
31: public static Filter getArtifactTypeFilter(String types) {
32: if (types == null || types.trim().equals("*")) {
33: return NO_FILTER;
34: }
35: String[] t = types.split(",");
36: List acceptedTypes = new ArrayList(t.length);
37: for (int i = 0; i < t.length; i++) {
38: acceptedTypes.add(t[i].trim());
39: }
40: return new ArtifactTypeFilter(acceptedTypes);
41: }
42:
43: /**
44: * Returns a new collection containing only the items from the given collectoin, which are
45: * accepted by the filter.
46: *
47: * @param col The collection to filter.
48: * @param filter The filter to use.
49: * @return A new collection instance containing the only the instance accepted by the filter.
50: *
51: * <br />
52: * Comment: We could have used
53: * <a href="http://jakarta.apache.org/commons/collections/">Commons-Collections</a>
54: * facility for this. If we accepted to add dependencies on third party jars.
55: */
56: public static Collection filter(Collection col, Filter filter) {
57: if (filter == null) {
58: return col;
59: }
60: Collection ret = new ArrayList(col);
61: for (Iterator iter = ret.iterator(); iter.hasNext();) {
62: Object element = (Object) iter.next();
63: if (!filter.accept(element)) {
64: iter.remove();
65: }
66: }
67: return ret;
68: }
69: }
|