/*
 * 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 <C>
 * @param <R>
 */
public class IfFn<C, R> implements Fn<C, R> {
	private Fn<? super C, ?> ifFn;
	private Fn<? super C, ? extends R> trueFn;
	private Fn<? super C, ? extends R> falseFn;
	
	/**
	 * @param ifFn 評価用の委譲先。
	 * @param trueFn 評価結果が{@link Boolean#TRUE}の場合の委譲先。
	 * @param falseFn 評価結果が{@link Boolean#TRUE}でない場合の委譲先。
	 */
	public IfFn(
			final Fn<? super C, ?> ifFn,
			final Fn<? super C, ? extends R> trueFn,
			final Fn<? super C, ? extends R> falseFn) {
		this.ifFn = ifFn;
		this.trueFn = trueFn;
		this.falseFn = falseFn;
	}

	public R exec(final C c) {
		final Object o = ifFn.exec(c);
		if (Boolean.TRUE.equals(o)) {
			return trueFn.exec(c);
		} else {
			return falseFn.exec(c);
		}
	}

	public Fn<? super C, ? extends R> getFalseFn() {
		return falseFn;
	}
	public Fn<? super C, ?> getIfFn() {
		return ifFn;
	}
	public Fn<? super C, ? extends R> getTrueFn() {
		return trueFn;
	}
	public void setFalseFn(final Fn<? super C, ? extends R> falseFn) {
		this.falseFn = falseFn;
	}
	public void setIfFn(final Fn<? super C, ?> ifFn) {
		this.ifFn = ifFn;
	}
	public void setTrueFn(final Fn<? super C, ? extends R> trueFn) {
		this.trueFn = trueFn;
	}
}
