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.Function;
020import org.apache.commons.functor.Predicate;
021import org.apache.commons.lang3.Validate;
022
023/**
024 * Adapts a
025 * {@link Predicate Predicate}
026 * to the
027 * {@link Function Function} interface.
028 *
029 * @param <A> the argument type.
030 * @version $Revision: 1537906 $ $Date: 2013-11-01 12:47:33 +0100 (Fr, 01 Nov 2013) $
031 */
032public final class PredicateFunction<A> implements Function<A, Boolean> {
033    /** The {@link Predicate Predicate} I'm wrapping. */
034    private final Predicate<? super A> predicate;
035
036    /**
037     * Create a new PredicateFunction.
038     * @param predicate to adapt
039     */
040    public PredicateFunction(Predicate<? super A> predicate) {
041        this.predicate = Validate.notNull(predicate, "Predicate argument was null");
042    }
043
044    /**
045     * {@inheritDoc}
046     * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>)
047     * when the {@link Predicate#test test} method of my underlying
048     * predicate returns <code>true</code> (<code>false</code>).
049     *
050     * @return a non-<code>null</code> <code>Boolean</code> instance
051     */
052    public Boolean evaluate(A obj) {
053        return Boolean.valueOf(predicate.test(obj));
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 PredicateFunction<?>)) {
065            return false;
066        }
067        PredicateFunction<?> that = (PredicateFunction<?>) obj;
068        return this.predicate.equals(that.predicate);
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    @Override
075    public int hashCode() {
076        int hash = "PredicateFunction".hashCode();
077        hash ^= predicate.hashCode();
078        return hash;
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public String toString() {
086        return "PredicateFunction<" + predicate + ">";
087    }
088
089    /**
090     * Adapt a Predicate to the Function interface.
091     * @param <A> the argument type.
092     * @param predicate to adapt
093     * @return PredicateFunction
094     */
095    public static <A> PredicateFunction<A> adapt(Predicate<? super A> predicate) {
096        return null == predicate ? null : new PredicateFunction<A>(predicate);
097    }
098
099}