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 BinaryPredicate as a Predicate by sending the same argument to both sides of the BinaryPredicate.
025 * @param <A> the argument type.
026 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
027 */
028public final class BinaryPredicatePredicate<A> implements Predicate<A> {
029    /**
030     * The adapted {@link BinaryPredicate}.
031     */
032    private final BinaryPredicate<? super A, ? super A> predicate;
033
034    /**
035     * Create a new BinaryPredicatePredicate.
036     * @param predicate to adapt
037     */
038    public BinaryPredicatePredicate(BinaryPredicate<? super A, ? super A> predicate) {
039        this.predicate = Validate.notNull(predicate, "BinaryPredicate argument was null");
040    }
041
042    /**
043     * {@inheritDoc}
044     */
045    public boolean test(A obj) {
046        return predicate.test(obj, obj);
047    }
048
049    /**
050     * {@inheritDoc}
051     */
052    @Override
053    public boolean equals(Object obj) {
054        if (obj == this) {
055            return true;
056        }
057        if (!(obj instanceof BinaryPredicatePredicate<?>)) {
058            return false;
059        }
060        BinaryPredicatePredicate<?> that = (BinaryPredicatePredicate<?>) obj;
061        return this.predicate.equals(that.predicate);
062    }
063
064    /**
065     * {@inheritDoc}
066     */
067    @Override
068    public int hashCode() {
069        return ("BinaryPredicatePredicate".hashCode() << 2) | predicate.hashCode();
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public String toString() {
077        return "BinaryPredicatePredicate<" + predicate + ">";
078    }
079
080    /**
081     * Adapt a BinaryPredicate as a Predicate.
082     * @param <A> the argument type.
083     * @param predicate BinaryPredicate to adapt
084     * @return Predicate<A>
085     */
086    public static <A> Predicate<A> adapt(BinaryPredicate<? super A, ? super A> predicate) {
087        return null == predicate ? null : new BinaryPredicatePredicate<A>(predicate);
088    }
089
090}