1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.collections15.functors.predicate;
17
18 import net.sf.collections15.Predicate;
19
20 import java.io.Serializable;
21
22
23 /***
24 * <code>Predicate</code> implementation that evaluates to <code>true</code> if
25 * the input is equal to the one stored in this <code>Predicate</code>,
26 * according to its <code>equals()</code> method.
27 *
28 * @author Stephen Colebourne
29 * @author Chris Lambrou (port to Java 5.0)
30 * @see SamePredicate
31 * @since Collections15 1.0
32 */
33 public final class EqualPredicate <E> implements Predicate<E>, Serializable
34 {
35
36 static final long serialVersionUID = -1843894372238453124L;
37
38 /***
39 * The value to compare input objects to.
40 */
41 private final E value;
42
43 /***
44 * Returns an instance that compares input objects to the specified value.
45 *
46 * @param value The object to compare input objects to.
47 *
48 * @return An instance that compares input objects to the specified value.
49 *
50 * @throws IllegalArgumentException Thrown if the specified value is
51 * <code>null</code>.
52 */
53 public static <T> EqualPredicate<T> getInstance(T value)
54 {
55 return new EqualPredicate<T>(value);
56 }
57
58 /***
59 * Creates a new instance that compares input objects to the specified
60 * value.
61 *
62 * @param value The object to compare input objects to.
63 *
64 * @throws IllegalArgumentException Thrown if the specified value is
65 * <code>null</code>.
66 */
67 protected EqualPredicate(E value)
68 {
69 if (value == null) {
70 throw new IllegalArgumentException("null value specified");
71 }
72 this.value = value;
73 }
74
75 /***
76 * Evaluates the predicate returning <code>true</code> if the input equals
77 * the stored value.
78 *
79 * @param object the input object
80 *
81 * @return true if input object equals stored value
82 */
83 public boolean evaluate(E object)
84 {
85 return value.equals(object);
86 }
87
88 /***
89 * Gets the value.
90 *
91 * @return The value against which input objects are compared for equality.
92 *
93 * @since Collections15 1.0
94 */
95 public E getValue()
96 {
97 return value;
98 }
99
100 }