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.collections4.functors;
18
19 import java.io.Serializable;
20 import java.util.Objects;
21
22 import org.apache.commons.collections4.Equator;
23 import org.apache.commons.collections4.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 * @param <T> the type of the input to the predicate.
30 * @since 3.0
31 */
32 public final class EqualPredicate<T> extends AbstractPredicate<T> implements Serializable {
33
34 /** Serial version UID */
35 private static final long serialVersionUID = 5633766978029907089L;
36
37 /**
38 * Creates the predicate.
39 *
40 * @param <T> the type that the predicate queries
41 * @param object the object to compare to
42 * @return the predicate
43 */
44 public static <T> Predicate<T> equalPredicate(final T object) {
45 if (object == null) {
46 return NullPredicate.nullPredicate();
47 }
48 return new EqualPredicate<>(object);
49 }
50
51 /**
52 * Creates the identity predicate.
53 *
54 * @param <T> the type that the predicate queries
55 * @param object the object to compare to
56 * @param equator the equator to use for comparison
57 * @return the predicate
58 * @since 4.0
59 */
60 public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> equator) {
61 if (object == null) {
62 return NullPredicate.nullPredicate();
63 }
64 return new EqualPredicate<>(object, equator);
65 }
66
67 /** The value to compare to */
68 private final T test;
69
70 /** The equator to use for comparison */
71 private final Equator<T> equator;
72
73 /**
74 * Constructor that performs no validation.
75 * Use {@code equalPredicate} if you want that.
76 *
77 * @param object the object to compare to
78 */
79 public EqualPredicate(final T object) {
80 // do not use the DefaultEquator to keep backwards compatibility
81 // the DefaultEquator returns also true if the two object references are equal
82 this(object, null);
83 }
84
85 /**
86 * Constructor that performs no validation.
87 * Use {@code equalPredicate} if you want that.
88 *
89 * @param test the object to compare to
90 * @param equator the equator to use for comparison
91 * @since 4.0
92 */
93 public EqualPredicate(final T test, final Equator<T> equator) {
94 this.test = test;
95 this.equator = equator;
96 }
97
98 /**
99 * Gets the value.
100 *
101 * @return the value
102 * @since 3.1
103 */
104 public Object getValue() {
105 return test;
106 }
107
108 /**
109 * Evaluates the predicate returning true if the input equals the stored value.
110 *
111 * @param object the input object
112 * @return true if input object equals stored value
113 */
114 @Override
115 public boolean test(final T object) {
116 if (equator != null) {
117 return equator.equate(test, object);
118 }
119 return Objects.equals(test, object);
120 }
121
122 }