Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BinaryFunction |
|
| 0.0;0 | ||||
BinaryFunction$FirstBoundBinaryFunction |
|
| 0.0;0 | ||||
BinaryFunction$SecondBoundBinaryFunction |
|
| 0.0;0 |
1 | /* | |
2 | Copyright 2004-2008 Paul R. Holser, Jr. All rights reserved. | |
3 | Licensed under the Academic Free License version 3.0 | |
4 | */ | |
5 | ||
6 | package jaggregate; | |
7 | ||
8 | /** | |
9 | * A functor that accepts two arguments. | |
10 | * | |
11 | * @param <A1> a constraint on the allowable types for the functor's first argument | |
12 | * @param <A2> a constraint on the allowable types for the functor's second argument | |
13 | * @param <R> a constraint on the allowable types for the functor's return value | |
14 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
15 | * @version $Id: BinaryFunction.java,v 1.5 2008/10/03 19:01:23 pholser Exp $ | |
16 | */ | |
17 | public abstract class BinaryFunction<A1, A2, R> implements BinaryFunctor<A1, A2, R> { | |
18 | /** | |
19 | * Creates a new functor. | |
20 | */ | |
21 | 6174 | protected BinaryFunction() { |
22 | // For subclasses. | |
23 | 6174 | } |
24 | ||
25 | /** | |
26 | * Answers a functor of one argument that evaluates in the same manner as this | |
27 | * functor would if evaluated using a fixed first argument and another argument. | |
28 | * | |
29 | * @param boundArgument the fixed argument used as the first argument to this | |
30 | * functor's {@link #evaluate(Object,Object) evaluate} method | |
31 | * @return a functor that accepts one argument, of the same type as this functor's | |
32 | * second argument type, that behaves as this functor would if always evaluated | |
33 | * using {@code boundArgument} as the first argument | |
34 | */ | |
35 | public final UnaryFunction<A2, R> bindFirst( A1 boundArgument ) { | |
36 | 1 | return new FirstBoundBinaryFunction<A1, A2, R>( this, boundArgument ); |
37 | } | |
38 | ||
39 | /** | |
40 | * Answers a functor of one argument that evaluates in the same manner as this | |
41 | * functor would if evaluated using a fixed second argument and another argument. | |
42 | * | |
43 | * @param boundArgument the fixed argument used as the second argument to this | |
44 | * functor's {@link #evaluate(Object,Object) evaluate} method | |
45 | * @return a functor that accepts one argument, of the same type as this functor's | |
46 | * first argument type, that behaves as this functor would if always evaluated | |
47 | * using {@code boundArgument} as the second argument | |
48 | */ | |
49 | public final UnaryFunction<A1, R> bindSecond( A2 boundArgument ) { | |
50 | 35 | return new SecondBoundBinaryFunction<A1, A2, R>( this, boundArgument ); |
51 | } | |
52 | ||
53 | private static class FirstBoundBinaryFunction<A1, A2, R> | |
54 | extends UnaryFunction<A2, R> { | |
55 | ||
56 | private final BinaryFunctor<A1, A2, R> wrapped; | |
57 | private final A1 boundArgument; | |
58 | ||
59 | 1 | FirstBoundBinaryFunction( BinaryFunctor<A1, A2, R> wrapped, A1 boundArgument ) { |
60 | 1 | this.wrapped = wrapped; |
61 | 1 | this.boundArgument = boundArgument; |
62 | 1 | } |
63 | ||
64 | public R evaluate( A2 argument ) { | |
65 | 2 | return wrapped.evaluate( boundArgument, argument ); |
66 | } | |
67 | } | |
68 | ||
69 | private static class SecondBoundBinaryFunction<A1, A2, R> | |
70 | extends UnaryFunction<A1, R> { | |
71 | ||
72 | private final BinaryFunctor<A1, A2, R> wrapped; | |
73 | private final A2 boundArgument; | |
74 | ||
75 | 35 | SecondBoundBinaryFunction( BinaryFunctor<A1, A2, R> wrapped, A2 boundArgument ) { |
76 | 35 | this.wrapped = wrapped; |
77 | 35 | this.boundArgument = boundArgument; |
78 | 35 | } |
79 | ||
80 | public R evaluate( A1 argument ) { | |
81 | 36 | return wrapped.evaluate( argument, boundArgument ); |
82 | } | |
83 | } | |
84 | } |