Reflect wrapper initialization does not crash anymore on the first exception. It accumulates all the exceptions and shows everything at the end.

This commit is contained in:
2023-05-09 11:57:05 +02:00
parent 3e0297c8af
commit 9b83f9699c
4 changed files with 197 additions and 84 deletions

View File

@@ -7,9 +7,9 @@ import java.util.function.ToIntBiFunction;
/**
* Implementation of the <a href="https://en.wikipedia.org/wiki/Levenshtein_distance">Levenshtein distance algorithm</a>
* that operate on characters. Its purpose is to compute a "distance" between two strings of characters, that represents
* how many edition operation it is needed to perform on the first string ({@code initialString}) to optain the second
* how many edition operations must be performed on the first string ({@code initialString}) to optain the second
* one ({@code finalString}).
*
* <p>
* All the parameters of the algorithm are configurable:
* <ul>
* <li>The score of adding a character</li>
@@ -21,15 +21,13 @@ import java.util.function.ToIntBiFunction;
* ToIntBiFunction)} (for a full control of the parameters) or {@link #LevenshteinDistance(String, String)} (to keep the
* default parameters value); then to call the method {@link #getCurrentDistance()} to compute the Levenshtein distance
* between the two strings.
*
* <p>
* A more advanced usage offer the possibility to progressively compute a distance from a predefined
* {@code initialString} to a {@code finalString} that is feeded progressively using {@link #add(char)} or
* {@link #add(String)}. This is useful if the {@code finalString} is an input that is currently being typed by the
* user, so the application can progressively update a list of suggested words based on the distance.
* For this usage, you can use those constructors to avoid initializing the {@code finalString}:
* {@link #LevenshteinDistance(String, int, int, ToIntBiFunction)} or {@link #LevenshteinDistance(String)}.
*
*
*/
public class LevenshteinDistance {

View File

@@ -0,0 +1,104 @@
package fr.pandacube.lib.util;
import fr.pandacube.lib.util.ThrowableUtil.RunnableException;
import fr.pandacube.lib.util.ThrowableUtil.SupplierException;
import java.util.function.Supplier;
/**
* A class that delay and accumulate thown exceptions, that can be thrown later using {@link #throwCatched()}.
* @param <T> the type of {@link Throwable} to accumulate.
*/
public class ThrowableAccumulator<T extends Throwable> {
T base = null;
final Class<T> throwableType;
/**
* Creates a new {@link ThrowableAccumulator} with the specified throwable type.
* @param throwableType The type of the {@link Throwable} to accumulate.
*/
public ThrowableAccumulator(Class<T> throwableType) {
this.throwableType = throwableType;
}
/**
* Run the provided {@link RunnableException}, catching an eventual exception to accumulate for later use.
* @param run the {@link RunnableException} to run.
* @throws Exception if an exception not handled by this accumulator is thrown.
*/
public void catchThrowable(RunnableException<Exception> run) throws Exception {
try {
run.run();
} catch (Throwable t) {
accumulateThrowable(t);
}
}
/**
* Run the provided {@link SupplierException}, catching an eventual exception to accumulate for later use.
* @param supp the {@link SupplierException} to run.
* @param returnValueIfException The value to return if this accumulator catch an exception.
* @return The return value of the supplier, or {@code returnValueIfException} if this accumulator catch an exception.
* @throws Exception if an exception not handled by this accumulator is thrown.
* @param <R> the type of the return value of the supplier.
*/
public <R> R catchThrowable(SupplierException<R, Exception> supp, R returnValueIfException) throws Exception {
return catchThrowable(supp, (Supplier<R>) () -> returnValueIfException);
}
/**
* Run the provided {@link SupplierException}, catching an eventual exception to accumulate for later use.
* @param supp the {@link SupplierException} to run.
* @param returnValueIfException The value to return if this accumulator catch an exception.
* @return The return value of the supplier, or the return value of {@code returnValueIfException} if this
* accumulator catch an exception.
* @throws Exception if an exception not handled by this accumulator is thrown.
* @param <R> the type of the return value of both suppliers.
*/
public <R> R catchThrowable(SupplierException<R, Exception> supp, Supplier<R> returnValueIfException) throws Exception {
try {
return supp.get();
} catch (Throwable t) {
accumulateThrowable(t);
}
return returnValueIfException.get();
}
private void accumulateThrowable(Throwable t) throws Exception {
if (throwableType.isInstance(t)) {
synchronized (this) {
if (base == null)
base = throwableType.cast(t);
else {
base.addSuppressed(t);
}
}
}
else {
throwEx(t);
}
}
/**
* Throws an exception if there is at least one catched by this accumulator.
* If multiple exception where catched, all the exception after the first one are added to the first one as
* suppressed exceptions.
* If no exception were catched, this method does nothing.
* @throws Exception the first accumulated throwable, the other ones being suppressed.
*/
public void throwCatched() throws Exception {
synchronized (this) {
if (base != null)
throwEx(base);
}
}
private void throwEx(Throwable t) throws Exception {
if (t instanceof Error e)
throw e;
else if (t instanceof Exception e)
throw e;
}
}