View Javadoc

1   /***
2    * ANDFilter.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.filters;
27  
28  /***
29   * This implementation of the ReportFilter interface enables to combine a number
30   * of other ReportFilter with AND logic operators.
31   * 
32   * This means that a value tested through this filter would have to qualify for
33   * each individual component filter.
34   * 
35   * @author $Author: dbregeon $
36   * @version $Revision: 1.2 $
37   */
38  public class ANDFilter extends BaseReportFilter {
39      static final long serialVersionUID = 6883172699329218012L;
40  
41      /***
42       * The filters to combine in this ANDFilter.
43       */
44      private final ReportFilter[] filters;
45  
46      /***
47       * Creates new ANDFilter
48       * 
49       * @param filters
50       *            the filters combined by this ANDFilter.
51       */
52      public ANDFilter(ReportFilter[] filters) {
53          this.filters = filters;
54      }
55  
56      /***
57       * This is the actual filtering method.
58       * 
59       * @param obj
60       *            object to filter.
61       * @return true if the given object satisfies all the conditions of the
62       *         filter.
63       */
64      public boolean accept(final Object obj) {
65          boolean result = true;
66          if (this.filters != null) {
67              for (int i = 0; ((result) && (i < this.filters.length)); i++) {
68                  if (this.filters[i] != null) {
69                      result = result && this.filters[i].accept(obj);
70                  }
71              }
72          }
73          return result;
74      }
75  
76      /***
77       * This method gives access to the filters combined by this ANDFilter.
78       * 
79       * @return the component filters.
80       */
81      public ReportFilter[] getFilters() {
82          return this.filters;
83      }
84  
85      /***
86       * This method clones this filter and the resulting ANDFilter has the
87       * component filters clones as components.
88       * 
89       * @return the cloned ANDFilter.
90       */
91      public Object clone() {
92          final ANDFilter clonedFilter = (ANDFilter) super.clone();
93          for (int i = 0; i < this.filters.length; i++) {
94              if (this.filters[i] != null) {
95                  clonedFilter.filters[i] = ((ReportFilter) this.filters[i]
96                          .clone());
97              }
98          }
99          return clonedFilter;
100     }
101 }