/*
 * Copyright 2006 Takahiro Nakamura.
 *
 * 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 woolpack.fn;


/**
 * ふたつの委譲先を接続する{@link Fn}です。
 * 最初の委譲先の返却値を引数してに次の委譲先を実行しその結果を返します。
 * 
 * @author nakamura
 *
 * @param <A>
 * @param <B>
 * @param <C>
 */
public class JoinFn<A, B, C> implements Fn<A, C> {
	private Fn<? super A, ? extends B> firstFn;
	private Fn<? super B, ? extends C> secondFn;
	
	/**
	 * @param firstFn 最初の委譲先。
	 * @param secondFn ふたつめの委譲先。
	 */
	public JoinFn(
			final Fn<? super A, ? extends B> firstFn,
			final Fn<? super B, ? extends C> secondFn) {
		this.firstFn = firstFn;
		this.secondFn = secondFn;
	}

	public C exec(final A a) {
		return secondFn.exec(firstFn.exec(a));
	}
	
	public Fn<? super A, ? extends B> getFirstFn() {
		return firstFn;
	}
	public void setFirstFn(final Fn<? super A, ? extends B> firstFn) {
		this.firstFn = firstFn;
	}
	public Fn<? super B, ? extends C> getSecondFn() {
		return secondFn;
	}
	public void setSecondFn(final Fn<? super B, ? extends C> secondFn) {
		this.secondFn = secondFn;
	}
}
