1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.collections.functors;
18
19 import static org.apache.commons.collections.functors.NullPredicate.nullPredicate;
20
21 import java.io.Serializable;
22
23 import org.apache.commons.collections.Predicate;
24
25 /**
26 * Predicate implementation that returns true if the input is the same object
27 * as the one stored in this predicate by equals.
28 *
29 * @since 3.0
30 * @version $Id: EqualPredicate.java 1429905 2013-01-07 17:15:14Z ggregory $
31 */
32 public final class EqualPredicate<T> implements Predicate<T>, Serializable {
33
34 /** Serial version UID */
35 private static final long serialVersionUID = 5633766978029907089L;
36
37 /** The value to compare to */
38 private final T iValue;
39
40 /** The equator to use for comparison */
41 private final Equator<T> equator;
42
43 /**
44 * Factory to create the predicate.
45 *
46 * @param <T> the type that the predicate queries
47 * @param object the object to compare to
48 * @return the predicate
49 * @throws IllegalArgumentException if the predicate is null
50 */
51 public static <T> Predicate<T> equalPredicate(final T object) {
52 if (object == null) {
53 return nullPredicate();
54 }
55 return new EqualPredicate<T>(object);
56 }
57
58 /**
59 * Factory to create the identity predicate.
60 *
61 * @param <T> the type that the predicate queries
62 * @param object the object to compare to
63 * @param equator the equator to use for comparison
64 * @return the predicate
65 * @throws IllegalArgumentException if the predicate is null
66 * @since 4.0
67 */
68 public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> equator) {
69 if (object == null) {
70 return nullPredicate();
71 }
72 return new EqualPredicate<T>(object, equator);
73 }
74
75 /**
76 * Constructor that performs no validation.
77 * Use <code>getInstance</code> if you want that.
78 *
79 * @param object the object to compare to
80 */
81 public EqualPredicate(final T object) {
82 // do not use the DefaultEquator to keep backwards compatibility
83 // the DefaultEquator returns also true if the two object references are equal
84 this(object, null);
85 }
86
87 /**
88 * Constructor that performs no validation.
89 * Use <code>getInstance</code> if you want that.
90 *
91 * @param object the object to compare to
92 * @param equator the equator to use for comparison
93 * @since 4.0
94 */
95 public EqualPredicate(final T object, final Equator<T> equator) {
96 super();
97 iValue = object;
98 this.equator = equator;
99 }
100
101 /**
102 * Evaluates the predicate returning true if the input equals the stored value.
103 *
104 * @param object the input object
105 * @return true if input object equals stored value
106 */
107 public boolean evaluate(final T object) {
108 if (equator != null) {
109 return equator.equate(iValue, object);
110 } else {
111 return iValue.equals(object);
112 }
113 }
114
115 /**
116 * Gets the value.
117 *
118 * @return the value
119 * @since 3.1
120 */
121 public Object getValue() {
122 return iValue;
123 }
124
125 }