GsonHolder: apply exclusion strategy to deserialization

throwable: add orThrow with exception constructor
This commit is contained in:
Johannes Frohnmeyer 2022-04-29 13:06:48 +02:00
parent 982fc0b9c6
commit 9196b77e5c
Signed by: Johannes
GPG Key ID: E76429612C2929F4
9 changed files with 54 additions and 9 deletions

View File

@ -19,7 +19,7 @@ public class GsonHolder {
.registerTypeAdapter(ComparableVersion.class, new ComparableVersionAdapter())
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.addSerializationExclusionStrategy(new GsonIgnoreExclusionStrategy())
.setExclusionStrategies(new GsonIgnoreExclusionStrategy())
.setPrettyPrinting();
private static boolean clean = false;

View File

@ -8,6 +8,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
@FunctionalInterface
public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
@ -90,11 +91,16 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiConsumer<T, U> orThrow() {
return orThrow(Try::runtimeException)::accept;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingBiConsumer<T, U, TEx1> orThrow(Function<Throwable, TEx1> generator) {
return (l, r) -> {
try {
this.accept(l, r);
} catch (Throwable e) {
throw Try.runtimeException(e);
throw generator.apply(e);
}
};
}

View File

@ -84,11 +84,16 @@ public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiFunction<T, U, R> orThrow() {
return orThrow(Try::runtimeException)::apply;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingBiFunction<T, U, R, TEx1> orThrow(Function<Throwable, TEx1> generator) {
return (t, u) -> {
try {
return this.apply(t, u);
} catch (Throwable e) {
throw Try.runtimeException(e);
throw generator.apply(e);
}
};
}

View File

@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Function;
import java.util.function.Predicate;
@FunctionalInterface
@ -86,11 +87,16 @@ public interface ThrowingBooleanSupplier<TEx extends Throwable> {
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BooleanSupplier orThrow() {
return orThrow(Try::runtimeException)::get;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingBooleanSupplier<TEx1> orThrow(Function<Throwable, TEx1> generator) {
return () -> {
try {
return this.get();
} catch (Throwable e) {
throw Try.runtimeException(e);
throw generator.apply(e);
}
};
}

View File

@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
@FunctionalInterface
public interface ThrowingConsumer<T, TEx extends Throwable> {
@ -78,11 +79,16 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Consumer<T> orThrow() {
return orThrow(Try::runtimeException)::accept;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingConsumer<T, TEx1> orThrow(Function<Throwable, TEx1> generator) {
return (t) -> {
try {
this.accept(t);
} catch (Throwable e) {
throw Try.runtimeException(e);
throw generator.apply(e);
}
};
}

View File

@ -63,11 +63,16 @@ public interface ThrowingFunction<T, R, TEx extends Throwable> {
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Function<T, R> orThrow() {
return orThrow(Try::runtimeException)::apply;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingFunction<T, R, TEx1> orThrow(Function<Throwable, TEx1> generator) {
return (t) -> {
try {
return this.apply(t);
} catch (Throwable e) {
throw Try.runtimeException(e);
throw generator.apply(e);
}
};
}

View File

@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
@FunctionalInterface
@ -103,11 +104,16 @@ public interface ThrowingPredicate<T, TEx extends Throwable> {
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Predicate<T> orThrow() {
return orThrow(Try::runtimeException)::test;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingPredicate<T, TEx1> orThrow(Function<Throwable, TEx1> generator) {
return (r) -> {
try {
return this.test(r);
} catch (Throwable e) {
throw Try.runtimeException(e);
throw generator.apply(e);
}
};
}

View File

@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
@FunctionalInterface
public interface ThrowingRunnable<TEx extends Throwable> {
@ -57,11 +58,16 @@ public interface ThrowingRunnable<TEx extends Throwable> {
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Runnable orThrow() {
return orThrow(Try::runtimeException)::run;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingRunnable<TEx1> orThrow(Function<Throwable, TEx1> generator) {
return () -> {
try {
this.run();
} catch (Throwable e) {
throw Try.runtimeException(e);
throw generator.apply(e);
}
};
}

View File

@ -52,11 +52,16 @@ public interface ThrowingSupplier<T, TEx extends Throwable> {
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Supplier<T> orThrow() {
return orThrow(Try::runtimeException)::get;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingSupplier<T, TEx1> orThrow(Function<Throwable, TEx1> generator) {
return () -> {
try {
return this.get();
} catch (Throwable e) {
throw Try.runtimeException(e);
throw generator.apply(e);
}
};
}