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.composite;
018
019import org.apache.commons.functor.BinaryPredicate;
020import org.apache.commons.lang3.Validate;
021
022/**
023 * {@link #test Tests} to the logical inverse
024 * of some other predicate.
025 * @param <L> the left argument type.
026 * @param <R> the right argument type.
027 * @version $Revision: 1537602 $ $Date: 2013-10-31 20:53:09 +0100 (Do, 31 Okt 2013) $
028 */
029public final class BinaryNot<L, R> implements BinaryPredicate<L, R> {
030    // attributes
031    // ------------------------------------------------------------------------
032    /**
033     * The adapted predicate.
034     */
035    private final BinaryPredicate<? super L, ? super R> predicate;
036
037    // constructor
038    // ------------------------------------------------------------------------
039    /**
040     * Create a new BinaryNot.
041     * @param predicate BinaryPredicate to negate
042     */
043    public BinaryNot(BinaryPredicate<? super L, ? super R> predicate) {
044        this.predicate = Validate.notNull(predicate, "BinaryPredicate argument was null");
045    }
046
047    // predicate interface
048    // ------------------------------------------------------------------------
049    /**
050     * {@inheritDoc}
051     */
052    public boolean test(L left, R right) {
053        return !(predicate.test(left, right));
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public boolean equals(Object obj) {
061        if (obj == this) {
062            return true;
063        }
064        if (!(obj instanceof BinaryNot<?, ?>)) {
065            return false;
066        }
067        BinaryNot<?, ?> that = (BinaryNot<?, ?>) obj;
068        return this.predicate.equals(that.predicate);
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public int hashCode() {
076        int hash = "BinaryNot".hashCode();
077        hash ^= predicate.hashCode();
078        return hash;
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public String toString() {
086        return "BinaryNot<" + predicate + ">";
087    }
088
089    // static
090    // ------------------------------------------------------------------------
091    /**
092     * Negate a BinaryPredicate.
093     * @param <L> the left argument type.
094     * @param <R> the right argument type.
095     * @param that BinaryPredicate to negate
096     * @return BinaryPredicate
097     */
098    public static <L, R> BinaryPredicate<L, R> not(BinaryPredicate<? super L, ? super R> that) {
099        return null == that ? null : new BinaryNot<L, R>(that);
100    }
101
102}