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.functor.core;
018
019import org.apache.commons.functor.BinaryPredicate;
020import org.apache.commons.functor.Predicate;
021import org.apache.commons.functor.adapter.RightBoundPredicate;
022
023/**
024 * {@link #test Tests}
025 * <code>true</code> iff its arguments are
026 * {@link Object#equals equal} or both
027 * <code>null</code>.
028 * <p>
029 * This relation is
030 * an equivalence relation on
031 * the set of objects that adhere to the
032 * <code>Object.equals</code> contract.
033 * </p>
034 *
035 * @param <L> the left argument type.
036 * @param <R> the right argument type.
037 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
038 */
039public final class IsEqual<L, R> implements BinaryPredicate<L, R> {
040    // static attributes
041    // ------------------------------------------------------------------------
042    /**
043     * Basic IsEqual<Object, Object> instance.
044     */
045    public static final IsEqual<Object, Object> INSTANCE = IsEqual.<Object, Object>instance();
046
047    // constructor
048    // ------------------------------------------------------------------------
049    /**
050     * Create a new IsEqual.
051     */
052    public IsEqual() {
053    }
054
055    // predicate interface
056    // ------------------------------------------------------------------------
057    /**
058     * {@inheritDoc}
059     */
060    public boolean test(L left, R right) {
061        return left == right || left != null && left.equals(right);
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public boolean equals(Object that) {
069        return that instanceof IsEqual<?, ?>;
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public int hashCode() {
077        return "IsEqual".hashCode();
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public String toString() {
085        return "IsEqual";
086    }
087
088    // static methods
089    // ------------------------------------------------------------------------
090    /**
091     * Get an IsEqual instance.
092     * @param <L> the left argument type.
093     * @param <R> the right argument type.
094     * @return IsEqual
095     */
096    public static <L, R> IsEqual<L, R> instance() {
097        return new IsEqual<L, R>();
098    }
099
100    /**
101     * Get an IsEqual Predicate.
102     * @param <L> the left argument type.
103     * @param <R> the right argument type.
104     * @param object bound comparison object
105     * @return Predicate<L>
106     */
107    public static <L, R> Predicate<L> to(R object) {
108        return new RightBoundPredicate<L>(new IsEqual<L, R>(), object);
109    }
110}