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} the reference (==) equality of its arguments.
027     *
028     * @param <L> the left argument type.
029     * @param <R> the right argument type.
030     * @version $Revision: 1160410 $ $Date: 2011-08-22 22:06:40 +0200 (Mon, 22 Aug 2011) $
031     * @author Matt Benson
032     */
033    public final class IsSame<L, R> implements BinaryPredicate<L, R>, Serializable {
034        // static attributes
035        // ------------------------------------------------------------------------
036        /**
037         * Basic IsSame<Object, Object> instance.
038         */
039        public static final IsSame<Object, Object> INSTANCE = IsSame.<Object, Object>instance();
040        /**
041         * serialVersionUID declaration.
042         */
043        private static final long serialVersionUID = 7024585699909734072L;
044    
045        // constructor
046        // ------------------------------------------------------------------------
047        /**
048         * Create a new IsSame.
049         */
050        public IsSame() {
051        }
052    
053        // predicate interface
054        // ------------------------------------------------------------------------
055        /**
056         * {@inheritDoc}
057         */
058        public boolean test(L left, R right) {
059            return left == right;
060        }
061    
062        /**
063         * {@inheritDoc}
064         */
065        public boolean equals(Object that) {
066            return that instanceof IsSame<?, ?>;
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        public int hashCode() {
073            return "IsSame".hashCode();
074        }
075    
076        /**
077         * {@inheritDoc}
078         */
079        public String toString() {
080            return "IsSame";
081        }
082    
083        // static methods
084        // ------------------------------------------------------------------------
085        /**
086         * Get an IsSame instance.
087         * @param <L> the left argument type.
088         * @param <R> the right argument type.
089         * @return IsSame
090         */
091        public static <L, R> IsSame<L, R> instance() {
092            return new IsSame<L, R>();
093        }
094    
095        /**
096         * Get an IsSame UnaryPredicate.
097         * @param <L> the left argument type.
098         * @param <R> the right argument type.
099         * @param object bound comparison object
100         * @return UnaryPredicate<L>
101         */
102        public static <L, R> UnaryPredicate<L> as(R object) {
103            return new RightBoundPredicate<L>(new IsSame<L, R>(), object);
104        }
105    }