package org.eclipse.collections.impl.block.function;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.predicate.Predicate;
public class IfFunction<T, V> implements Function<T, V>
{
private static final long serialVersionUID = 1L;
private final Predicate<? super T> predicate;
private final Function<? super T, ? extends V> function;
private final Function<? super T, ? extends V> elseFunction;
public IfFunction(
Predicate<? super T> newPredicate,
Function<? super T, ? extends V> function)
{
this(newPredicate, function, null);
}
public IfFunction(
Predicate<? super T> predicate,
Function<? super T, ? extends V> function,
Function<? super T, ? extends V> elseFunction)
{
this.predicate = predicate;
this.function = function;
this.elseFunction = elseFunction;
}
@Override
public V valueOf(T object)
{
if (this.predicate.accept(object))
{
return this.function.valueOf(object);
}
if (this.elseFunction != null)
{
return this.elseFunction.valueOf(object);
}
return null;
}
@Override
public String toString()
{
return "new IfFunction(" + this.predicate + ", " + this.function + ", " + this.elseFunction + ')';
}
}