1 /***
2 * InclusionFilter.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.Arrays;
30 import java.util.Iterator;
31 import java.util.List;
32
33 /***
34 * A filters that tests the inclusion (or non-inclusion) of the tested object in
35 * a set of accepted values.
36 *
37 * @author $Author: dbregeon $
38 * @version $Revision: 1.3 $
39 */
40 public class InclusionFilter implements Filter, Serializable {
41 static final long serialVersionUID = -7530563440355198322L;
42
43 /***
44 * The inclusion operator used by this filter (IN or NOT_IN).
45 */
46 private final InclusionComparisonOperator operator;
47
48 /***
49 * The set of accepted values.
50 */
51 private final List acceptedValues;
52
53 /***
54 * Creates new InclusionComparator
55 *
56 * @param op
57 * The inclusion operator to be used.
58 * @param list
59 * The set of accepted values.
60 * @throws IllegalArgumentException
61 * In case one of the passed in parameters is null, which is not
62 * wanted here.
63 */
64 public InclusionFilter(InclusionComparisonOperator op, Object[] list)
65 throws IllegalArgumentException {
66 if ((op == null) || (list == null)) {
67 throw new IllegalArgumentException("Null is no valid value.");
68 }
69 for (int i = 0; i < list.length; i++) {
70 if (list[i] == null) {
71 throw new IllegalArgumentException(
72 "list contains a null value.");
73 }
74 }
75 this.operator = op;
76 this.acceptedValues = Arrays.asList(list);
77 }
78
79 /***
80 * Tells whether the given object 'passes' the filter or not.
81 *
82 * @return True if the passed in parameter is accepted by the filter, false
83 * otherwise.
84 * @param o
85 * The object you want to 'test'.
86 */
87 public boolean accept(final Object o) {
88 boolean result = this.acceptedValues.contains(o);
89 if (this.operator == InclusionComparisonOperator.NOT_IN) {
90 result = !result;
91 }
92 return result;
93 }
94
95 /***
96 * This method enables access to the list of values that are admissible
97 * through the filter.
98 *
99 * @return an array containing the values.
100 */
101 public Object[] getAcceptedValues() {
102 return this.acceptedValues.toArray();
103 }
104
105 /***
106 * This method enables access to the comparison operator used in this
107 * filter.
108 *
109 * @return the operator.
110 */
111 public ComparisonOperator getOperator() {
112 return this.operator;
113 }
114
115 /***
116 * Gives an SQL-like string representation of this filter.
117 *
118 * @return the string representation of the filter.
119 */
120 public String toString() {
121 final StringBuffer result = new StringBuffer();
122 result.append(this.operator).append(" (");
123 final Iterator iter = this.acceptedValues.iterator();
124 while (iter.hasNext()) {
125 result.append("'").append(iter.next()).append("'");
126 if (iter.hasNext()) {
127 result.append(",");
128 }
129 }
130 result.append(")");
131 return result.toString();
132 }
133
134 /***
135 * Overloads the 'dummy' implementation in Object, so that it returns true
136 * only if the passed in parameter is a filter of the same kind as this one,
137 * and accepts exactly the same objects.
138 *
139 * @return True if the passed in object is an InclusionFilter that accepts
140 * exactly the same objects as this one.
141 * @param o
142 * The object to be compared to this one.
143 */
144 public boolean equals(final Object o) {
145 boolean result = false;
146 if (o instanceof InclusionFilter) {
147 final InclusionFilter filter = (InclusionFilter) o;
148 result = (filter.operator == this.operator);
149 result = result
150 && (filter.acceptedValues.equals(this.acceptedValues));
151 }
152 return result;
153 }
154
155 /***
156 * Overloads the 'dummy' implementation in Object so that it fulfills the
157 * implicit hashCode contract, i.e. same filters have the same hashCode and
158 * different ones have different hashCodes.
159 *
160 * @return The hash code for this filter.
161 */
162 public int hashCode() {
163 int hash = this.operator.hashCode();
164 final Iterator iter = this.acceptedValues.iterator();
165 while (iter.hasNext()) {
166 hash ^= iter.next().hashCode();
167 }
168 return hash;
169 }
170
171 /***
172 * @see java.lang.Object#clone()
173 */
174 public Object clone() {
175 Object result = null;
176 try {
177
178 result = super.clone();
179 } catch (CloneNotSupportedException e) {
180 e.printStackTrace();
181 }
182 return result;
183 }
184 }