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     * not {@link Object#equals equal} or both
029     * <code>null</code>.
030     * <p>
031     * This relation is symmetric but irreflexive
032     * and not transitive.
033     * </p>
034     * @param <L> the left argument type.
035     * @param <R> the right argument type.
036     * @version $Revision: 1160397 $ $Date: 2011-08-22 21:42:42 +0200 (Mon, 22 Aug 2011) $
037     * @author Rodney Waldhoff
038     */
039    public final class IsNotEqual<L, R> implements BinaryPredicate<L, R>, Serializable {
040        // static attributes
041        // ------------------------------------------------------------------------
042        /**
043         * Basic IsNotEqual<Object, Object> instance.
044         */
045        public static final IsNotEqual<Object, Object> INSTANCE = IsNotEqual.<Object, Object>instance();
046        /**
047         * serialVersionUID declaration.
048         */
049        private static final long serialVersionUID = -7303588338955281317L;
050    
051        // constructor
052        // ------------------------------------------------------------------------
053        /**
054         * Create a new IsNotEqual.
055         */
056        public IsNotEqual() {
057        }
058    
059        // predicate interface
060        // ------------------------------------------------------------------------
061        /**
062         * {@inheritDoc}
063         */
064        public boolean test(L left, R right) {
065            return (null == left ? null != right : !left.equals(right));
066        }
067    
068        /**
069         * {@inheritDoc}
070         */
071        public boolean equals(Object that) {
072            return that instanceof IsNotEqual<?, ?>;
073        }
074    
075        /**
076         * {@inheritDoc}
077         */
078        public int hashCode() {
079            return "IsNotEqual".hashCode();
080        }
081    
082        /**
083         * {@inheritDoc}
084         */
085        public String toString() {
086            return "IsNotEqual";
087        }
088    
089        // static methods
090      // ------------------------------------------------------------------------
091    
092        /**
093         * Get an IsNotEqual instance.
094         * @param <L> the left argument type.
095         * @param <R> the right argument type.
096         * @return IsNotEqual<L, R>
097         */
098        public static <L, R> IsNotEqual<L, R> instance() {
099            return new IsNotEqual<L, R>();
100        }
101    
102        /**
103         * Get an IsNotEqual UnaryPredicate.
104         * @param <L> the left argument type.
105         * @param <R> the right argument type.
106         * @param object bound comparison object
107         * @return UnaryPredicate<L>
108         */
109        public static <L, R> UnaryPredicate<L> to(R object) {
110            return new RightBoundPredicate<L>(new IsNotEqual<L, R>(), object);
111        }
112    }