4594 |
21 Oct 08 |
nicklas |
1 |
/** |
4594 |
21 Oct 08 |
nicklas |
$Id$ |
4594 |
21 Oct 08 |
nicklas |
3 |
|
4594 |
21 Oct 08 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4594 |
21 Oct 08 |
nicklas |
5 |
|
4594 |
21 Oct 08 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4594 |
21 Oct 08 |
nicklas |
Available at http://base.thep.lu.se/ |
4594 |
21 Oct 08 |
nicklas |
8 |
|
4594 |
21 Oct 08 |
nicklas |
BASE is free software; you can redistribute it and/or |
4594 |
21 Oct 08 |
nicklas |
modify it under the terms of the GNU General Public License |
4594 |
21 Oct 08 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4594 |
21 Oct 08 |
nicklas |
of the License, or (at your option) any later version. |
4594 |
21 Oct 08 |
nicklas |
13 |
|
4594 |
21 Oct 08 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4594 |
21 Oct 08 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4594 |
21 Oct 08 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4594 |
21 Oct 08 |
nicklas |
GNU General Public License for more details. |
4594 |
21 Oct 08 |
nicklas |
18 |
|
4594 |
21 Oct 08 |
nicklas |
You should have received a copy of the GNU General Public License |
4594 |
21 Oct 08 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
4594 |
21 Oct 08 |
nicklas |
21 |
*/ |
4594 |
21 Oct 08 |
nicklas |
22 |
package net.sf.basedb.util.filter; |
4594 |
21 Oct 08 |
nicklas |
23 |
|
4594 |
21 Oct 08 |
nicklas |
24 |
import java.util.ArrayList; |
4594 |
21 Oct 08 |
nicklas |
25 |
import java.util.Collection; |
4594 |
21 Oct 08 |
nicklas |
26 |
import java.util.LinkedList; |
4594 |
21 Oct 08 |
nicklas |
27 |
import java.util.List; |
4594 |
21 Oct 08 |
nicklas |
28 |
|
4594 |
21 Oct 08 |
nicklas |
29 |
/** |
4594 |
21 Oct 08 |
nicklas |
Utility class for working with and using filters. |
4594 |
21 Oct 08 |
nicklas |
31 |
|
4594 |
21 Oct 08 |
nicklas |
@author Nicklas |
4594 |
21 Oct 08 |
nicklas |
@version 2.9 |
4594 |
21 Oct 08 |
nicklas |
@base.modified $Date$ |
4594 |
21 Oct 08 |
nicklas |
35 |
*/ |
4594 |
21 Oct 08 |
nicklas |
36 |
public class FilterUtil |
4594 |
21 Oct 08 |
nicklas |
37 |
{ |
4594 |
21 Oct 08 |
nicklas |
38 |
/** |
4594 |
21 Oct 08 |
nicklas |
Filter a collection and return a new collection with the |
4594 |
21 Oct 08 |
nicklas |
objects that passes the {@link Filter#evaluate(Object)} method. |
4594 |
21 Oct 08 |
nicklas |
If the original collection is null, null is returned. If the |
4594 |
21 Oct 08 |
nicklas |
original collection is empty, or if no objects passes the filter |
4594 |
21 Oct 08 |
nicklas |
an empty list is returned. If the filter is null a list with all objects |
4594 |
21 Oct 08 |
nicklas |
of the original collection is returned. The original collection |
4594 |
21 Oct 08 |
nicklas |
is never modifed. |
4594 |
21 Oct 08 |
nicklas |
46 |
|
4594 |
21 Oct 08 |
nicklas |
@param objects The collection of objects |
4594 |
21 Oct 08 |
nicklas |
@param filter The filter |
4594 |
21 Oct 08 |
nicklas |
@return A new collection, or null if the original collection is null |
4594 |
21 Oct 08 |
nicklas |
@since 2.9 |
4594 |
21 Oct 08 |
nicklas |
51 |
*/ |
4594 |
21 Oct 08 |
nicklas |
52 |
public static <T> List<T> filter(Collection<? extends T> objects, Filter<? super T> filter) |
4594 |
21 Oct 08 |
nicklas |
53 |
{ |
4594 |
21 Oct 08 |
nicklas |
54 |
List<T> list = null; |
4594 |
21 Oct 08 |
nicklas |
55 |
if (objects != null) |
4594 |
21 Oct 08 |
nicklas |
56 |
{ |
4594 |
21 Oct 08 |
nicklas |
57 |
if (filter == null) |
4594 |
21 Oct 08 |
nicklas |
58 |
{ |
4594 |
21 Oct 08 |
nicklas |
59 |
list = new ArrayList<T>(objects); |
4594 |
21 Oct 08 |
nicklas |
60 |
} |
4594 |
21 Oct 08 |
nicklas |
61 |
else |
4594 |
21 Oct 08 |
nicklas |
62 |
{ |
4594 |
21 Oct 08 |
nicklas |
63 |
list = new LinkedList<T>(); |
4594 |
21 Oct 08 |
nicklas |
64 |
for (T object : objects) |
4594 |
21 Oct 08 |
nicklas |
65 |
{ |
4594 |
21 Oct 08 |
nicklas |
66 |
if (filter.evaluate(object)) list.add(object); |
4594 |
21 Oct 08 |
nicklas |
67 |
} |
4594 |
21 Oct 08 |
nicklas |
68 |
} |
4594 |
21 Oct 08 |
nicklas |
69 |
} |
4594 |
21 Oct 08 |
nicklas |
70 |
return list; |
4594 |
21 Oct 08 |
nicklas |
71 |
} |
4594 |
21 Oct 08 |
nicklas |
72 |
} |