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: 1365329 $ $Date: 2012-07-24 18:34:23 -0400 (Tue, 24 Jul 2012) $
28 */
29 public final class Composite {
30 // constructor - for beanish apis
31 // ------------------------------------------------------------------------
32 /**
33 * <p>{@code Composite} instances should NOT be constructed in
34 * standard programming. Instead, the methods of the class should be invoked
35 * statically.</p>
36 *
37 * <p>This constructor is public to permit tools that require a JavaBean
38 * instance to operate.</p>
39 */
40 public Composite() { }
41
42 /**
43 * Create a composite UnaryProcedure.
44 * @param <A> the procedure argument type.
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 <A> the function argument type.
55 * @param <T> the the procedure argument type and function returned value type.
56 * @param procedure UnaryProcedure to execute against output of <code>f</code>
57 * @param function UnaryFunction to apply
58 * @return CompositeUnaryProcedure<A>
59 */
60 public static <A, T> CompositeUnaryProcedure<A> procedure(UnaryProcedure<? super T> procedure,
61 UnaryFunction<? super A, ? extends T> function) {
62 return new CompositeUnaryProcedure<T>(procedure).of(function);
63 }
64
65 /**
66 * Create a composite UnaryPredicate.
67 * @param <A> the predicate argument type.
68 * @param pred UnaryPredicate to test the output of <code>f</code>
69 * @return CompositeUnaryPredicate<A>
70 */
71 public static <A> CompositeUnaryPredicate<A> predicate(UnaryPredicate<? super A> pred) {
72 return new CompositeUnaryPredicate<A>(pred);
73 }
74
75 /**
76 * Create a composite UnaryPredicate.
77 * @param <A> the function argument type.
78 * @param <T> the predicate argument type and the function returned value type.
79 * @param predicate UnaryPredicate to test the output of <code>f</code>
80 * @param function UnaryFunction to apply
81 * @return CompositeUnaryPredicate<A>
82 */
83 public static <A, T> CompositeUnaryPredicate<A> predicate(UnaryPredicate<? super T> predicate,
84 UnaryFunction<? super A, ? extends T> function) {
85 return new CompositeUnaryPredicate<T>(predicate).of(function);
86 }
87
88 /**
89 * Create a composite BinaryPredicate.
90 * @param <L> the output predicate left argument type.
91 * @param <R> the output predicate right argument type.
92 * @param <G> the input functions left argument type.
93 * @param <H> the input functions right argument type.
94 * @param p BinaryPredicate to test <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i>
95 * @param g left UnaryFunction
96 * @param h right UnaryFunction
97 * @return BinaryPredicate
98 */
99 public static <L, R, G, H> UnaryCompositeBinaryPredicate<L, R> predicate(
100 BinaryPredicate<? super G, ? super H> p, UnaryFunction<? super L, ? extends G> g,
101 UnaryFunction<? super R, ? extends H> h) {
102 return new UnaryCompositeBinaryPredicate<L, R>(p, g, h);
103 }
104
105 /**
106 * Create a composite UnaryFunction.
107 * @param <A> the function argument type.
108 * @param <T> the function returned value type.
109 * @param f UnaryFunction to apply to the output of <code>g</code>
110 * @return UnaryFunction
111 */
112 public static <A, T> CompositeUnaryFunction<A, T> function(UnaryFunction<? super A, ? extends T> f) {
113 return new CompositeUnaryFunction<A, T>(f);
114 }
115
116 /**
117 * Create a composite UnaryFunction.
118 * @param <A> the function argument type.
119 * @param <X> the function argument type.
120 * @param <T> the function returned value type.
121 * @param f UnaryFunction to apply to the output of <code>g</code>
122 * @param g UnaryFunction to apply first
123 * @return UnaryFunction
124 */
125 public static <A, X, T> CompositeUnaryFunction<A, T> function(UnaryFunction<? super X, ? extends T> f,
126 UnaryFunction<? super A, ? extends X> g) {
127 return new CompositeUnaryFunction<X, T>(f).of(g);
128 }
129
130 // /**
131 // * Chain a BinaryFunction to a UnaryFunction.
132 // * @param <L>
133 // * @param <R>
134 // * @param <X>
135 // * @param <T>
136 // * @param f UnaryFunction to apply to the output of <code>g</code>
137 // * @param g BinaryFunction to apply first
138 // * @return BinaryFunction<L, R, T>
139 // */
140 // public static <L, R, X, T> BinaryFunction<L, R, T> function(UnaryFunction<? super X, ? extends T> f,
141 // BinaryFunction<? super L,
142 // ? super R, ? extends X> g) {
143 // return new CompositeUnaryFunction<X, T>(f).of(g);
144 // }
145
146 /**
147 * Create a composite<UnaryFunction> BinaryFunction.
148 * @param <L> the output predicate left argument type.
149 * @param <R> the output predicate right argument type.
150 * @param <G> the input functions left argument type.
151 * @param <H> the input functions right argument type.
152 * @param <T> the function returned value type.
153 * @param f BinaryFunction to apply to <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i>
154 * @param g left UnaryFunction
155 * @param h right UnaryFunction
156 * @return BinaryFunction
157 */
158 public static <L, R, G, H, T> UnaryCompositeBinaryFunction<L, R, T> function(
159 BinaryFunction<? super G, ? super H, ? extends T> f, UnaryFunction<? super L, ? extends G> g,
160 UnaryFunction<? super R, ? extends H> h) {
161 return new UnaryCompositeBinaryFunction<L, R, T>(f, g, h);
162 }
163
164 /**
165 * Create a composite<BinaryFunction> BinaryFunction.
166 * @param <L> the output predicate left argument type.
167 * @param <R> the output predicate right argument type.
168 * @param <G> the input functions left argument type.
169 * @param <H> the input functions right argument type.
170 * @param <T> the function returned value type.
171 * @param f BinaryFunction to apply to <i>output(</i><code>f</code><i>), output(</i><code>g</code><i>)</i>
172 * @param g left BinaryFunction
173 * @param h right BinaryFunction
174 * @return BinaryFunction
175 */
176 public static <L, R, G, H, T> BinaryCompositeBinaryFunction<L, R, T> function(
177 BinaryFunction<? super G, ? super H, ? extends T> f, BinaryFunction<? super L, ? super R, ? extends G> g,
178 BinaryFunction<? super L, ? super R, ? extends H> h) {
179 return new BinaryCompositeBinaryFunction<L, R, T>(f, g, h);
180 }
181 }