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 static org.apache.commons.collections4.functors.NullPredicate.nullPredicate;
020
021import java.io.Serializable;
022
023import org.apache.commons.collections4.Equator;
024import org.apache.commons.collections4.Predicate;
025
026/**
027 * Predicate implementation that returns true if the input is the same object
028 * as the one stored in this predicate by equals.
029 *
030 * @since 3.0
031 * @version $Id: EqualPredicate.html 972421 2015-11-14 20:00:04Z tn $
032 */
033public final class EqualPredicate<T> implements Predicate<T>, Serializable {
034
035    /** Serial version UID */
036    private static final long serialVersionUID = 5633766978029907089L;
037
038    /** The value to compare to */
039    private final T iValue;
040
041    /** The equator to use for comparison */
042    private final Equator<T> equator;
043
044    /**
045     * Factory to create the predicate.
046     *
047     * @param <T> the type that the predicate queries
048     * @param object  the object to compare to
049     * @return the predicate
050     * @throws IllegalArgumentException if the predicate is null
051     */
052    public static <T> Predicate<T> equalPredicate(final T object) {
053        if (object == null) {
054            return nullPredicate();
055        }
056        return new EqualPredicate<T>(object);
057    }
058
059    /**
060     * Factory to create the identity predicate.
061     *
062     * @param <T> the type that the predicate queries
063     * @param object  the object to compare to
064     * @param equator  the equator to use for comparison
065     * @return the predicate
066     * @throws IllegalArgumentException if the predicate is null
067     * @since 4.0
068     */
069    public static <T> Predicate<T> equalPredicate(final T object, final Equator<T> equator) {
070        if (object == null) {
071            return nullPredicate();
072        }
073        return new EqualPredicate<T>(object, equator);
074    }
075
076    /**
077     * Constructor that performs no validation.
078     * Use <code>equalPredicate</code> if you want that.
079     *
080     * @param object  the object to compare to
081     */
082    public EqualPredicate(final T object) {
083        // do not use the DefaultEquator to keep backwards compatibility
084        // the DefaultEquator returns also true if the two object references are equal
085        this(object, null);
086    }
087
088    /**
089     * Constructor that performs no validation.
090     * Use <code>equalPredicate</code> if you want that.
091     *
092     * @param object  the object to compare to
093     * @param equator  the equator to use for comparison
094     * @since 4.0
095     */
096    public EqualPredicate(final T object, final Equator<T> equator) {
097        super();
098        iValue = object;
099        this.equator = equator;
100    }
101
102    /**
103     * Evaluates the predicate returning true if the input equals the stored value.
104     *
105     * @param object  the input object
106     * @return true if input object equals stored value
107     */
108    public boolean evaluate(final T object) {
109        if (equator != null) {
110            return equator.equate(iValue, object);
111        } else {
112            return iValue.equals(object);
113        }
114    }
115
116    /**
117     * Gets the value.
118     *
119     * @return the value
120     * @since 3.1
121     */
122    public Object getValue() {
123        return iValue;
124    }
125
126}