BlockThunk.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;


/** Compose a block with a thunk to produce a new thunk whose output is the composition of the thunk with
the block.
	@param <V1> The type of the input to the block, the output of the provided thunk.
	@param <V2> The type of the output of this thunk instance.
*/
public final class BlockThunk<V1,V2> implements Thunk<V2> {
	private final Block<V1,V2> functor;
	private final Thunk<V1> constant;

	/** Create a new thunk whose output is the composition of <code>functor.eval(constant.eval())</code>.
		@param functor The functor which produces the final output.
		@param constant The thunk which produces the input to the functor.
	*/
	public BlockThunk(final Block<? super V1,? extends V2> functor, final Thunk<? super V1> constant) {
		this.functor = (Block<V1,V2>) functor;
		this.constant = (Thunk<V1>) constant;
	}

	@Override public V2 eval() throws Exception { return functor.eval(constant.eval()); }
}