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 import org.apache.commons.lang3.Validate; 25 26 /** 27 * A {@link UnaryPredicate UnaryPredicate} 28 * representing the composition of 29 * {@link UnaryFunction UnaryFunctions}, 30 * "chaining" the output of one to the input 31 * of another. For example, 32 * <pre>new CompositeUnaryPredicate(p).of(f)</pre> 33 * {@link #test tests} to 34 * <code>p.test(f.evaluate(obj))</code>, and 35 * <pre>new CompositeUnaryPredicate(p).of(f).of(g)</pre> 36 * {@link #test tests} to 37 * <code>p.test(f.evaluate(g.evaluate(obj)))</code>. 38 * <p> 39 * Note that although this class implements 40 * {@link Serializable}, a given instance will 41 * only be truly <code>Serializable</code> if all the 42 * underlying functors are. Attempts to serialize 43 * an instance whose delegates are not all 44 * <code>Serializable</code> will result in an exception. 45 * </p> 46 * @param <A> the predicate argument type. 47 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $ 48 */ 49 public final class CompositeUnaryPredicate<A> implements UnaryPredicate<A>, Serializable { 50 /** 51 * serialVersionUID declaration. 52 */ 53 private static final long serialVersionUID = 4880363949059265252L; 54 // attributes 55 // ------------------------------------------------------------------------ 56 /** 57 * The adapted composite function. 58 */ 59 private final CompositeUnaryFunction<? super A, Boolean> function; 60 61 // constructor 62 // ------------------------------------------------------------------------ 63 /** 64 * Create a new CompositeUnaryPredicate. 65 * @param predicate UnaryPredicate against which the composite functions' output will be tested 66 */ 67 public CompositeUnaryPredicate(UnaryPredicate<? super A> predicate) { 68 this.function = 69 new CompositeUnaryFunction<A, Boolean>( 70 new UnaryPredicateUnaryFunction<A>(Validate.notNull(predicate, 71 "predicate must not be null"))); 72 } 73 74 /** 75 * Create a new CompositeUnaryPredicate. 76 * @param function delegate 77 */ 78 private CompositeUnaryPredicate(CompositeUnaryFunction<? super A, Boolean> function) { 79 this.function = function; 80 } 81 82 // modifiers 83 // ------------------------------------------------------------------------ 84 /** 85 * Fluently obtain a CompositeUnaryPredicate that applies our predicate to the result of the preceding function. 86 * @param <P> the input function left argument and output predicate argument types 87 * @param preceding UnaryFunction 88 * @return CompositeUnaryPredicate<P> 89 */ 90 public <P> CompositeUnaryPredicate<P> of(UnaryFunction<? super P, ? extends A> preceding) { 91 return new CompositeUnaryPredicate<P>(function.of(preceding)); 92 } 93 94 // predicate interface 95 // ------------------------------------------------------------------------ 96 /** 97 * {@inheritDoc} 98 */ 99 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 }