View Javadoc

1   /*
2    * @(#) CompositeFilter.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 composite filter combines two filters.
11   * <p>
12   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
13   * University of Technology </a>, the Netherlands.
14   * <p>
15   * See for project information <a
16   * href="http://www.simulation.tudelft.nl/dsol/language">www.simulation.tudelft.nl/language
17   * </a> <br>
18   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
19   * General Public License (LGPL) </a>, no warranty
20   * 
21   * @author <a
22   *         href="http://web.eur.nl/fbk/dep/dep1/Introduction/Staff/People/Lang">Niels
23   *         Lang </a><a href="http://www.peter-jacobs.com/index.htm">Peter
24   *         Jacobs </a>
25   * @version $Revision: 1.12 $ $Date: 2005/08/04 12:08:54 $
26   * @since 1.5
27   */
28  public class CompositeFilter extends AbstractFilter
29  {
30      /*** the AND operator */
31      public static final short AND = 0;
32  
33      /*** the OR operator */
34      public static final short OR = 1;
35  
36      /*** the operator of the composite filter */
37      private short operator = -1;
38  
39      /*** the filters to compose */
40      private Filterinterface[] filters = new Filterinterface[2];
41  
42      /***
43       * constructs a new CompositeFilter
44       * 
45       * @param filter1 the first filter
46       * @param filter2 the second filter
47       * @param operator the operator (AND or OR)
48       */
49      public CompositeFilter(final Filterinterface filter1,
50              final Filterinterface filter2, final short operator)
51      {
52          super();
53          if (operator < 0 || operator > 1)
54          {
55              throw new IllegalArgumentException("unknown operator");
56          }
57          this.filters[0] = filter1;
58          this.filters[1] = filter2;
59          this.operator = operator;
60      }
61  
62      /***
63       * @see nl.tudelft.simulation.language.filters.AbstractFilter#filter(java.lang.Object)
64       */
65      @Override
66  	protected boolean filter(final Object entry)
67      {
68          if (this.operator == CompositeFilter.AND)
69          {
70              return this.filters[0].accept(entry)
71                      && this.filters[1].accept(entry);
72          }
73          return this.filters[0].accept(entry) || this.filters[1].accept(entry);
74      }
75  
76      /***
77       * Converts the operator of this filter into a human readable string.
78       * 
79       * @return the operator in human readable string
80       */
81      protected String operatorToString()
82      {
83          if (this.operator == AND)
84          {
85              return "AND";
86          }
87          return "OR";
88      }
89  
90      /***
91       * @see nl.tudelft.simulation.language.filters.AbstractFilter#getCriterium()
92       */
93      @Override
94  	public String getCriterium()
95      {
96          return "composed[" + this.filters[0].getCriterium() + ";"
97                  + operatorToString() + ";" + this.filters[1].getCriterium()
98                  + "]";
99      }
100 }