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