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     */
017    package org.apache.commons.functor.core;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.BinaryPredicate;
022    import org.apache.commons.functor.UnaryPredicate;
023    import 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: 1160389 $ $Date: 2011-08-22 21:31:15 +0200 (Mon, 22 Aug 2011) $
040     * @author Rodney Waldhoff
041     */
042    public final class IsEqual<L, R> implements BinaryPredicate<L, R>, Serializable {
043        // static attributes
044        // ------------------------------------------------------------------------
045        /**
046         * Basic IsEqual<Object, Object> instance.
047         */
048        public static final IsEqual<Object, Object> INSTANCE = IsEqual.<Object, Object>instance();
049        /**
050         * serialVersionUID declaration.
051         */
052        private static final long serialVersionUID = -6777016236280121159L;
053    
054        // constructor
055        // ------------------------------------------------------------------------
056        /**
057         * Create a new IsEqual.
058         */
059        public IsEqual() {
060        }
061    
062        // predicate interface
063        // ------------------------------------------------------------------------
064        /**
065         * {@inheritDoc}
066         */
067        public boolean test(L left, R right) {
068            return left == right || left != null && left.equals(right);
069        }
070    
071        /**
072         * {@inheritDoc}
073         */
074        public boolean equals(Object that) {
075            return that instanceof IsEqual<?, ?>;
076        }
077    
078        /**
079         * {@inheritDoc}
080         */
081        public int hashCode() {
082            return "IsEqual".hashCode();
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public String toString() {
089            return "IsEqual";
090        }
091    
092        // static methods
093        // ------------------------------------------------------------------------
094        /**
095         * Get an IsEqual instance.
096         * @param <L> the left argument type.
097         * @param <R> the right argument type.
098         * @return IsEqual
099         */
100        public static <L, R> IsEqual<L, R> instance() {
101            return new IsEqual<L, R>();
102        }
103    
104        /**
105         * Get an IsEqual UnaryPredicate.
106         * @param <L> the left argument type.
107         * @param <R> the right argument type.
108         * @param object bound comparison object
109         * @return UnaryPredicate<L>
110         */
111        public static <L, R> UnaryPredicate<L> to(R object) {
112            return new RightBoundPredicate<L>(new IsEqual<L, R>(), object);
113        }
114    }