All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.l4.commons.io.IOSupplier Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package se.l4.commons.io;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.Supplier;

/**
 * {@link Supplier} of a result that can throw a {@link IOException}.
 */
@FunctionalInterface
public interface IOSupplier
{
	/**
	 * Gets a result.
	 *
	 * @return
	 *   a result
	 * @throws IOException
	 *   if I/O error occurs
	 */
	T get()
		throws IOException;


	/**
	 * Turns this {@link IOSupplier} into a normal {@link Supplier}. The
	 * normal supplier will throw {@link UncheckedIOException} if an
	 * {@link IOException} is raised.
	 *
	 * @return
	 *   supplier
	 */
	default Supplier toSupplier()
	{
		return () -> {
			try
			{
				return get();
			}
			catch(IOException e)
			{
				throw new UncheckedIOException(e);
			}
		};
	}

	/**
	 * Adapt a regular {@link Supplier} into an {@link IOSupplier}.
	 *
	 * @return
	 *   {@link IOSupplier} that delegates to the given supplier
	 */
	static  IOSupplier adapt(Supplier supplier)
	{
		return () -> supplier.get();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy