1 /***
2 * IntervalFilter.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 import java.io.Serializable;
29
30 /***
31 * This filter enables to perform an AND between two OrderFilters. Therefore it
32 * models an interval of values. This class should be overriden to provide a
33 * stronger typing. No accessor methods are provided here so that the
34 * implementing classes can provide a typed method.
35 *
36 * @author $Author: dbregeon $
37 * @version $Revision: 1.3 $
38 */
39 public class IntervalFilter implements Filter, Serializable, Cloneable {
40 static final long serialVersionUID = -5828731130497450560L;
41
42 /***
43 * The property that is used to signal changes of the lower filter.
44 */
45 public static final String LOWER_FILTER_PROPERTY = "lowerFilter";
46
47 /***
48 * The property that is used to signal changes of the upper filter.
49 */
50 public static final String UPPER_FILTER_PROPERTY = "upperFilter";
51
52 /***
53 * The order filter that defines the lower end of the interval.
54 */
55 private OrderFilter lowerFilter = null;
56
57 /***
58 * The order filter that defines the upper end of the interval.
59 */
60 private OrderFilter upperFilter = null;
61
62 /***
63 * Creates a new instance of IntervalFilter
64 *
65 * @param lowerFilter
66 * the lower filter for the interval.
67 * @param upperFilter
68 * the upper filter for the interval.
69 */
70 public IntervalFilter(OrderFilter lowerFilter, OrderFilter upperFilter) {
71 this.lowerFilter = lowerFilter;
72 this.upperFilter = upperFilter;
73 }
74
75 /***
76 * @param filter
77 * a filter to restrict the current interval.
78 */
79 protected void setLowerFilter(final OrderFilter filter) {
80 this.lowerFilter = filter;
81 }
82
83 /***
84 * @return the lower filter of the interval.
85 */
86 protected OrderFilter getLowerFilterInternal() {
87 return this.lowerFilter;
88 }
89
90 /***
91 * @param filter
92 * a filter to restrict the current interval.
93 */
94 protected void setUpperFilter(final OrderFilter filter) {
95 this.upperFilter = filter;
96 }
97
98 /***
99 * @return the upper filter of the interval.
100 */
101 protected OrderFilter getUpperFilterInternal() {
102 return this.upperFilter;
103 }
104
105 /***
106 * This is the actual filtering method.
107 *
108 * @param obj
109 * the Object to filter.
110 *
111 * @return true if the given object satisfies all the conditions of the
112 * filter.
113 */
114 public boolean accept(final Object obj) {
115 return (((this.lowerFilter == null) || (this.lowerFilter.accept(obj))) && ((this.upperFilter == null) || (this.upperFilter
116 .accept(obj))));
117 }
118
119 /***
120 * @see Object#toString()
121 */
122 public String toString() {
123 final StringBuffer buffer = new StringBuffer();
124 if (this.lowerFilter != null) {
125 buffer.append("(");
126 buffer.append(this.lowerFilter);
127 buffer.append(")");
128 if (this.upperFilter != null) {
129 buffer.append(" AND ");
130 }
131 }
132 if (this.upperFilter != null) {
133 buffer.append("(");
134 buffer.append(this.upperFilter);
135 buffer.append(")");
136 }
137 return buffer.toString();
138 }
139
140 /***
141 * @see Object#hashCode()
142 */
143 public int hashCode() {
144 int result = 0;
145 if (this.lowerFilter != null) {
146 result ^= this.lowerFilter.hashCode();
147 }
148 if (this.upperFilter != null) {
149 result ^= this.upperFilter.hashCode();
150 }
151 return result;
152 }
153
154 /***
155 * @see Object#equals(java.lang.Object)
156 */
157 public boolean equals(final Object o) {
158 boolean result = false;
159 if (o instanceof IntervalFilter) {
160 final IntervalFilter filter = (IntervalFilter) o;
161 result = ((this.lowerFilter == null) ? (filter.lowerFilter == null)
162 : (this.lowerFilter.equals(filter.lowerFilter)))
163 && ((this.upperFilter == null) ? (filter.upperFilter == null)
164 : (this.upperFilter.equals(filter.upperFilter)));
165 }
166 return result;
167 }
168
169 /***
170 * @see Object#clone()
171 */
172 public Object clone() {
173 IntervalFilter result = null;
174 try {
175 result = (IntervalFilter) super.clone();
176 result.lowerFilter = (OrderFilter) this.lowerFilter.clone();
177 result.upperFilter = (OrderFilter) this.upperFilter.clone();
178 } catch (CloneNotSupportedException e) {
179 e.printStackTrace();
180 }
181 return result;
182 }
183 }