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