se.l4.commons.io.IOFunction Maven / Gradle / Ivy
package se.l4.commons.io;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Objects;
import java.util.function.Function;
/**
* {@link Function} that can throw {@link IOException}.
*
* @author Andreas Holstenson
*
* @param
* @param
*/
@FunctionalInterface
public interface IOFunction
{
/**
* Apply this function to the given input.
*
* @param t
* input to apply to
* @return
* result
*/
R apply(T t)
throws IOException;
/**
* Compose the before function with this one. The before function will be
* applied first followed by this one.
*
* @param before
* the function to apply before this one
* @return
* composed function
*/
default IOFunction compose(IOFunction super V, ? extends T> before)
{
Objects.requireNonNull(before);
return t -> apply(before.apply(t));
}
/**
* Compose the before function with this one. The before function will be
* applied first followed by this one.
*
* @param before
* the function to apply before this one
* @return
* composed function
*/
default IOFunction compose(Function super V, ? extends T> before)
{
Objects.requireNonNull(before);
return t -> apply(before.apply(t));
}
/**
* Compose this function with another one. This function will be applied
* first followed by the after function.
*
* @param after
* the function to apply after this function
* @return
* composed function
*/
default IOFunction andThen(IOFunction super R, ? extends V> after)
{
Objects.requireNonNull(after);
return t -> after.apply(apply(t));
}
/**
* Compose this function with another one. This function will be applied
* first followed by the after function.
*
* @param after
* the function to apply after this function
* @return
* composed function
*/
default IOFunction andThen(Function super R, ? extends V> after)
{
Objects.requireNonNull(after);
return t -> after.apply(apply(t));
}
/**
* Turn this {@link IoFunction} into a regular {@link Function}. The
* regular function will throw {@link UncheckedIOException} if this
* function raises an {@link IOException}.
*
* @return
* function that invokes this one
*/
default Function toFunction()
{
return t -> {
try
{
return apply(t);
}
catch(IOException e)
{
throw new UncheckedIOException(e);
}
};
}
/**
* Adapt a {@link Function} into a {@link IOFunction}.
*
* @param func
* the function to adapt
* @return
* {@link IOFunction} that runs the given regular {@link Function}
*/
static IOFunction adapt(Function func)
{
return item -> func.apply(item);
}
}