Some refactoring in pandalib-util
This commit is contained in:
@@ -4,43 +4,39 @@ import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Represents a lazy loaded value.
|
||||
*
|
||||
* The value will be computed using the Supplier provided in the
|
||||
* constructor, only the first time the {@link #get()} method is
|
||||
* called.
|
||||
* A Supplier that cache the value the first time it is called.
|
||||
*
|
||||
* @param <T> the type of the enclosed value.
|
||||
* @param <T> the type of the supplied value.
|
||||
*/
|
||||
public class Lazy<T> implements Supplier<T> {
|
||||
public class CachedSupplier<T> implements Supplier<T> {
|
||||
|
||||
private T cachedValue;
|
||||
private final Supplier<T> supplier;
|
||||
private final Supplier<T> source;
|
||||
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 Lazy(Supplier<T> s) {
|
||||
supplier = Objects.requireNonNull(s);
|
||||
public CachedSupplier(Supplier<T> s) {
|
||||
source = Objects.requireNonNull(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the wrapped value, from cache or from the provider if it is not yet cached.
|
||||
*
|
||||
* Get the value, from cache or from the source supplier if it's 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() {
|
||||
if (!cached)
|
||||
set(supplier.get());
|
||||
set(source.get());
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the cached value. The next call to {@link #get()} will get the value from the provider.
|
||||
* Reset the cached value. The next call to {@link #get()} will get the value from the source.
|
||||
*/
|
||||
public synchronized void reset() {
|
||||
cached = false;
|
@@ -1,35 +1,31 @@
|
||||
package fr.pandacube.lib.util;
|
||||
|
||||
import fr.pandacube.lib.util.function.SupplierException;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
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.
|
||||
* A Supplier that cache the value the first time it is called.
|
||||
*
|
||||
* @param <T> the type of the enclosed value.
|
||||
* @param <E> the exception type
|
||||
* @param <T> the type of the supplied value.
|
||||
* @param <E> the exception type that may be thrown by the source supplier.
|
||||
*/
|
||||
public class LazyOrException<T, E extends Exception> implements SupplierException<T, E> {
|
||||
public class CachedSupplierException<T, E extends Exception> implements SupplierException<T, E> {
|
||||
|
||||
private T cachedValue;
|
||||
private final SupplierException<T, E> supplier;
|
||||
private final SupplierException<T, E> source;
|
||||
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, E> s) {
|
||||
supplier = Objects.requireNonNull(s);
|
||||
public CachedSupplierException(SupplierException<T, E> s) {
|
||||
source = Objects.requireNonNull(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the wrapped value, from cache or from the provider if it is not yet cached.
|
||||
* Get the value, from cache or from the provider if it's 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).
|
||||
@@ -37,12 +33,12 @@ public class LazyOrException<T, E extends Exception> implements SupplierExceptio
|
||||
@Override
|
||||
public synchronized T get() throws E {
|
||||
if (!cached)
|
||||
set(supplier.get());
|
||||
set(source.get());
|
||||
return cachedValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the cached value. The next call to {@link #get()} will get the value from the provider.
|
||||
* Reset the cached value. The next call to {@link #get()} will get the value from the source.
|
||||
*/
|
||||
public synchronized void reset() {
|
||||
cached = false;
|
@@ -1,7 +1,7 @@
|
||||
package fr.pandacube.lib.util;
|
||||
|
||||
import fr.pandacube.lib.util.ThrowableUtil.RunnableException;
|
||||
import fr.pandacube.lib.util.ThrowableUtil.SupplierException;
|
||||
import fr.pandacube.lib.util.function.RunnableException;
|
||||
import fr.pandacube.lib.util.function.SupplierException;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package fr.pandacube.lib.util;
|
||||
|
||||
import fr.pandacube.lib.util.function.RunnableException;
|
||||
import fr.pandacube.lib.util.function.SupplierException;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
@@ -145,101 +148,4 @@ public class ThrowableUtil {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A supplier that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface SupplierException<T, E extends Exception> {
|
||||
/**
|
||||
* Gets a result.
|
||||
* @return a result.
|
||||
* @throws E if implementation failed to run.
|
||||
*/
|
||||
T get() throws E;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A runnable that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface RunnableException<E extends Exception> {
|
||||
/**
|
||||
* Run any code implemented.
|
||||
* @throws E if implementation failed to run.
|
||||
*/
|
||||
void run() throws E;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A predicate that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface PredicateException<T, E extends Exception> {
|
||||
/**
|
||||
* Test the predicate on the specified value.
|
||||
* @param value the value to test against.
|
||||
* @return the result of the test.
|
||||
* @throws E if implementation failed to run.
|
||||
*/
|
||||
boolean test(T value) throws E;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A function that can possibly throw a checked exception.
|
||||
*/
|
||||
public interface ToIntBiFunctionException<T, U, E extends Exception> {
|
||||
/**
|
||||
* Run on the specified parameters to return an int value.
|
||||
* @param t the first parameter of the function.
|
||||
* @param u the second parameter of the function.
|
||||
* @return the result of the function.
|
||||
* @throws E if the function fails.
|
||||
*/
|
||||
int applyAsInt(T t, U u) throws E;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A consumer that can possibly throw a checked exception.
|
||||
*/
|
||||
public interface BiConsumerException<T, U, E extends Exception> {
|
||||
/**
|
||||
* Run the consumer on the specified parameters.
|
||||
* @param t the first parameter of the consumer.
|
||||
* @param u the second parameter of the consumer.
|
||||
* @throws E if the function fails.
|
||||
*/
|
||||
void accept(T t, U u) throws E;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* A consumer that can possibly throw a checked exception.
|
||||
*/
|
||||
public interface TriConsumerException<T, U, V, E extends Exception> {
|
||||
/**
|
||||
* Run the consumer on the specified parameters.
|
||||
* @param t the first parameter of the consumer.
|
||||
* @param u the second parameter of the consumer.
|
||||
* @param v the tird parameter of the consumer.
|
||||
* @throws E if the function fails.
|
||||
*/
|
||||
void accept(T t, U u, V v) throws E;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,16 @@
|
||||
package fr.pandacube.lib.util.function;
|
||||
|
||||
/**
|
||||
* A consumer that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface BiConsumerException<T, U, E extends Exception> {
|
||||
/**
|
||||
* Run the consumer on the specified parameters.
|
||||
*
|
||||
* @param t the first parameter of the consumer.
|
||||
* @param u the second parameter of the consumer.
|
||||
* @throws E if the function fails.
|
||||
*/
|
||||
void accept(T t, U u) throws E;
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package fr.pandacube.lib.util.function;
|
||||
|
||||
/**
|
||||
* A predicate that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface PredicateException<T, E extends Exception> {
|
||||
/**
|
||||
* Test the predicate on the specified value.
|
||||
*
|
||||
* @param value the value to test against.
|
||||
* @return the result of the test.
|
||||
* @throws E if implementation failed to run.
|
||||
*/
|
||||
boolean test(T value) throws E;
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package fr.pandacube.lib.util.function;
|
||||
|
||||
/**
|
||||
* A runnable that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface RunnableException<E extends Exception> {
|
||||
/**
|
||||
* Run any code implemented.
|
||||
*
|
||||
* @throws E if implementation failed to run.
|
||||
*/
|
||||
void run() throws E;
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package fr.pandacube.lib.util.function;
|
||||
|
||||
/**
|
||||
* A supplier that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface SupplierException<T, E extends Exception> {
|
||||
/**
|
||||
* Gets a result.
|
||||
*
|
||||
* @return a result.
|
||||
* @throws E if implementation failed to run.
|
||||
*/
|
||||
T get() throws E;
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package fr.pandacube.lib.util.function;
|
||||
|
||||
/**
|
||||
* A function that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToIntBiFunctionException<T, U, E extends Exception> {
|
||||
/**
|
||||
* Run on the specified parameters to return an int value.
|
||||
*
|
||||
* @param t the first parameter of the function.
|
||||
* @param u the second parameter of the function.
|
||||
* @return the result of the function.
|
||||
* @throws E if the function fails.
|
||||
*/
|
||||
int applyAsInt(T t, U u) throws E;
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package fr.pandacube.lib.util.function;
|
||||
|
||||
/**
|
||||
* A consumer that can possibly throw a checked exception.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface TriConsumerException<T, U, V, E extends Exception> {
|
||||
/**
|
||||
* Run the consumer on the specified parameters.
|
||||
*
|
||||
* @param t the first parameter of the consumer.
|
||||
* @param u the second parameter of the consumer.
|
||||
* @param v the third parameter of the consumer.
|
||||
* @throws E if the function fails.
|
||||
*/
|
||||
void accept(T t, U u, V v) throws E;
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package fr.pandacube.lib.util.logs;
|
||||
package fr.pandacube.lib.util.log;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
@@ -1,4 +1,4 @@
|
||||
package fr.pandacube.lib.util;
|
||||
package fr.pandacube.lib.util.log;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Level;
|
Reference in New Issue
Block a user