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;
020
021import org.apache.commons.collections4.Predicate;
022
023/**
024 * Predicate implementation that returns true if the input is the same object
025 * as the one stored in this predicate.
026 *
027 * @since 3.0
028 * @version $Id: IdentityPredicate.html 972421 2015-11-14 20:00:04Z tn $
029 */
030public final class IdentityPredicate<T> implements Predicate<T>, Serializable {
031
032    /** Serial version UID */
033    private static final long serialVersionUID = -89901658494523293L;
034
035    /** The value to compare to */
036    private final T iValue;
037
038    /**
039     * Factory to create the identity predicate.
040     *
041     * @param <T> the type that the predicate queries
042     * @param object  the object to compare to
043     * @return the predicate
044     * @throws IllegalArgumentException if the predicate is null
045     */
046    public static <T> Predicate<T> identityPredicate(final T object) {
047        if (object == null) {
048            return NullPredicate.<T>nullPredicate();
049        }
050        return new IdentityPredicate<T>(object);
051    }
052
053    /**
054     * Constructor that performs no validation.
055     * Use <code>identityPredicate</code> if you want that.
056     *
057     * @param object  the object to compare to
058     */
059    public IdentityPredicate(final T object) {
060        super();
061        iValue = object;
062    }
063
064    /**
065     * Evaluates the predicate returning true if the input object is identical to
066     * the stored object.
067     *
068     * @param object  the input object
069     * @return true if input is the same object as the stored value
070     */
071    public boolean evaluate(final T object) {
072        return iValue == object;
073    }
074
075    /**
076     * Gets the value.
077     *
078     * @return the value
079     * @since 3.1
080     */
081    public T getValue() {
082        return iValue;
083    }
084
085}