src/core/net/sf/basedb/util/filter/FilterUtil.java

Code
Comments
Other
Rev Date Author Line
4594 21 Oct 08 nicklas 1 /**
4594 21 Oct 08 nicklas 2   $Id$
4594 21 Oct 08 nicklas 3
4594 21 Oct 08 nicklas 4   Copyright (C) 2008 Nicklas Nordborg
4594 21 Oct 08 nicklas 5
4594 21 Oct 08 nicklas 6   This file is part of BASE - BioArray Software Environment.
4594 21 Oct 08 nicklas 7   Available at http://base.thep.lu.se/
4594 21 Oct 08 nicklas 8
4594 21 Oct 08 nicklas 9   BASE is free software; you can redistribute it and/or
4594 21 Oct 08 nicklas 10   modify it under the terms of the GNU General Public License
4594 21 Oct 08 nicklas 11   as published by the Free Software Foundation; either version 3
4594 21 Oct 08 nicklas 12   of the License, or (at your option) any later version.
4594 21 Oct 08 nicklas 13
4594 21 Oct 08 nicklas 14   BASE is distributed in the hope that it will be useful,
4594 21 Oct 08 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4594 21 Oct 08 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4594 21 Oct 08 nicklas 17   GNU General Public License for more details.
4594 21 Oct 08 nicklas 18
4594 21 Oct 08 nicklas 19   You should have received a copy of the GNU General Public License
4594 21 Oct 08 nicklas 20   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 30   Utility class for working with and using filters.
4594 21 Oct 08 nicklas 31
4594 21 Oct 08 nicklas 32   @author Nicklas
4594 21 Oct 08 nicklas 33   @version 2.9
4594 21 Oct 08 nicklas 34   @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 39     Filter a collection and return a new collection with the
4594 21 Oct 08 nicklas 40      objects that passes the {@link Filter#evaluate(Object)} method.
4594 21 Oct 08 nicklas 41     If the original collection is null, null is returned. If the
4594 21 Oct 08 nicklas 42     original collection is empty, or if no objects passes the filter
4594 21 Oct 08 nicklas 43     an empty list is returned. If the filter is null a list with all objects
4594 21 Oct 08 nicklas 44     of the original collection is returned. The original collection
4594 21 Oct 08 nicklas 45     is never modifed.
4594 21 Oct 08 nicklas 46     
4594 21 Oct 08 nicklas 47     @param objects The collection of objects
4594 21 Oct 08 nicklas 48     @param filter The filter
4594 21 Oct 08 nicklas 49     @return A new collection, or null if the original collection is null
4594 21 Oct 08 nicklas 50     @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 }