View Javadoc

1   /*
2    * @(#) AbstractFilter.java Oct 26, 2004 Copyright (c) 2002-2005 Delft
3    * University of Technology Jaffalaan 5, 2628 BX Delft, the Netherlands. All
4    * rights reserved. This software is proprietary information of Delft University
5    * of Technology The code is published under the Lesser General Public License
6    */
7   package nl.tudelft.simulation.language.filters;
8   
9   /***
10   * The abstract filter forms the abstract class for all filters. The filter
11   * method should be implemented by all subclasses. This filter method should
12   * have the same semantics as the accept(inverted=false) method.
13   * <p>
14   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
15   * University of Technology </a>, the Netherlands.
16   * <p>
17   * See for project information <a
18   * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language
19   * </a> <br>
20   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
21   * General Public License (LGPL) </a>, no warranty
22   * 
23   * @author <a
24   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
25   *         Lang </a><a href="http://www.peter-jacobs.com/index.htm">Peter
26   *         Jacobs </a>
27   * @version $Revision: 1.10 $ $Date: 2005/08/04 12:08:54 $
28   * @since 1.5
29   */
30  public abstract class AbstractFilter implements Filterinterface
31  {
32      /*** is this filter inverted */
33      protected boolean inverted = false;
34  
35      /***
36       * constructs a new AbstractFilter
37       */
38      public AbstractFilter()
39      {
40          super();
41      }
42  
43      /***
44       * @see nl.tudelft.simulation.language.filters.Filterinterface#isInverted()
45       */
46      public boolean isInverted()
47      {
48          return this.inverted;
49      }
50  
51      /***
52       * @see nl.tudelft.simulation.language.filters.Filterinterface
53       *      #setInverted(boolean)
54       */
55      public void setInverted(final boolean inverted)
56      {
57          this.inverted = inverted;
58      }
59  
60      /***
61       * @see nl.tudelft.simulation.language.filters.Filterinterface#accept(java.lang.Object)
62       */
63      public boolean accept(final Object entry)
64      {
65          boolean value = this.filter(entry);
66          if (!this.inverted)
67          {
68              return value;
69          }
70          return !value;
71      }
72  
73      /***
74       * filters the entry. This method should be implemented by every filter
75       * based on its semantic meaning.
76       * 
77       * @param entry the entry to filter.
78       * @return whether to accept the value.
79       */
80      protected abstract boolean filter(final Object entry);
81  
82      /***
83       * returns the filter criterium
84       * 
85       * @return the criterium
86       */
87      public abstract String getCriterium();
88  
89      /***
90       * adds filter to this filter and returns the composed filter
91       * 
92       * @param filter the filter to add
93       * @return the composed filter
94       */
95      public Filterinterface and(final Filterinterface filter)
96      {
97          return new CompositeFilter(this, filter, CompositeFilter.AND);
98      }
99  
100     /***
101      * creates a new composite filter which is one or two
102      * 
103      * @param filter the filter to add
104      * @return the composed filter
105      */
106     public Filterinterface or(final Filterinterface filter)
107     {
108         return new CompositeFilter(this, filter, CompositeFilter.OR);
109     }
110 
111     /***
112      * @see java.lang.Object#toString()
113      */
114     @Override
115 	public String toString()
116     {
117         return "Filter[criterium=" + this.getCriterium() + ";inverted="
118                 + this.inverted + "]";
119     }
120 
121 }