View Javadoc

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