src/core/net/sf/basedb/util/filter/InstanceOfFilter.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 /**
4594 21 Oct 08 nicklas 25   A filter implementation that accepts all objects
4594 21 Oct 08 nicklas 26   that are (sub)classes of a specified class.
4594 21 Oct 08 nicklas 27   
4594 21 Oct 08 nicklas 28   @author Nicklas
4594 21 Oct 08 nicklas 29   @version 2.9
4594 21 Oct 08 nicklas 30   @base.modified $Date$
4594 21 Oct 08 nicklas 31 */
4594 21 Oct 08 nicklas 32 public class InstanceOfFilter<T>
4594 21 Oct 08 nicklas 33   implements Filter<T>
4594 21 Oct 08 nicklas 34 {
4594 21 Oct 08 nicklas 35   
4594 21 Oct 08 nicklas 36   private final Class<?> clazz;
4594 21 Oct 08 nicklas 37   private final boolean allowSubclass;
4594 21 Oct 08 nicklas 38   /**
4594 21 Oct 08 nicklas 39     Create a new filter. 
4594 21 Oct 08 nicklas 40     @param clazz The class to match against
4594 21 Oct 08 nicklas 41     @param allowSubclass If subclasses to the specified class should
4594 21 Oct 08 nicklas 42       be accepted (TRUE) or denied (FALSE)
4594 21 Oct 08 nicklas 43   */
4594 21 Oct 08 nicklas 44   public InstanceOfFilter(Class<?> clazz, boolean allowSubclass)
4594 21 Oct 08 nicklas 45   {
4594 21 Oct 08 nicklas 46     this.clazz = clazz;
4594 21 Oct 08 nicklas 47     this.allowSubclass = allowSubclass;
4594 21 Oct 08 nicklas 48   }
4594 21 Oct 08 nicklas 49
4594 21 Oct 08 nicklas 50   /**
4594 21 Oct 08 nicklas 51     @return TRUE if the object is an instance of the given class, FALSE otherwise
4594 21 Oct 08 nicklas 52   */
4594 21 Oct 08 nicklas 53   @Override
4594 21 Oct 08 nicklas 54   public boolean evaluate(T object)
4594 21 Oct 08 nicklas 55   {
4594 21 Oct 08 nicklas 56     return allowSubclass ? 
4594 21 Oct 08 nicklas 57       clazz.isInstance(object) : 
4594 21 Oct 08 nicklas 58       object != null && object.getClass() == clazz;
4594 21 Oct 08 nicklas 59   }
4594 21 Oct 08 nicklas 60   
4594 21 Oct 08 nicklas 61 }