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 org.apache.commons.functor.Function; 20 import org.apache.commons.functor.Predicate; 21 import org.apache.commons.functor.adapter.PredicateFunction; 22 import org.apache.commons.lang3.Validate; 23 24 /** 25 * A {@link Predicate Predicate} 26 * representing the composition of 27 * {@link Function Functions}, 28 * "chaining" the output of one to the input 29 * of another. For example, 30 * <pre>new CompositePredicate(p).of(f)</pre> 31 * {@link #test tests} to 32 * <code>p.test(f.evaluate(obj))</code>, and 33 * <pre>new CompositePredicate(p).of(f).of(g)</pre> 34 * {@link #test tests} to 35 * <code>p.test(f.evaluate(g.evaluate(obj)))</code>. 36 * @param <A> the predicate argument type. 37 * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $ 38 */ 39 public final class CompositePredicate<A> implements Predicate<A> { 40 // attributes 41 // ------------------------------------------------------------------------ 42 /** 43 * The adapted composite function. 44 */ 45 private final CompositeFunction<? super A, Boolean> function; 46 47 // constructor 48 // ------------------------------------------------------------------------ 49 /** 50 * Create a new CompositePredicate. 51 * @param predicate Predicate against which the composite functions' output will be tested 52 */ 53 public CompositePredicate(Predicate<? super A> predicate) { 54 this.function = 55 new CompositeFunction<A, Boolean>( 56 new PredicateFunction<A>(Validate.notNull(predicate, 57 "predicate must not be null"))); 58 } 59 60 /** 61 * Create a new CompositePredicate. 62 * @param function delegate 63 */ 64 private CompositePredicate(CompositeFunction<? super A, Boolean> function) { 65 this.function = function; 66 } 67 68 // modifiers 69 // ------------------------------------------------------------------------ 70 /** 71 * Fluently obtain a CompositePredicate that applies our predicate to the result of the preceding function. 72 * @param <P> the input function left argument and output predicate argument types 73 * @param preceding Function 74 * @return CompositePredicate<P> 75 */ 76 public <P> CompositePredicate<P> of(Function<? super P, ? extends A> preceding) { 77 return new CompositePredicate<P>(function.of(preceding)); 78 } 79 80 // predicate interface 81 // ------------------------------------------------------------------------ 82 /** 83 * {@inheritDoc} 84 */ 85 public boolean test(A obj) { 86 return function.evaluate(obj).booleanValue(); 87 } 88 89 /** 90 * {@inheritDoc} 91 */ 92 @Override 93 public boolean equals(Object obj) { 94 if (obj == this) { 95 return true; 96 } 97 if (!(obj instanceof CompositePredicate<?>)) { 98 return false; 99 } 100 CompositePredicate<?> that = (CompositePredicate<?>) obj; 101 return this.function.equals(that.function); 102 } 103 104 /** 105 * {@inheritDoc} 106 */ 107 @Override 108 public int hashCode() { 109 int hash = "CompositePredicate".hashCode(); 110 hash <<= 2; 111 hash ^= function.hashCode(); 112 return hash; 113 } 114 115 /** 116 * {@inheritDoc} 117 */ 118 @Override 119 public String toString() { 120 return "CompositeFunction<" + function + ">"; 121 } 122 123 }