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