1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.language.filters;
11
12 /***
13 * The composite filter combines two filters.
14 * <p>
15 * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl/dsol/">Delft
16 * University of Technology </a>, the Netherlands. <br>
17 * See for project information <a href="http://www.simulation.tudelft.nl/dsol/">
18 * www.simulation.tudelft.nl/dsol </a> <br>
19 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
20 * License (GPL) </a>, no warranty <br>
21 *
22 * @author <a
23 * href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
24 * Lang </a><a
25 * href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
26 * Jacobs </a>
27 * @version 1.0 Oct 26, 2004
28 * @since 1.2
29 */
30 public class CompositeFilter extends AbstractFilter
31 {
32 /*** the ADD operator */
33 public static final short ADD = 0;
34
35 /*** the OR operator */
36 public static final short OR = 1;
37
38 /*** the operator of the composite filter */
39 private short operator = -1;
40
41 /*** the filters to compose */
42 private Filterinterface[] filters = new Filterinterface[2];
43
44 /***
45 * constructs a new CompositeFilter
46 *
47 * @param filter1 the first filter
48 * @param filter2 the second filter
49 * @param operator the operator (AND or OR)
50 */
51 public CompositeFilter(Filterinterface filter1, Filterinterface filter2,
52 short operator)
53 {
54 super();
55 if (operator < 0 || operator > 1)
56 {
57 throw new IllegalArgumentException("unknown operator");
58 }
59 this.filters[0] = filter1;
60 this.filters[1] = filter2;
61 this.operator = operator;
62 }
63
64 /***
65 * @see nl.tudelft.simulation.language.filters.AbstractFilter#filter(java.lang.Object)
66 */
67 protected boolean filter(Object entry)
68 {
69 if (this.operator == CompositeFilter.ADD)
70 {
71 return this.filters[0].accept(entry)
72 && this.filters[1].accept(entry);
73 }
74 return this.filters[0].accept(entry) || this.filters[1].accept(entry);
75 }
76
77 /***
78 * @see nl.tudelft.simulation.language.filters.AbstractFilter#getCriterium()
79 */
80 public String getCriterium()
81 {
82 return "composed[" + this.filters[0].getCriterium() + ";"
83 + this.filters[0].getCriterium() + ";" + this.operator + "]";
84 }
85 }