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 org.apache.commons.functor.BinaryFunction; 020import org.apache.commons.functor.BinaryPredicate; 021import org.apache.commons.functor.Function; 022import org.apache.commons.functor.Predicate; 023import org.apache.commons.functor.Procedure; 024 025/** 026 * Utility/fluent methods for creating composite functors. 027 * @version $Revision: 1539879 $ $Date: 2013-11-08 01:23:22 +0100 (Fr, 08 Nov 2013) $ 028 */ 029public final class Composite { 030 031 /** 032 * Hidden constructor as this only is a helper class with static methods. 033 */ 034 private Composite() { 035 } 036 037 /** 038 * Create a composite Procedure. 039 * @param <A> the procedure argument type. 040 * @param procedure Procedure to execute against output of <code>f</code> 041 * @return CompositeProcedure<A> 042 */ 043 public static <A> CompositeProcedure<A> procedure(Procedure<? super A> procedure) { 044 return new CompositeProcedure<A>(procedure); 045 } 046 047 /** 048 * Create a composite Procedure. 049 * @param <A> the function argument type. 050 * @param <T> the the procedure argument type and function returned value type. 051 * @param procedure Procedure to execute against output of <code>f</code> 052 * @param function Function to apply 053 * @return CompositeProcedure<A> 054 */ 055 public static <A, T> CompositeProcedure<A> procedure(Procedure<? super T> procedure, 056 Function<? super A, ? extends T> function) { 057 return new CompositeProcedure<T>(procedure).of(function); 058 } 059 060 /** 061 * Create a composite Predicate. 062 * @param <A> the predicate argument type. 063 * @param pred Predicate to test the output of <code>f</code> 064 * @return CompositePredicate<A> 065 */ 066 public static <A> CompositePredicate<A> predicate(Predicate<? super A> pred) { 067 return new CompositePredicate<A>(pred); 068 } 069 070 /** 071 * Create a composite Predicate. 072 * @param <A> the function argument type. 073 * @param <T> the predicate argument type and the function returned value type. 074 * @param predicate Predicate to test the output of <code>f</code> 075 * @param function Function to apply 076 * @return CompositePredicate<A> 077 */ 078 public static <A, T> CompositePredicate<A> predicate(Predicate<? super T> predicate, 079 Function<? super A, ? extends T> function) { 080 return new CompositePredicate<T>(predicate).of(function); 081 } 082 083 /** 084 * Create a composite BinaryPredicate. 085 * @param <L> the output predicate left argument type. 086 * @param <R> the output predicate right argument type. 087 * @param <G> the input functions left argument type. 088 * @param <H> the input functions right argument type. 089 * @param p BinaryPredicate to test <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i> 090 * @param g left Function 091 * @param h right Function 092 * @return BinaryPredicate 093 */ 094 public static <L, R, G, H> CompositeBinaryPredicate<L, R> predicate( 095 BinaryPredicate<? super G, ? super H> p, Function<? super L, ? extends G> g, 096 Function<? super R, ? extends H> h) { 097 return new CompositeBinaryPredicate<L, R>(p, g, h); 098 } 099 100 /** 101 * Create a composite Function. 102 * @param <A> the function argument type. 103 * @param <T> the function returned value type. 104 * @param f Function to apply to the output of <code>g</code> 105 * @return Function 106 */ 107 public static <A, T> CompositeFunction<A, T> function(Function<? super A, ? extends T> f) { 108 return new CompositeFunction<A, T>(f); 109 } 110 111 /** 112 * Create a composite Function. 113 * @param <A> the function argument type. 114 * @param <X> the function argument type. 115 * @param <T> the function returned value type. 116 * @param f Function to apply to the output of <code>g</code> 117 * @param g Function to apply first 118 * @return Function 119 */ 120 public static <A, X, T> CompositeFunction<A, T> function(Function<? super X, ? extends T> f, 121 Function<? super A, ? extends X> g) { 122 return new CompositeFunction<X, T>(f).of(g); 123 } 124 125// /** 126// * Chain a BinaryFunction to a Function. 127// * @param <L> 128// * @param <R> 129// * @param <X> 130// * @param <T> 131// * @param f Function to apply to the output of <code>g</code> 132// * @param g BinaryFunction to apply first 133// * @return BinaryFunction<L, R, T> 134// */ 135// public static <L, R, X, T> BinaryFunction<L, R, T> function(Function<? super X, ? extends T> f, 136// BinaryFunction<? super L, 137// ? super R, ? extends X> g) { 138// return new CompositeFunction<X, T>(f).of(g); 139// } 140 141 /** 142 * Create a composite<Function> BinaryFunction. 143 * @param <L> the output predicate left argument type. 144 * @param <R> the output predicate right argument type. 145 * @param <G> the input functions left argument type. 146 * @param <H> the input functions right argument type. 147 * @param <T> the function returned value type. 148 * @param f BinaryFunction to apply to <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i> 149 * @param g left Function 150 * @param h right Function 151 * @return BinaryFunction 152 */ 153 public static <L, R, G, H, T> CompositeBinaryFunction<L, R, T> function( 154 BinaryFunction<? super G, ? super H, ? extends T> f, Function<? super L, ? extends G> g, 155 Function<? super R, ? extends H> h) { 156 return new CompositeBinaryFunction<L, R, T>(f, g, h); 157 } 158 159 /** 160 * Create a composite<BinaryFunction> BinaryFunction. 161 * @param <L> the output predicate left argument type. 162 * @param <R> the output predicate right argument type. 163 * @param <G> the input functions left argument type. 164 * @param <H> the input functions right argument type. 165 * @param <T> the function returned value type. 166 * @param f BinaryFunction to apply to <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i> 167 * @param g left BinaryFunction 168 * @param h right BinaryFunction 169 * @return BinaryFunction 170 */ 171 public static <L, R, G, H, T> BinaryCompositeBinaryFunction<L, R, T> function( 172 BinaryFunction<? super G, ? super H, ? extends T> f, BinaryFunction<? super L, ? super R, ? extends G> g, 173 BinaryFunction<? super L, ? super R, ? extends H> h) { 174 return new BinaryCompositeBinaryFunction<L, R, T>(f, g, h); 175 } 176}