Makes exception types generic in ThrowableUtil

This commit is contained in:
Marc Baloup 2023-03-11 12:16:09 +01:00
parent df46026457
commit f1ef4e1927
3 changed files with 18 additions and 18 deletions

View File

@ -10,7 +10,7 @@ import java.lang.reflect.Modifier;
public final class ReflectField<T> extends ReflectMember<T, String, Field, NoSuchFieldException> {
/* Those fields are used to modify the value of a static variable. Depending of the current Java version,
* one of them whill be used for this purpose.
* one of them will be used for this purpose.
*/
private static sun.misc.Unsafe sunMiscUnsafeInstance;
private static Field modifiersFieldInFieldClass;

View File

@ -6,35 +6,36 @@ import fr.pandacube.lib.util.ThrowableUtil.SupplierException;
/**
* Represents a lazy loaded value.
*
* <p>
* The value will be computed using the Supplier provided in the
* constructor, only the first time the {@link #get()} method is
* called.
*
* @param <T> the type of the enclosed value.
* @param <E> the exception type
*/
public class LazyOrException<T> implements SupplierException<T> {
public class LazyOrException<T, E extends Exception> implements SupplierException<T, E> {
private T cachedValue;
private final SupplierException<T> supplier;
private final SupplierException<T, E> supplier;
private boolean cached = false;
/**
* Create a lazy value loader that will call the provided supplier to get the value.
* @param s the supplier from which the value is fetched.
*/
public LazyOrException(SupplierException<T> s) {
public LazyOrException(SupplierException<T, E> s) {
supplier = Objects.requireNonNull(s);
}
/**
* Get the wrapped value, from cache or from the provider if it is not yet cached.
*
* <p>
* If the provider throws an exception, it will be redirected to the caller as is, and no value will be cached
* (the next call to this method will execute the supplier again).
*/
@Override
public synchronized T get() throws Exception {
public synchronized T get() throws E {
if (!cached)
set(supplier.get());
return cachedValue;

View File

@ -5,7 +5,6 @@ import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.function.BiConsumer;
/**
* Utility class to easily manipulate {@link Throwable}s.
@ -43,7 +42,7 @@ public class ThrowableUtil {
* @throws RuntimeException if the provided {@link SupplierException} throws a checked exception.
* @param <T> the type of the returned object
*/
public static <T> T wrapEx(SupplierException<T> supp) {
public static <T> T wrapEx(SupplierException<T, Exception> supp) {
try {
return supp.get();
} catch (Exception e) {
@ -56,7 +55,7 @@ public class ThrowableUtil {
* @param run the {@link RunnableException} to run.
* @throws RuntimeException if the provided {@link RunnableException} throws a checked exception.
*/
public static void wrapEx(RunnableException run) {
public static void wrapEx(RunnableException<Exception> run) {
try {
run.run();
} catch (Exception e) {
@ -74,7 +73,7 @@ public class ThrowableUtil {
* @throws RuntimeException if the provided {@link SupplierException} throws a checked exception.
* @param <T> the type of the returned object
*/
public static <T> T wrapReflectEx(SupplierException<T> supp) {
public static <T> T wrapReflectEx(SupplierException<T, Exception> supp) {
try {
return supp.get();
} catch (Exception e) {
@ -88,7 +87,7 @@ public class ThrowableUtil {
* @param run the {@link RunnableException} to run.
* @throws RuntimeException if the provided {@link RunnableException} throws a checked exception.
*/
public static void wrapReflectEx(RunnableException run) {
public static void wrapReflectEx(RunnableException<Exception> run) {
try {
run.run();
} catch (Exception e) {
@ -152,13 +151,13 @@ public class ThrowableUtil {
* A supplier that can possibly throw a checked exception.
*/
@FunctionalInterface
public interface SupplierException<T> { // TODO make exception type generic
public interface SupplierException<T, E extends Exception> {
/**
* Gets a result.
* @return a result.
* @throws Exception if implementation failed to run.
* @throws E if implementation failed to run.
*/
T get() throws Exception;
T get() throws E;
}
@ -167,12 +166,12 @@ public class ThrowableUtil {
* A runnable that can possibly throw a checked exception.
*/
@FunctionalInterface
public interface RunnableException { // TODO make exception type generic
public interface RunnableException<E extends Exception> {
/**
* Run any code implemented.
* @throws Exception if implementation failed to run.
* @throws E if implementation failed to run.
*/
void run() throws Exception;
void run() throws E;
}