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.adapter;
018
019import org.apache.commons.functor.BinaryPredicate;
020import org.apache.commons.functor.Predicate;
021import org.apache.commons.lang3.Validate;
022
023/**
024 * Adapts a
025 * {@link BinaryPredicate BinaryPredicate}
026 * to the
027 * {@link Predicate Predicate} interface
028 * using a constant left-side argument.
029 *
030 * @param <A> the argument type.
031 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
032 */
033public final class RightBoundPredicate<A> implements Predicate<A> {
034    /** The {@link BinaryPredicate BinaryPredicate} I'm wrapping. */
035    private final BinaryPredicate<? super A, Object> predicate;
036    /** The parameter to pass to {@code predicate}. */
037    private final Object param;
038
039    /**
040     * Create a new RightBoundPredicate.
041     * @param <R> bound arg type
042     * @param predicate the predicate to adapt
043     * @param arg the constant argument to use
044     */
045    @SuppressWarnings("unchecked")
046    public <R> RightBoundPredicate(BinaryPredicate<? super A, ? super R> predicate, R arg) {
047        this.predicate =
048            (BinaryPredicate<? super A, Object>) Validate.notNull(predicate,
049                "BinaryPredicate argument was null");
050        this.param = arg;
051    }
052
053    /**
054     * {@inheritDoc}
055     */
056    public boolean test(A obj) {
057        return predicate.test(obj, param);
058    }
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public boolean equals(Object obj) {
065        if (obj == this) {
066            return true;
067        }
068        if (!(obj instanceof RightBoundPredicate<?>)) {
069            return false;
070        }
071        RightBoundPredicate<?> that = (RightBoundPredicate<?>) obj;
072        return this.predicate.equals(that.predicate)
073                && (null == this.param ? null == that.param : this.param.equals(that.param));
074    }
075
076    /**
077     * {@inheritDoc}
078     */
079    @Override
080    public int hashCode() {
081        int hash = "RightBoundPredicate".hashCode();
082        hash <<= 2;
083        hash ^= predicate.hashCode();
084        if (null != param) {
085            hash <<= 2;
086            hash ^= param.hashCode();
087        }
088        return hash;
089    }
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public String toString() {
096        return "RightBoundPredicate<" + predicate + "(?," + param + ")>";
097    }
098
099    /**
100     * Adapt a BinaryPredicate as a Predicate.
101     * @param <L> the left argument type.
102     * @param <R> the right argument type.
103     * @param predicate to adapt
104     * @param arg right side
105     * @return RightBoundPredicate
106     */
107    public static <L, R> RightBoundPredicate<L> bind(BinaryPredicate<? super L, ? super R> predicate, R arg) {
108        return null == predicate ? null : new RightBoundPredicate<L>(predicate, arg);
109    }
110
111}