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.adapter;
018    
019    import org.apache.commons.functor.BinaryPredicate;
020    import org.apache.commons.functor.UnaryPredicate;
021    
022    /**
023     * Adapts a BinaryPredicate as a UnaryPredicate by sending the same argument to both sides of the BinaryPredicate.
024     * @param <A> the argument type.
025     * @version $Revision: 1156771 $ $Date: 2011-08-11 21:42:56 +0200 (Thu, 11 Aug 2011) $
026     * @author Matt Benson
027     */
028    public final class BinaryPredicateUnaryPredicate<A> implements UnaryPredicate<A> {
029        /**
030         * The adapted {@link BinaryPredicate}.
031         */
032        private final BinaryPredicate<? super A, ? super A> predicate;
033    
034        /**
035         * Create a new BinaryPredicateUnaryPredicate.
036         * @param predicate to adapt
037         */
038        public BinaryPredicateUnaryPredicate(BinaryPredicate<? super A, ? super A> predicate) {
039            if (null == predicate) {
040                throw new IllegalArgumentException("BinaryPredicate argument was null");
041            }
042            this.predicate = predicate;
043        }
044    
045        /**
046         * {@inheritDoc}
047         */
048        public boolean test(A obj) {
049            return predicate.test(obj, obj);
050        }
051    
052        /**
053         * {@inheritDoc}
054         */
055        @Override
056        public boolean equals(Object obj) {
057            return obj == this || obj instanceof BinaryPredicateUnaryPredicate<?>
058                    && equals((BinaryPredicateUnaryPredicate<?>) obj);
059        }
060    
061        /**
062         * Learn whether another BinaryPredicateUnaryPredicate is equal to
063         * <code>this</code>.
064         *
065         * @param that BinaryPredicateUnaryPredicate to check
066         *
067         * @return whether equal
068         */
069        public boolean equals(BinaryPredicateUnaryPredicate<?> that) {
070            return that != null && that.predicate.equals(this.predicate);
071        }
072    
073        /**
074         * {@inheritDoc}
075         */
076        @Override
077        public int hashCode() {
078            return ("BinaryPredicateUnaryPredicate".hashCode() << 2) | predicate.hashCode();
079        }
080    
081        /**
082         * {@inheritDoc}
083         */
084        @Override
085        public String toString() {
086            return "BinaryPredicateUnaryPredicate<" + predicate + ">";
087        }
088    
089        /**
090         * Adapt a BinaryFunction as a UnaryFunction.
091         * @param <A> the argument type.
092         * @param predicate BinaryPredicate to adapt
093         * @return UnaryPredicate<A>
094         */
095        public static <A> UnaryPredicate<A> adapt(BinaryPredicate<? super A, ? super A> predicate) {
096            return null == predicate ? null : new BinaryPredicateUnaryPredicate<A>(predicate);
097        }
098    
099    }