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.core.composite;
018    
019    import java.io.Serializable;
020    
021    import org.apache.commons.functor.UnaryFunction;
022    import org.apache.commons.functor.UnaryPredicate;
023    import org.apache.commons.functor.adapter.UnaryPredicateUnaryFunction;
024    
025    /**
026     * A {@link UnaryPredicate UnaryPredicate}
027     * representing the composition of
028     * {@link UnaryFunction UnaryFunctions},
029     * "chaining" the output of one to the input
030     * of another.  For example,
031     * <pre>new CompositeUnaryPredicate(p).of(f)</pre>
032     * {@link #test tests} to
033     * <code>p.test(f.evaluate(obj))</code>, and
034     * <pre>new CompositeUnaryPredicate(p).of(f).of(g)</pre>
035     * {@link #test tests} to
036     * <code>p.test(f.evaluate(g.evaluate(obj)))</code>.
037     * <p>
038     * Note that although this class implements
039     * {@link Serializable}, a given instance will
040     * only be truly <code>Serializable</code> if all the
041     * underlying functors are.  Attempts to serialize
042     * an instance whose delegates are not all
043     * <code>Serializable</code> will result in an exception.
044     * </p>
045     * @version $Revision: 1166375 $ $Date: 2011-09-07 22:13:25 +0200 (Wed, 07 Sep 2011) $
046     * @author Rodney Waldhoff
047     */
048    public final class CompositeUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
049        /**
050         * serialVersionUID declaration.
051         */
052        private static final long serialVersionUID = 4880363949059265252L;
053        // attributes
054        // ------------------------------------------------------------------------
055        private final CompositeUnaryFunction<? super A, Boolean> function;
056    
057        // constructor
058        // ------------------------------------------------------------------------
059        /**
060         * Create a new CompositeUnaryPredicate.
061         * @param predicate UnaryPredicate against which the composite functions' output will be tested
062         */
063        public CompositeUnaryPredicate(UnaryPredicate<? super A> predicate) {
064            if (null == predicate) {
065                throw new IllegalArgumentException("predicate must not be null");
066            }
067            this.function = new CompositeUnaryFunction<A, Boolean>(new UnaryPredicateUnaryFunction<A>(predicate));
068        }
069    
070        /**
071         * Create a new CompositeUnaryPredicate.
072         * @param function delegate
073         */
074        private CompositeUnaryPredicate(CompositeUnaryFunction<? super A, Boolean> function) {
075            this.function = function;
076        }
077    
078        // modifiers
079        // ------------------------------------------------------------------------
080        /**
081         * Fluently obtain a CompositeUnaryPredicate that applies our predicate to the result of the preceding function.
082         * @param preceding UnaryFunction
083         * @return CompositeUnaryPredicate<P>
084         */
085        public <P> CompositeUnaryPredicate<P> of(UnaryFunction<? super P, ? extends A> preceding) {
086            return new CompositeUnaryPredicate<P>(function.of(preceding));
087        }
088    
089        // predicate interface
090        // ------------------------------------------------------------------------
091        /**
092         * {@inheritDoc}
093         */
094        public boolean test(A obj) {
095            return function.evaluate(obj);
096        }
097    
098        /**
099         * {@inheritDoc}
100         */
101        public boolean equals(Object that) {
102            return that == this || (that instanceof CompositeUnaryPredicate<?>
103                                        && equals((CompositeUnaryPredicate<?>) that));
104        }
105    
106        /**
107         * Learn whether another CompositeUnaryPredicate is equal to this.
108         * @param that CompositeUnaryPredicate to test
109         * @return boolean
110         */
111        public boolean equals(CompositeUnaryPredicate<?> that) {
112            return null != that && function.equals(that.function);
113        }
114    
115        /**
116         * {@inheritDoc}
117         */
118        public int hashCode() {
119            int hash = "CompositeUnaryPredicate".hashCode();
120            hash <<= 2;
121            hash ^= function.hashCode();
122            return hash;
123        }
124    
125        /**
126         * {@inheritDoc}
127         */
128        public String toString() {
129            return "CompositeUnaryFunction<" + function + ">";
130        }
131    
132    }