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 java.io.Serializable;
020
021import org.apache.commons.functor.UnaryFunction;
022import org.apache.commons.functor.UnaryPredicate;
023import org.apache.commons.lang3.Validate;
024
025/**
026 * Adapts a <code>Boolean</code>-valued
027 * {@link UnaryFunction UnaryFunction}
028 * to the {@link UnaryPredicate UnaryPredicate}
029 * interface.
030 * <p/>
031 * Note that although this class implements
032 * {@link Serializable}, a given instance will
033 * only be truly <code>Serializable</code> if the
034 * underlying function is.  Attempts to serialize
035 * an instance whose delegate is not
036 * <code>Serializable</code> will result in an exception.
037 *
038 * @param <A> the argument type.
039 * @version $Revision: 1365377 $ $Date: 2012-07-24 20:59:23 -0400 (Tue, 24 Jul 2012) $
040 */
041public final class UnaryFunctionUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
042    /**
043     * serialVersionUID declaration.
044     */
045    private static final long serialVersionUID = -9211927278252224707L;
046    /** The {@link UnaryFunction UnaryFunction} I'm wrapping. */
047    private final UnaryFunction<? super A, Boolean> function;
048
049    /**
050     * Create an {@link UnaryPredicate UnaryPredicate} wrapping
051     * the given {@link UnaryFunction UnaryFunction}.
052     * @param function the {@link UnaryFunction UnaryFunction} to wrap
053     */
054    public UnaryFunctionUnaryPredicate(UnaryFunction<? super A, Boolean> function) {
055        this.function = Validate.notNull(function, "UnaryFunction argument was null");
056    }
057
058    /**
059     * {@inheritDoc}
060     * Returns the <code>boolean</code> value of the non-<code>null</code>
061     * <code>Boolean</code> returned by the {@link UnaryFunction#evaluate evaluate}
062     * method of my underlying function.
063     */
064    public boolean test(A obj) {
065        return function.evaluate(obj).booleanValue();
066    }
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public boolean equals(Object that) {
073        return that == this
074                || (that instanceof UnaryFunctionUnaryPredicate<?> && equals((UnaryFunctionUnaryPredicate<?>) that));
075    }
076
077    /**
078     * Learn whether another UnaryFunctionUnaryPredicate is equal to this.
079     * @param that UnaryFunctionUnaryPredicate to test
080     * @return boolean
081     */
082    public boolean equals(UnaryFunctionUnaryPredicate<?> that) {
083        return null != that && function.equals(that.function);
084    }
085
086    /**
087     * {@inheritDoc}
088     */
089    @Override
090    public int hashCode() {
091        int hash = "UnaryFunctionUnaryPredicate".hashCode();
092        hash ^= function.hashCode();
093        return hash;
094    }
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public String toString() {
101        return "UnaryFunctionUnaryPredicate<" + function + ">";
102    }
103
104    /**
105     * Adapt the given, possibly-<code>null</code>,
106     * {@link UnaryFunction UnaryFunction} to the
107     * {@link UnaryPredicate UnaryPredicate} interface.
108     * When the given <code>UnaryFunction</code> is <code>null</code>,
109     * returns <code>null</code>.
110     *
111     * @param <A> the argument type.
112     * @param function the possibly-<code>null</code>
113     *        {@link UnaryFunction UnaryFunction} to adapt
114     * @return a {@link UnaryPredicate UnaryPredicate} wrapping the given
115     *         {@link UnaryFunction UnaryFunction}, or <code>null</code>
116     *         if the given <code>UnaryFunction</code> is <code>null</code>
117     */
118    public static <A> UnaryFunctionUnaryPredicate<A> adapt(UnaryFunction<? super A, Boolean> function) {
119        return null == function ? null : new UnaryFunctionUnaryPredicate<A>(function);
120    }
121
122}