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.BinaryFunction; 20 import org.apache.commons.functor.BinaryPredicate; 21 import org.apache.commons.functor.UnaryFunction; 22 import org.apache.commons.functor.UnaryPredicate; 23 import org.apache.commons.functor.UnaryProcedure; 24 25 /** 26 * Utility/fluent methods for creating composite functors. 27 * @version $Revision: 1166365 $ $Date: 2011-09-07 22:06:50 +0200 (Wed, 07 Sep 2011) $ 28 * @author Rodney Waldhoff 29 */ 30 public final class Composite { 31 // constructor - for beanish apis 32 // ------------------------------------------------------------------------ 33 /** 34 * <p>{@code Composite} instances should NOT be constructed in 35 * standard programming. Instead, the methods of the class should be invoked 36 * statically.</p> 37 * 38 * <p>This constructor is public to permit tools that require a JavaBean 39 * instance to operate.</p> 40 */ 41 public Composite() { } 42 43 /** 44 * Create a composite UnaryProcedure. 45 * @param procedure UnaryProcedure to execute against output of <code>f</code> 46 * @return CompositeUnaryProcedure<A> 47 */ 48 public static <A> CompositeUnaryProcedure<A> procedure(UnaryProcedure<? super A> procedure) { 49 return new CompositeUnaryProcedure<A>(procedure); 50 } 51 52 /** 53 * Create a composite UnaryProcedure. 54 * @param procedure UnaryProcedure to execute against output of <code>f</code> 55 * @param function UnaryFunction to apply 56 * @return CompositeUnaryProcedure<A> 57 */ 58 public static <A, T> CompositeUnaryProcedure<A> procedure(UnaryProcedure<? super T> procedure, 59 UnaryFunction<? super A, ? extends T> function) { 60 return new CompositeUnaryProcedure<T>(procedure).of(function); 61 } 62 63 /** 64 * Create a composite UnaryPredicate. 65 * @param pred UnaryPredicate to test the output of <code>f</code> 66 * @return CompositeUnaryPredicate<A> 67 */ 68 public static <A> CompositeUnaryPredicate<A> predicate(UnaryPredicate<? super A> pred) { 69 return new CompositeUnaryPredicate<A>(pred); 70 } 71 72 /** 73 * Create a composite UnaryPredicate. 74 * @param predicate UnaryPredicate to test the output of <code>f</code> 75 * @param function UnaryFunction to apply 76 * @return CompositeUnaryPredicate<A> 77 */ 78 public static <A, T> CompositeUnaryPredicate<A> predicate(UnaryPredicate<? super T> predicate, 79 UnaryFunction<? super A, ? extends T> function) { 80 return new CompositeUnaryPredicate<T>(predicate).of(function); 81 } 82 83 /** 84 * Create a composite BinaryPredicate. 85 * @param p BinaryPredicate to test <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i> 86 * @param g left UnaryFunction 87 * @param h right UnaryFunction 88 * @return BinaryPredicate 89 */ 90 public static <L, R, G, H> UnaryCompositeBinaryPredicate<L, R> predicate( 91 BinaryPredicate<? super G, ? super H> p, UnaryFunction<? super L, ? extends G> g, 92 UnaryFunction<? super R, ? extends H> h) { 93 return new UnaryCompositeBinaryPredicate<L, R>(p, g, h); 94 } 95 96 /** 97 * Create a composite UnaryFunction. 98 * @param f UnaryFunction to apply to the output of <code>g</code> 99 * @return UnaryFunction 100 */ 101 public static <A, T> CompositeUnaryFunction<A, T> function(UnaryFunction<? super A, ? extends T> f) { 102 return new CompositeUnaryFunction<A, T>(f); 103 } 104 105 /** 106 * Create a composite UnaryFunction. 107 * @param f UnaryFunction to apply to the output of <code>g</code> 108 * @param g UnaryFunction to apply first 109 * @return UnaryFunction 110 */ 111 public static <A, X, T> CompositeUnaryFunction<A, T> function(UnaryFunction<? super X, ? extends T> f, 112 UnaryFunction<? super A, ? extends X> g) { 113 return new CompositeUnaryFunction<X, T>(f).of(g); 114 } 115 116 // /** 117 // * Chain a BinaryFunction to a UnaryFunction. 118 // * @param <L> 119 // * @param <R> 120 // * @param <X> 121 // * @param <T> 122 // * @param f UnaryFunction to apply to the output of <code>g</code> 123 // * @param g BinaryFunction to apply first 124 // * @return BinaryFunction<L, R, T> 125 // */ 126 // public static <L, R, X, T> BinaryFunction<L, R, T> function(UnaryFunction<? super X, ? extends T> f, 127 // BinaryFunction<? super L, 128 // ? super R, ? extends X> g) { 129 // return new CompositeUnaryFunction<X, T>(f).of(g); 130 // } 131 132 /** 133 * Create a composite<UnaryFunction> BinaryFunction. 134 * @param f BinaryFunction to apply to <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i> 135 * @param g left UnaryFunction 136 * @param h right UnaryFunction 137 * @return BinaryFunction 138 */ 139 public static <L, R, G, H, T> UnaryCompositeBinaryFunction<L, R, T> function( 140 BinaryFunction<? super G, ? super H, ? extends T> f, UnaryFunction<? super L, ? extends G> g, 141 UnaryFunction<? super R, ? extends H> h) { 142 return new UnaryCompositeBinaryFunction<L, R, T>(f, g, h); 143 } 144 145 /** 146 * Create a composite<BinaryFunction> BinaryFunction. 147 * @param f BinaryFunction to apply to <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i> 148 * @param g left BinaryFunction 149 * @param h right BinaryFunction 150 * @return BinaryFunction 151 */ 152 public static <L, R, G, H, T> BinaryCompositeBinaryFunction<L, R, T> function( 153 BinaryFunction<? super G, ? super H, ? extends T> f, BinaryFunction<? super L, ? super R, ? extends G> g, 154 BinaryFunction<? super L, ? super R, ? extends H> h) { 155 return new BinaryCompositeBinaryFunction<L, R, T>(f, g, h); 156 } 157 }