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 java.io.Serializable;
020    
021    import org.apache.commons.functor.UnaryFunction;
022    import org.apache.commons.functor.UnaryPredicate;
023    
024    /**
025     * Adapts a <code>Boolean</code>-valued
026     * {@link UnaryFunction UnaryFunction}
027     * to the {@link UnaryPredicate UnaryPredicate}
028     * interface.
029     * <p/>
030     * Note that although this class implements
031     * {@link Serializable}, a given instance will
032     * only be truly <code>Serializable</code> if the
033     * underlying function is.  Attempts to serialize
034     * an instance whose delegate is not
035     * <code>Serializable</code> will result in an exception.
036     *
037     * @param <A> the argument type.
038     * @version $Revision: 1157648 $ $Date: 2011-08-14 22:22:47 +0200 (Sun, 14 Aug 2011) $
039     * @author Rodney Waldhoff
040     */
041    public 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            if (function == null) {
056                throw new IllegalArgumentException("UnaryFunction argument was null");
057            }
058            this.function = function;
059        }
060    
061        /**
062         * {@inheritDoc}
063         * Returns the <code>boolean</code> value of the non-<code>null</code>
064         * <code>Boolean</code> returned by the {@link UnaryFunction#evaluate evaluate}
065         * method of my underlying function.
066         */
067        public boolean test(A obj) {
068            return function.evaluate(obj);
069        }
070    
071        /**
072         * {@inheritDoc}
073         */
074        public boolean equals(Object that) {
075            return that == this
076                    || (that instanceof UnaryFunctionUnaryPredicate<?> && equals((UnaryFunctionUnaryPredicate<?>) that));
077        }
078    
079        /**
080         * Learn whether another UnaryFunctionUnaryPredicate is equal to this.
081         * @param that UnaryFunctionUnaryPredicate to test
082         * @return boolean
083         */
084        public boolean equals(UnaryFunctionUnaryPredicate<?> that) {
085            return null != that && (null == function ? null == that.function : function.equals(that.function));
086        }
087    
088        /**
089         * {@inheritDoc}
090         */
091        public int hashCode() {
092            int hash = "UnaryFunctionUnaryPredicate".hashCode();
093            if (null != function) {
094                hash ^= function.hashCode();
095            }
096            return hash;
097        }
098    
099        /**
100         * {@inheritDoc}
101         */
102        public String toString() {
103            return "UnaryFunctionUnaryPredicate<" + function + ">";
104        }
105    
106        /**
107         * Adapt the given, possibly-<code>null</code>,
108         * {@link UnaryFunction UnaryFunction} to the
109         * {@link UnaryPredicate UnaryPredicate} interface.
110         * When the given <code>UnaryFunction</code> is <code>null</code>,
111         * returns <code>null</code>.
112         *
113         * @param <A> the argument type.
114         * @param function the possibly-<code>null</code>
115         *        {@link UnaryFunction UnaryFunction} to adapt
116         * @return a {@link UnaryPredicate UnaryPredicate} wrapping the given
117         *         {@link UnaryFunction UnaryFunction}, or <code>null</code>
118         *         if the given <code>UnaryFunction</code> is <code>null</code>
119         */
120        public static <A> UnaryFunctionUnaryPredicate<A> adapt(UnaryFunction<? super A, Boolean> function) {
121            return null == function ? null : new UnaryFunctionUnaryPredicate<A>(function);
122        }
123    
124    }