src/core/net/sf/basedb/util/overview/filter/HasAttributeFilter.java

Code
Comments
Other
Rev Date Author Line
5748 19 Sep 11 nicklas 1 /**
5748 19 Sep 11 nicklas 2   $Id $
5748 19 Sep 11 nicklas 3
5748 19 Sep 11 nicklas 4   Copyright (C) 2011 Nicklas Nordborg
5748 19 Sep 11 nicklas 5
5748 19 Sep 11 nicklas 6   This file is part of BASE - BioArray Software Environment.
5748 19 Sep 11 nicklas 7   Available at http://base.thep.lu.se/
5748 19 Sep 11 nicklas 8
5748 19 Sep 11 nicklas 9   BASE is free software; you can redistribute it and/or
5748 19 Sep 11 nicklas 10   modify it under the terms of the GNU General Public License
5748 19 Sep 11 nicklas 11   as published by the Free Software Foundation; either version 3
5748 19 Sep 11 nicklas 12   of the License, or (at your option) any later version.
5748 19 Sep 11 nicklas 13
5748 19 Sep 11 nicklas 14   BASE is distributed in the hope that it will be useful,
5748 19 Sep 11 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
5748 19 Sep 11 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5748 19 Sep 11 nicklas 17   GNU General Public License for more details.
5748 19 Sep 11 nicklas 18
5748 19 Sep 11 nicklas 19   You should have received a copy of the GNU General Public License
5748 19 Sep 11 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
5748 19 Sep 11 nicklas 21 */
5748 19 Sep 11 nicklas 22 package net.sf.basedb.util.overview.filter;
5748 19 Sep 11 nicklas 23
5748 19 Sep 11 nicklas 24 import net.sf.basedb.util.filter.Filter;
5748 19 Sep 11 nicklas 25 import net.sf.basedb.util.overview.Node;
5748 19 Sep 11 nicklas 26 import net.sf.basedb.util.overview.NodeAttribute;
5748 19 Sep 11 nicklas 27
5748 19 Sep 11 nicklas 28 /**
5748 19 Sep 11 nicklas 29   A filter implementation that checks if a value for a given attribute
6455 09 May 14 nicklas 30   exists on a node. It will evaluate to true for all {@link Node}:s that
5748 19 Sep 11 nicklas 31   has a value for a specific attribute.
5748 19 Sep 11 nicklas 32
5748 19 Sep 11 nicklas 33   @author Nicklas
5748 19 Sep 11 nicklas 34   @since 3.0
5748 19 Sep 11 nicklas 35   @base.modified $Date $
5748 19 Sep 11 nicklas 36 */
5748 19 Sep 11 nicklas 37 public class HasAttributeFilter
5748 19 Sep 11 nicklas 38   implements Filter<Node>
5748 19 Sep 11 nicklas 39 {
5748 19 Sep 11 nicklas 40   
5748 19 Sep 11 nicklas 41   private final NodeAttribute<?> attribute;
6455 09 May 14 nicklas 42   private final Object value;
5748 19 Sep 11 nicklas 43   
5748 19 Sep 11 nicklas 44   /**
5748 19 Sep 11 nicklas 45     Create a filter that finds nodes that has value for the given
5748 19 Sep 11 nicklas 46     attribute.
5748 19 Sep 11 nicklas 47     @param attribute The attribute to check
5748 19 Sep 11 nicklas 48   */
5748 19 Sep 11 nicklas 49   public HasAttributeFilter(NodeAttribute<?> attribute)
5748 19 Sep 11 nicklas 50   {
6455 09 May 14 nicklas 51     this(attribute, null);
6455 09 May 14 nicklas 52   }
6455 09 May 14 nicklas 53   
6455 09 May 14 nicklas 54   /**
6455 09 May 14 nicklas 55     Create a filter that finds nodes that has a specific value for the given
6455 09 May 14 nicklas 56     attribute.
6455 09 May 14 nicklas 57     @param attribute The attribute to check
6455 09 May 14 nicklas 58     @param value The value that must match
6455 09 May 14 nicklas 59     @since 3.3
6455 09 May 14 nicklas 60   */
6455 09 May 14 nicklas 61   public HasAttributeFilter(NodeAttribute<?> attribute, Object value)
6455 09 May 14 nicklas 62   {
5748 19 Sep 11 nicklas 63     this.attribute = attribute;
6455 09 May 14 nicklas 64     this.value = value;
5748 19 Sep 11 nicklas 65   }
5748 19 Sep 11 nicklas 66
5748 19 Sep 11 nicklas 67   /*
5748 19 Sep 11 nicklas 68     From the Filter interface
5748 19 Sep 11 nicklas 69     -------------------------
5748 19 Sep 11 nicklas 70   */
5748 19 Sep 11 nicklas 71   @Override
5748 19 Sep 11 nicklas 72   public boolean evaluate(Node node)
5748 19 Sep 11 nicklas 73   {
6455 09 May 14 nicklas 74     if (value == null)
6455 09 May 14 nicklas 75     {
6455 09 May 14 nicklas 76       return node.hasAttribute(attribute);
6455 09 May 14 nicklas 77     }
6455 09 May 14 nicklas 78     else
6455 09 May 14 nicklas 79     {
6455 09 May 14 nicklas 80       return value.equals(node.getAttribute(attribute));
6455 09 May 14 nicklas 81     }
5748 19 Sep 11 nicklas 82   }
5748 19 Sep 11 nicklas 83   // -----------------------------
5748 19 Sep 11 nicklas 84 }