Makes exception types generic in ThrowableUtil
This commit is contained in:
parent
df46026457
commit
f1ef4e1927
@ -10,7 +10,7 @@ import java.lang.reflect.Modifier;
|
|||||||
public final class ReflectField<T> extends ReflectMember<T, String, Field, NoSuchFieldException> {
|
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,
|
/* 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 sun.misc.Unsafe sunMiscUnsafeInstance;
|
||||||
private static Field modifiersFieldInFieldClass;
|
private static Field modifiersFieldInFieldClass;
|
||||||
|
@ -6,35 +6,36 @@ import fr.pandacube.lib.util.ThrowableUtil.SupplierException;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a lazy loaded value.
|
* Represents a lazy loaded value.
|
||||||
*
|
* <p>
|
||||||
* The value will be computed using the Supplier provided in the
|
* The value will be computed using the Supplier provided in the
|
||||||
* constructor, only the first time the {@link #get()} method is
|
* constructor, only the first time the {@link #get()} method is
|
||||||
* called.
|
* called.
|
||||||
*
|
*
|
||||||
* @param <T> the type of the enclosed value.
|
* @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 T cachedValue;
|
||||||
private final SupplierException<T> supplier;
|
private final SupplierException<T, E> supplier;
|
||||||
private boolean cached = false;
|
private boolean cached = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a lazy value loader that will call the provided supplier to get the value.
|
* 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.
|
* @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);
|
supplier = Objects.requireNonNull(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the wrapped value, from cache or from the provider if it is not yet cached.
|
* 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
|
* 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).
|
* (the next call to this method will execute the supplier again).
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public synchronized T get() throws Exception {
|
public synchronized T get() throws E {
|
||||||
if (!cached)
|
if (!cached)
|
||||||
set(supplier.get());
|
set(supplier.get());
|
||||||
return cachedValue;
|
return cachedValue;
|
||||||
|
@ -5,7 +5,6 @@ import java.io.IOException;
|
|||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.function.BiConsumer;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class to easily manipulate {@link Throwable}s.
|
* 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.
|
* @throws RuntimeException if the provided {@link SupplierException} throws a checked exception.
|
||||||
* @param <T> the type of the returned object
|
* @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 {
|
try {
|
||||||
return supp.get();
|
return supp.get();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -56,7 +55,7 @@ public class ThrowableUtil {
|
|||||||
* @param run the {@link RunnableException} to run.
|
* @param run the {@link RunnableException} to run.
|
||||||
* @throws RuntimeException if the provided {@link RunnableException} throws a checked exception.
|
* @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 {
|
try {
|
||||||
run.run();
|
run.run();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -74,7 +73,7 @@ public class ThrowableUtil {
|
|||||||
* @throws RuntimeException if the provided {@link SupplierException} throws a checked exception.
|
* @throws RuntimeException if the provided {@link SupplierException} throws a checked exception.
|
||||||
* @param <T> the type of the returned object
|
* @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 {
|
try {
|
||||||
return supp.get();
|
return supp.get();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -88,7 +87,7 @@ public class ThrowableUtil {
|
|||||||
* @param run the {@link RunnableException} to run.
|
* @param run the {@link RunnableException} to run.
|
||||||
* @throws RuntimeException if the provided {@link RunnableException} throws a checked exception.
|
* @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 {
|
try {
|
||||||
run.run();
|
run.run();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@ -152,13 +151,13 @@ public class ThrowableUtil {
|
|||||||
* A supplier that can possibly throw a checked exception.
|
* A supplier that can possibly throw a checked exception.
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface SupplierException<T> { // TODO make exception type generic
|
public interface SupplierException<T, E extends Exception> {
|
||||||
/**
|
/**
|
||||||
* Gets a result.
|
* Gets a result.
|
||||||
* @return 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.
|
* A runnable that can possibly throw a checked exception.
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface RunnableException { // TODO make exception type generic
|
public interface RunnableException<E extends Exception> {
|
||||||
/**
|
/**
|
||||||
* Run any code implemented.
|
* 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user