Chain.java
/***************************************************************************
Copyright 2009 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.functor;
/** Composition of functions, implemented with blocks. Take two provided blocks
and produce a new block whose result is the composition of the functions represented by the
blocks.
@param <V1> The input type of this block.
@param <V2> The type of the intermediate result of the composition.
@param <V3> The output type of this block.
*/
public final class Chain<V1,V2,V3> implements Block<V1,V3> {
private final Block<? super V2,V3> b1;
private final Block<V1,? extends V2> b2;
/** Create a new block representing the composition of the two provided blocks.
<code>b2 compose b1</code> i.e. <code>b1.eval(b2.eval(...))</code>.
@param b1 The output block.
@param b2 The input block.
*/
public Chain(final Block<? super V2,V3> b1, final Block<V1,? extends V2> b2) {
this.b1 = b1;
this.b2 = b2;
}
@Override public V3 eval(final V1 v) throws Exception { return b1.eval(b2.eval(v)); }
}