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.Function;
022    import org.apache.commons.functor.Predicate;
023    
024    /**
025     * Adapts a <code>Boolean</code>-valued
026     * {@link Function Function} to the
027     * {@link Predicate Predicate} interface.
028     * <p/>
029     * Note that although this class implements
030     * {@link Serializable}, a given instance will
031     * only be truly <code>Serializable</code> if the
032     * underlying functor is.  Attempts to serialize
033     * an instance whose delegate is not
034     * <code>Serializable</code> will result in an exception.
035     *
036     * @version $Revision: 1157608 $ $Date: 2011-08-14 21:40:57 +0200 (Sun, 14 Aug 2011) $
037     * @author Rodney Waldhoff
038     */
039    public final class FunctionPredicate implements Predicate, Serializable {
040    
041        /**
042         * serialVersionUID declaration.
043         */
044        private static final long serialVersionUID = 6564796937660102222L;
045        /** The {@link Function Function} I'm wrapping. */
046        private final Function<Boolean> function;
047    
048        /**
049         * Create a new FunctionPredicate.
050         * @param function to adapt
051         */
052        public FunctionPredicate(Function<Boolean> function) {
053            if (function == null) {
054                throw new IllegalArgumentException("Function argument was null");
055            }
056            this.function = function;
057        }
058    
059        /**
060         * Returns the <code>boolean</code> value of the non-<code>null</code>
061         * <code>Boolean</code> returned by the {@link Function#evaluate evaluate}
062         * method of my underlying function.
063         * {@inheritDoc}
064         */
065        public boolean test() {
066            return function.evaluate();
067        }
068    
069        /**
070         * {@inheritDoc}
071         */
072        public boolean equals(Object that) {
073            return that == this || (that instanceof FunctionPredicate && equals((FunctionPredicate) that));
074        }
075    
076        /**
077         * Learn whether another FunctionPredicate is equal to this.
078         * @param that FunctionPredicate to test
079         * @return boolean
080         */
081        public boolean equals(FunctionPredicate that) {
082            return null != that && (null == function ? null == that.function : function.equals(that.function));
083        }
084    
085        /**
086         * {@inheritDoc}
087         */
088        public int hashCode() {
089            int hash = "FunctionPredicate".hashCode();
090            if (null != function) {
091                hash ^= function.hashCode();
092            }
093            return hash;
094        }
095    
096        /**
097         * {@inheritDoc}
098         */
099        public String toString() {
100            return "FunctionPredicate<" + function + ">";
101        }
102    
103        /**
104         * Adapt a Function as a Predicate.
105         * @param function to adapt
106         * @return FunctionPredicate
107         */
108        public static FunctionPredicate adapt(Function<Boolean> function) {
109            return null == function ? null : new FunctionPredicate(function);
110        }
111    }