net.lenni0451.commons.Sneaky Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of main Show documentation
Show all versions of main Show documentation
A java library with many useful functions and classes
package net.lenni0451.commons;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import java.util.function.Supplier;
@UtilityClass
public class Sneaky {
/**
* Fake throw an exception to have to declare it in the method signature.
* Can be used to suppress warnings when using {@link #sneak}
*
* @param exceptionType The type of the exception to fake throw
* @param The type of the exception
* @throws T The exception
*/
public static void fake(final Class exceptionType) throws T {
}
/**
* Throw a throwable without having to declare it in the method signature.
*
* @param t The throwable to throw
*/
@SneakyThrows
public static void sneak(final Throwable t) {
throw t;
}
/**
* Run a runnable without having to declare the throwable in the method signature.
*
* @param runnable The runnable to run
*/
@SneakyThrows
public static void sneak(final SneakyRunnable runnable) {
runnable.run();
}
/**
* Get a value from a supplier without having to declare the throwable in the method signature.
*
* @param supplier The supplier to get the value from
* @param The type of the value
* @return The value
*/
@SneakyThrows
public static O sneak(final SneakySupplier supplier) {
return supplier.get();
}
/**
* Convert a {@link SneakyRunnable} to a {@link Runnable}.
*
* @param runnable The sneaky runnable to convert
* @return The converted runnable
*/
public Runnable toRunnable(final SneakyRunnable runnable) {
return () -> sneak(runnable);
}
/**
* Convert a {@link SneakySupplier} to a {@link Supplier}.
*
* @param supplier The sneaky supplier to convert
* @param The type of the value
* @return The converted supplier
*/
public O toSupplier(final SneakySupplier supplier) {
return sneak(supplier);
}
@FunctionalInterface
public interface SneakyRunnable {
void run() throws Throwable;
}
@FunctionalInterface
public interface SneakySupplier {
T get() throws Throwable;
}
}