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
026     * {@link Predicate Predicate}
027     * to the
028     * {@link Function Function} 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 predicate is.  Attempts to serialize
034     * an instance whose delegate is not
035     * <code>Serializable</code> will result in an exception.
036     *
037     * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
038     * @author Rodney Waldhoff
039     */
040    public final class PredicateFunction implements Function<Boolean>, Serializable {
041        /**
042         * serialVersionUID declaration.
043         */
044        private static final long serialVersionUID = -8858981355549412629L;
045        /** The {@link Predicate Predicate} I'm wrapping. */
046        private final Predicate predicate;
047    
048        /**
049         * Create a new PredicateFunction.
050         * @param predicate to adapt
051         */
052        public PredicateFunction(Predicate predicate) {
053            if (predicate == null) {
054                throw new IllegalArgumentException("Predicate argument was null");
055            }
056            this.predicate = predicate;
057        }
058    
059        /**
060         * {@inheritDoc}
061         * Returns <code>Boolean.TRUE</code> (<code>Boolean.FALSE</code>)
062         * when the {@link Predicate#test test} method of my underlying
063         * predicate returns <code>true</code> (<code>false</code>).
064         *
065         * @return a non-<code>null</code> <code>Boolean</code> instance
066         */
067        public Boolean evaluate() {
068            return predicate.test();
069        }
070    
071        /**
072         * {@inheritDoc}
073         */
074        public boolean equals(Object that) {
075            return that == this || (that instanceof PredicateFunction && equals((PredicateFunction) that));
076        }
077    
078        /**
079         * Learn whether another PredicateFunction is equal to this.
080         * @param that PredicateFunction to test
081         * @return boolean
082         */
083        public boolean equals(PredicateFunction that) {
084            return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
085        }
086    
087        /**
088         * {@inheritDoc}
089         */
090        public int hashCode() {
091            int hash = "PredicateFunction".hashCode();
092            if (null != predicate) {
093                hash ^= predicate.hashCode();
094            }
095            return hash;
096        }
097    
098        /**
099         * {@inheritDoc}
100         */
101        public String toString() {
102            return "PredicateFunction<" + predicate + ">";
103        }
104    
105        /**
106         * Adapt a Predicate to the Function interface.
107         * @param predicate to adapt
108         * @return PredicateFunction
109         */
110        public static PredicateFunction adapt(Predicate predicate) {
111            return null == predicate ? null : new PredicateFunction(predicate);
112        }
113    
114    }