001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.functors;
018
019import java.io.Serializable;
020import java.util.Objects;
021
022import org.apache.commons.collections4.Equator;
023import org.apache.commons.collections4.Predicate;
024
025/**
026 * Predicate implementation that returns true if the input is the same object
027 * as the one stored in this predicate by equals.
028 *
029 * @param <T> the type of the input to the predicate.
030 * @since 3.0
031 */
032public final class EqualPredicate<T> extends AbstractPredicate<T> implements Serializable {
033
034    /** Serial version UID */
035    private static final long serialVersionUID = 5633766978029907089L;
036
037    /**
038     * Creates the predicate.
039     *
040     * @param <T> the type that the predicate queries
041     * @param object  the object to compare to
042     * @return the predicate
043     */
044    public static <T> Predicate<T> equalPredicate(final T object) {
045        if (object == null) {
046            return NullPredicate.nullPredicate();
047        }
048        return new EqualPredicate<>(object);
049    }
050
051    /**
052     * Creates the identity predicate.
053     *
054     * @param <T> the type that the predicate queries
055     * @param object  the object to compare to
056     * @param equator  the equator to use for comparison
057     * @return the predicate
058     * @since 4.0
059     */
060    public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> equator) {
061        if (object == null) {
062            return NullPredicate.nullPredicate();
063        }
064        return new EqualPredicate<>(object, equator);
065    }
066
067    /** The value to compare to */
068    private final T test;
069
070    /** The equator to use for comparison */
071    private final Equator<T> equator;
072
073    /**
074     * Constructor that performs no validation.
075     * Use {@code equalPredicate} if you want that.
076     *
077     * @param object  the object to compare to
078     */
079    public EqualPredicate(final T object) {
080        // do not use the DefaultEquator to keep backwards compatibility
081        // the DefaultEquator returns also true if the two object references are equal
082        this(object, null);
083    }
084
085    /**
086     * Constructor that performs no validation.
087     * Use {@code equalPredicate} if you want that.
088     *
089     * @param test  the object to compare to
090     * @param equator  the equator to use for comparison
091     * @since 4.0
092     */
093    public EqualPredicate(final T test, final Equator<T> equator) {
094        this.test = test;
095        this.equator = equator;
096    }
097
098    /**
099     * 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}