Coverage Report - org.apache.commons.functor.core.composite.CompositeUnaryPredicate
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeUnaryPredicate
100%
17/17
91%
11/12
1.625
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.functor.core.composite;
 18  
 
 19  
 import java.io.Serializable;
 20  
 
 21  
 import org.apache.commons.functor.UnaryFunction;
 22  
 import org.apache.commons.functor.UnaryPredicate;
 23  
 import org.apache.commons.functor.adapter.UnaryPredicateUnaryFunction;
 24  
 
 25  
 /**
 26  
  * A {@link UnaryPredicate UnaryPredicate}
 27  
  * representing the composition of
 28  
  * {@link UnaryFunction UnaryFunctions},
 29  
  * "chaining" the output of one to the input
 30  
  * of another.  For example,
 31  
  * <pre>new CompositeUnaryPredicate(p).of(f)</pre>
 32  
  * {@link #test tests} to
 33  
  * <code>p.test(f.evaluate(obj))</code>, and
 34  
  * <pre>new CompositeUnaryPredicate(p).of(f).of(g)</pre>
 35  
  * {@link #test tests} to
 36  
  * <code>p.test(f.evaluate(g.evaluate(obj)))</code>.
 37  
  * <p>
 38  
  * Note that although this class implements
 39  
  * {@link Serializable}, a given instance will
 40  
  * only be truly <code>Serializable</code> if all the
 41  
  * underlying functors are.  Attempts to serialize
 42  
  * an instance whose delegates are not all
 43  
  * <code>Serializable</code> will result in an exception.
 44  
  * </p>
 45  
  * @version $Revision: 1166375 $ $Date: 2011-09-07 22:13:25 +0200 (Wed, 07 Sep 2011) $
 46  
  * @author Rodney Waldhoff
 47  
  */
 48  
 public final class CompositeUnaryPredicate<A> implements UnaryPredicate<A>, Serializable {
 49  
     /**
 50  
      * serialVersionUID declaration.
 51  
      */
 52  
     private static final long serialVersionUID = 4880363949059265252L;
 53  
     // attributes
 54  
     // ------------------------------------------------------------------------
 55  
     private final CompositeUnaryFunction<? super A, Boolean> function;
 56  
 
 57  
     // constructor
 58  
     // ------------------------------------------------------------------------
 59  
     /**
 60  
      * Create a new CompositeUnaryPredicate.
 61  
      * @param predicate UnaryPredicate against which the composite functions' output will be tested
 62  
      */
 63  28
     public CompositeUnaryPredicate(UnaryPredicate<? super A> predicate) {
 64  28
         if (null == predicate) {
 65  2
             throw new IllegalArgumentException("predicate must not be null");
 66  
         }
 67  26
         this.function = new CompositeUnaryFunction<A, Boolean>(new UnaryPredicateUnaryFunction<A>(predicate));
 68  26
     }
 69  
 
 70  
     /**
 71  
      * Create a new CompositeUnaryPredicate.
 72  
      * @param function delegate
 73  
      */
 74  66
     private CompositeUnaryPredicate(CompositeUnaryFunction<? super A, Boolean> function) {
 75  66
         this.function = function;
 76  66
     }
 77  
 
 78  
     // modifiers
 79  
     // ------------------------------------------------------------------------
 80  
     /**
 81  
      * Fluently obtain a CompositeUnaryPredicate that applies our predicate to the result of the preceding function.
 82  
      * @param preceding UnaryFunction
 83  
      * @return CompositeUnaryPredicate<P>
 84  
      */
 85  
     public <P> CompositeUnaryPredicate<P> of(UnaryFunction<? super P, ? extends A> preceding) {
 86  66
         return new CompositeUnaryPredicate<P>(function.of(preceding));
 87  
     }
 88  
 
 89  
     // predicate interface
 90  
     // ------------------------------------------------------------------------
 91  
     /**
 92  
      * {@inheritDoc}
 93  
      */
 94  
     public boolean test(A obj) {
 95  158
         return function.evaluate(obj);
 96  
     }
 97  
 
 98  
     /**
 99  
      * {@inheritDoc}
 100  
      */
 101  
     public boolean equals(Object that) {
 102  66
         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  58
         return null != that && function.equals(that.function);
 113  
     }
 114  
 
 115  
     /**
 116  
      * {@inheritDoc}
 117  
      */
 118  
     public int hashCode() {
 119  68
         int hash = "CompositeUnaryPredicate".hashCode();
 120  68
         hash <<= 2;
 121  68
         hash ^= function.hashCode();
 122  68
         return hash;
 123  
     }
 124  
 
 125  
     /**
 126  
      * {@inheritDoc}
 127  
      */
 128  
     public String toString() {
 129  60
         return "CompositeUnaryFunction<" + function + ">";
 130  
     }
 131  
 
 132  
 }