| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| UnaryFunction |
|
| 0.0;0 | ||||
| UnaryFunction$ComposedUnaryFunction |
|
| 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 | import static jaggregate.internal.ArgumentChecks.*; | |
| 9 | ||
| 10 | /** | |
| 11 | * A functor that accepts one argument. | |
| 12 | * | |
| 13 | * @param <A> a constraint on the allowable types for the functor's argument | |
| 14 | * @param <R> a constraint on the allowable types for the functor's return value | |
| 15 | * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a> | |
| 16 | * @version $Id: UnaryFunction.java,v 1.6 2008/10/03 19:01:23 pholser Exp $ | |
| 17 | */ | |
| 18 | public abstract class UnaryFunction<A, R> implements UnaryFunctor<A, R> { | |
| 19 | /** | |
| 20 | * Creates a new functor. | |
| 21 | */ | |
| 22 | 45 | protected UnaryFunction() { |
| 23 | // For subclasses. | |
| 24 | 45 | } |
| 25 | ||
| 26 | /** | |
| 27 | * Answers a unary functor that evaluates in the same manner as this functor | |
| 28 | * would if handed the result of evaluating a given unary functor with a given | |
| 29 | * argument as its own argument; effectively offering a <dfn>composition</dfn> | |
| 30 | * of the two functors. | |
| 31 | * | |
| 32 | * @param <T> a constraint on the allowable types of the argument to the composed | |
| 33 | * functor | |
| 34 | * @param other the functor to compose | |
| 35 | * @return a composed unary functor | |
| 36 | * @throws NullPointerException if {@code other} is {@code null} | |
| 37 | */ | |
| 38 | public final <T> UnaryFunction<T, R> compose( UnaryFunction<? super T, A> other ) { | |
| 39 | 3 | return new ComposedUnaryFunction<A, R, T>( this, other ); |
| 40 | } | |
| 41 | ||
| 42 | private static class ComposedUnaryFunction<A, R, T> extends UnaryFunction<T, R> { | |
| 43 | private final UnaryFunction<A, R> wrapper; | |
| 44 | private final UnaryFunction<? super T, A> composed; | |
| 45 | ||
| 46 | ComposedUnaryFunction( UnaryFunction<A, R> wrapper, | |
| 47 | 3 | UnaryFunction<? super T, A> composed ) { |
| 48 | ||
| 49 | 3 | ensureNotNull( composed, "'right-hand' operand" ); |
| 50 | ||
| 51 | 2 | this.wrapper = wrapper; |
| 52 | 2 | this.composed = composed; |
| 53 | 2 | } |
| 54 | ||
| 55 | public R evaluate( T argument ) { | |
| 56 | 3 | return wrapper.evaluate( composed.evaluate( argument ) ); |
| 57 | } | |
| 58 | } | |
| 59 | } |