View Javadoc

1   /***
2    * OrderFilter.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  import java.util.Comparator;
30  
31  /***
32   * A filter that accepts objects that fulfill a given comparison relation with a
33   * given referent object, according to a given Comparator object.
34   * 
35   * @author $Author: dbregeon $
36   * @version $Revision: 1.3 $
37   */
38  public class OrderFilter implements Filter, Serializable {
39  	static final long serialVersionUID = -8352083873613992423L;
40  
41  	/***
42  	 * The operator used by this filter.
43  	 */
44  	private final OrderComparisonOperator operator;
45  
46  	/***
47  	 * The comparator object used to compare 'tested' objects with the referent
48  	 * object.
49  	 */
50  	private final Comparator comparator;
51  
52  	/***
53  	 * The referent object which 'tested' objects are compared to.
54  	 */
55  	private final Object referent;
56  
57  	/***
58  	 * Creates new OrderComparator
59  	 * 
60  	 * @param op
61  	 *            The OrderComparisonOperator to be used.
62  	 * @param comp
63  	 *            The Comparator object to be used to compare objects to the
64  	 *            referent object.
65  	 * @param referent
66  	 *            The referent object 'tested' objects have to be compare to.
67  	 * @throws IllegalArgumentException
68  	 *             In case any of the passed in parameters is null, which is not
69  	 *             wanted here.
70  	 */
71  	public OrderFilter(OrderComparisonOperator op, Comparator comp,
72  			Object referent) throws IllegalArgumentException {
73  		if ((op == null) || (comp == null) || (referent == null)) {
74  			throw new IllegalArgumentException("Null is no valid value.");
75  		}
76  		this.operator = op;
77  		this.comparator = comp;
78  		this.referent = referent;
79  	}
80  
81  	/***
82  	 * Tells whether the given object 'passes' the filter or not.
83  	 * 
84  	 * @return True if the passed in parameter is accepted by the filter, false
85  	 *         otherwise.
86  	 * @param o
87  	 *            The object you want to 'test'.
88  	 */
89  	public boolean accept(final Object o) {
90  		boolean result = false;
91  		final int temp = this.comparator.compare(o, this.referent);
92  		if (this.operator == OrderComparisonOperator.LESSER_THAN) {
93  			result = (temp < 0);
94  		} else if (this.operator == OrderComparisonOperator.LESSER_THAN_EQUAL) {
95  			result = (temp <= 0);
96  		} else if (this.operator == OrderComparisonOperator.GREATER_THAN) {
97  			result = (temp > 0);
98  		} else if (this.operator == OrderComparisonOperator.GREATER_THAN_EQUAL) {
99  			result = (temp >= 0);
100 		} else if (this.operator == OrderComparisonOperator.EQUAL) {
101 			result = (temp == 0);
102 		} else if (this.operator == OrderComparisonOperator.NOT_EQUAL) {
103 			result = (temp != 0);
104 		}
105 		return result;
106 	}
107 
108 	/***
109 	 * Gives access to the operator used by this filter.
110 	 * 
111 	 * @return The comparison operator used by this filter.
112 	 */
113 	public ComparisonOperator getOperator() {
114 		return this.operator;
115 	}
116 
117 	/***
118 	 * Gives access to the referent object used by this filter.
119 	 * 
120 	 * @return The referent object used by this filter.
121 	 */
122 	public Object getReferent() {
123 		return this.referent;
124 	}
125 
126 	/***
127 	 * Gives access to the COmparator object used by this filter.
128 	 * 
129 	 * @return The Comparator object used by this filter.
130 	 */
131 	public Comparator getComparator() {
132 		return this.comparator;
133 	}
134 
135 	/***
136 	 * Gives an SQL-like string representation for this filter.
137 	 * 
138 	 * @return the string representation of the filter.
139 	 */
140 	public String toString() {
141 		return this.operator + " " + this.referent;
142 	}
143 
144 	/***
145 	 * This method is useful when generating SQL queries. This method will
146 	 * return the actual SQL condition.
147 	 * 
148 	 * @param field
149 	 *            the field name that applies to this filter.
150 	 * @return the string representation of the filter including the field name.
151 	 */
152 	public String toString(final String field) {
153 		final StringBuffer result = new StringBuffer(toString());
154 		if (field != null) {
155 			result.insert(0, " ");
156 			result.insert(0, field);
157 		}
158 		return result.toString();
159 	}
160 
161 	/***
162 	 * Overloads the 'dummy' implementation in Object, so that it returns true
163 	 * only if the passed in parameter is a filter of the same kind as this one,
164 	 * and accepts exactly the same objects.
165 	 * 
166 	 * @return True if the passed in object is an OrderFilter that accepts
167 	 *         exactly the same objects as this one.
168 	 * @param o
169 	 *            The object to be compared to this one.
170 	 */
171 	public boolean equals(final Object o) {
172 		boolean result = false;
173 		if (getClass().isInstance(o)) {
174 			final OrderFilter filter = (OrderFilter) o;
175 			result = (this.operator == filter.getOperator());
176 			result = result
177 					&& ((this.comparator == filter.getComparator()) || ((this.comparator != null) && (this.comparator
178 							.getClass().equals(filter.getComparator()
179 							.getClass()))));
180 			result = result && (this.referent.equals(filter.getReferent()));
181 		}
182 		return result;
183 	}
184 
185 	/***
186 	 * Overloads the 'dummy' implementation in Object so that it fulfills the
187 	 * implicit hashCode contract, i.e. same filters have the same hashCode and
188 	 * different ones have different hashCodes.
189 	 * 
190 	 * @return The hash code for this filter.
191 	 */
192 	public int hashCode() {
193 		return this.operator.hashCode() ^ this.referent.hashCode();
194 	}
195 
196 	/***
197 	 * This forces override of the Object class clone method. it makes a deep
198 	 * copy so a change in a subfilter does not affect the cloned version.
199 	 * 
200 	 * @return the cloned object.
201 	 */
202 	public Object clone() {
203 		Object result = null;
204 		try {
205 			result = super.clone();
206 		} catch (CloneNotSupportedException e) {
207 			e.printStackTrace();
208 		}
209 		return result;
210 	}
211 
212 }