1 /***
2 * ORFilter.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 OR logic operators.
31 *
32 * This means that a value tested through this filter would have to qualify for
33 * one component filter.
34 *
35 * @author $Author: dbregeon $
36 * @version $Revision: 1.2 $
37 */
38 public class ORFilter extends BaseReportFilter {
39 static final long serialVersionUID = 541328042785797656L;
40
41 /***
42 * The filters to combine in this ANDFilter.
43 */
44 private final ReportFilter[] filters;
45
46 /***
47 * Creates new ORFilter
48 *
49 * @param filters
50 * the filters combined by this ORFilter.
51 */
52 public ORFilter(ReportFilter[] filters) {
53 this.filters = filters;
54 }
55
56 /***
57 * Creates a binary ORFilter
58 *
59 * @param firstFilter
60 * the first filter in the pair.
61 * @param secondFilter
62 * the second filter in the pair.
63 */
64 public ORFilter(ReportFilter firstFilter, ReportFilter secondFilter) {
65 this.filters = new ReportFilter[2];
66 this.filters[0] = firstFilter;
67 this.filters[1] = secondFilter;
68 }
69
70 /***
71 * This is the actual filtering method.
72 *
73 * @param obj
74 * the Object to Filter.
75 *
76 * @return true if the given object satisfies one of the conditions of the
77 * filter.
78 */
79 public boolean accept(final Object obj) {
80 boolean result = false;
81 for (int i = 0; ((!result) && (i < this.filters.length)); i++) {
82 result = result || this.filters[i].accept(obj);
83 }
84 return result;
85 }
86
87 /***
88 * This method gives access to the filters combined by this ORFilter.
89 *
90 * @return the component filters.
91 */
92 public ReportFilter[] getFilters() {
93 return this.filters;
94 }
95
96 /***
97 * This method clones this filter and the resulting ORFilter has the
98 * component filters clones as components.
99 *
100 * @return the cloned ORFilter.
101 */
102 public Object clone() {
103 final ORFilter clonedFilter = (ORFilter) super.clone();
104 for (int i = 0; i < this.filters.length; i++) {
105 if (this.filters[i] != null) {
106 clonedFilter.filters[i] = ((ReportFilter) this.filters[i]
107 .clone());
108 }
109 }
110 return clonedFilter;
111 }
112 }