[main] Even more generics additions

This commit is contained in:
Johannes Frohnmeyer 2022-04-22 19:18:17 +02:00
parent 472258c62b
commit 246ebfac32
Signed by: Johannes
GPG Key ID: E76429612C2929F4
10 changed files with 321 additions and 9 deletions

View File

@ -8,6 +8,28 @@ import java.util.function.Consumer;
public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
void accept(T var1, U var2) throws TEx;
default <V> ThrowingConsumer<V, TEx> compose(ThrowingFunction<? super V, ? extends T, ? extends TEx> left, ThrowingFunction<? super V, ? extends U, ? extends TEx> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return (t) -> this.accept(left.apply(t), right.apply(t));
}
default <V, W> ThrowingBiConsumer<V, W, TEx> biCompose(ThrowingFunction<? super V, ? extends T, ? extends TEx> left, ThrowingFunction<? super W, ? extends U, ? extends TEx> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return (l, r) -> this.accept(left.apply(l), right.apply(r));
}
default <V> ThrowingBiConsumer<V, U, TEx> composeLeft(ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.accept(before.apply(l), r);
}
default <V> ThrowingBiConsumer<T, V, TEx> composeRight(ThrowingFunction<? super V, ? extends U, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.accept(l, before.apply(r));
}
default ThrowingBiConsumer<T, U, TEx> andThen(ThrowingBiConsumer<? super T, ? super U, ? extends TEx> after) {
Objects.requireNonNull(after);
return (l, r) -> {
@ -16,7 +38,16 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
};
}
default ThrowingBiConsumer<T, U, TEx> andThen(ThrowingRunnable<? extends TEx> after) {
Objects.requireNonNull(after);
return (l, r) -> {
this.accept(l, r);
after.run();
};
}
default BiConsumer<T, U> addHandler(Consumer<Throwable> handler) {
Objects.requireNonNull(handler);
return (l, r) -> {
try {
this.accept(l, r);
@ -25,4 +56,14 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
}
};
}
default BiConsumer<T, U> orThrow() {
return (l, r) -> {
try {
this.accept(l, r);
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}

View File

@ -8,12 +8,40 @@ import java.util.function.Function;
public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
R apply(T var1, U var2) throws TEx;
default <V> ThrowingBiFunction<T, U, V, TEx> andThen(ThrowingFunction<? super R, ? extends V, TEx> after) {
default <V> ThrowingFunction<V, R, TEx> compose(ThrowingFunction<? super V, ? extends T, ? extends TEx> left, ThrowingFunction<? super V, ? extends U, ? extends TEx> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return (t) -> this.apply(left.apply(t), right.apply(t));
}
default <V, W> ThrowingBiFunction<V, W, R, TEx> biCompose(ThrowingFunction<? super V, ? extends T, ? extends TEx> left, ThrowingFunction<? super W, ? extends U, ? extends TEx> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return (l, r) -> this.apply(left.apply(l), right.apply(r));
}
default <V> ThrowingBiFunction<V, U, R, TEx> composeLeft(ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.apply(before.apply(l), r);
}
default <V> ThrowingBiFunction<T, V, R, TEx> composeRight(ThrowingFunction<? super V, ? extends U, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.apply(l, before.apply(r));
}
default <V> ThrowingBiFunction<T, U, V, TEx> andThen(ThrowingFunction<? super R, ? extends V, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t, u) -> after.apply(this.apply(t, u));
}
default BiFunction<T, U, R> addHandler(Function<Throwable, R> handler) {
default ThrowingBiConsumer<T, U, TEx> andThen(ThrowingConsumer<? super R, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t, u) -> after.accept(this.apply(t, u));
}
default BiFunction<T, U, R> addHandler(Function<Throwable, ? extends R> handler) {
Objects.requireNonNull(handler);
return (t, u) -> {
try {
return this.apply(t, u);
@ -22,4 +50,14 @@ public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
}
};
}
default BiFunction<T, U, R> orThrow() {
return (t, u) -> {
try {
return this.apply(t, u);
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}

View File

@ -0,0 +1,31 @@
package io.gitlab.jfronny.libjf.generic;
import java.util.Objects;
import java.util.function.BooleanSupplier;
import java.util.function.Predicate;
@FunctionalInterface
public interface ThrowingBooleanSupplier<TEx extends Throwable> {
boolean get() throws TEx;
default BooleanSupplier addHandler(Predicate<Throwable> handler) {
Objects.requireNonNull(handler);
return () -> {
try {
return this.get();
} catch (Throwable e) {
return handler.test(e);
}
};
}
default BooleanSupplier orThrow() {
return () -> {
try {
return this.get();
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}

View File

@ -7,6 +7,16 @@ import java.util.function.Consumer;
public interface ThrowingConsumer<T, TEx extends Throwable> {
void accept(T var1) throws TEx;
default <V> ThrowingConsumer<V, TEx> compose(ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (t) -> this.accept(before.apply(t));
}
default ThrowingRunnable<TEx> compose(ThrowingSupplier<? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return () -> this.accept(before.get());
}
default ThrowingConsumer<T, TEx> andThen(ThrowingConsumer<? super T, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> {
@ -15,7 +25,24 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
};
}
default ThrowingConsumer<T, TEx> andThen(ThrowingRunnable<? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> {
this.accept(t);
after.run();
};
}
default <V> ThrowingBiConsumer<T, V, TEx> compound(ThrowingConsumer<? super V, ? extends TEx> other) {
Objects.requireNonNull(other);
return (l, r) -> {
this.accept(l);
other.accept(r);
};
}
default Consumer<T> addHandler(Consumer<Throwable> handler) {
Objects.requireNonNull(handler);
return (t) -> {
try {
this.accept(t);
@ -24,4 +51,14 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
}
};
}
default Consumer<T> orThrow() {
return (t) -> {
try {
this.accept(t);
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}

View File

@ -7,6 +7,11 @@ import java.util.function.Function;
public interface ThrowingFunction<T, R, TEx extends Throwable> {
R apply(T var1) throws TEx;
default ThrowingSupplier<R, TEx> compose(ThrowingSupplier<? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return () -> this.apply(before.get());
}
default <V> ThrowingFunction<V, R, TEx> compose(ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (v) -> this.apply(before.apply(v));
@ -17,7 +22,18 @@ public interface ThrowingFunction<T, R, TEx extends Throwable> {
return (t) -> after.apply(this.apply(t));
}
default Function<T, R> addHandler(Function<Throwable, R> handler) {
default ThrowingPredicate<T, TEx> andThen(ThrowingPredicate<? super R, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> after.test(this.apply(t));
}
default ThrowingConsumer<T, TEx> andThen(ThrowingConsumer<? super R, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> after.accept(this.apply(t));
}
default Function<T, R> addHandler(Function<Throwable, ? extends R> handler) {
Objects.requireNonNull(handler);
return (t) -> {
try {
return this.apply(t);
@ -26,4 +42,14 @@ public interface ThrowingFunction<T, R, TEx extends Throwable> {
}
};
}
default Function<T, R> orThrow() {
return (t) -> {
try {
return this.apply(t);
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}

View File

@ -0,0 +1,63 @@
package io.gitlab.jfronny.libjf.generic;
import java.util.Objects;
import java.util.function.Predicate;
@FunctionalInterface
public interface ThrowingPredicate<T, TEx extends Throwable> {
boolean test(T var1) throws TEx;
default <V> ThrowingPredicate<V, TEx> compose(ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (t) -> this.test(before.apply(t));
}
default ThrowingBooleanSupplier<TEx> compose(ThrowingSupplier<? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return () -> this.test(before.get());
}
default ThrowingPredicate<T, TEx> and(ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.test(t) && other.test(t);
}
default ThrowingPredicate<T, TEx> negate() {
return (t) -> !this.test(t);
}
default ThrowingPredicate<T, TEx> or(ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.test(t) || other.test(t);
}
static <T> ThrowingPredicate<T, Throwable> isEqual(Object targetRef) {
return null == targetRef ? Objects::isNull : targetRef::equals;
}
static <T, TEx extends Throwable> ThrowingPredicate<T, TEx> not(ThrowingPredicate<T, TEx> target) {
Objects.requireNonNull(target);
return target.negate();
}
default Predicate<T> addHandler(Predicate<Throwable> handler) {
Objects.requireNonNull(handler);
return (r) -> {
try {
return this.test(r);
} catch (Throwable e) {
return handler.test(e);
}
};
}
default Predicate<T> orThrow() {
return (r) -> {
try {
return this.test(r);
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}

View File

@ -1,12 +1,30 @@
package io.gitlab.jfronny.libjf.generic;
import java.util.Objects;
import java.util.function.Consumer;
@FunctionalInterface
public interface ThrowingRunnable<TEx extends Throwable> {
void run() throws TEx;
default ThrowingRunnable<TEx> compose(ThrowingRunnable<? extends TEx> before) {
Objects.requireNonNull(before);
return () -> {
before.run();
this.run();
};
}
default ThrowingRunnable<TEx> andThen(ThrowingRunnable<? extends TEx> after) {
Objects.requireNonNull(after);
return () -> {
this.run();
after.run();
};
}
default Runnable addHandler(Consumer<Throwable> handler) {
Objects.requireNonNull(handler);
return () -> {
try {
this.run();
@ -15,4 +33,14 @@ public interface ThrowingRunnable<TEx extends Throwable> {
}
};
}
default Runnable orThrow() {
return () -> {
try {
this.run();
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}

View File

@ -1,5 +1,6 @@
package io.gitlab.jfronny.libjf.generic;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
@ -7,7 +8,23 @@ import java.util.function.Supplier;
public interface ThrowingSupplier<T, TEx extends Throwable> {
T get() throws TEx;
default Supplier<T> addHandler(Function<Throwable, T> handler) {
default <V> ThrowingSupplier<V, TEx> andThen(ThrowingFunction<? super T, ? extends V, ? extends TEx> after) {
Objects.requireNonNull(after);
return () -> after.apply(this.get());
}
default ThrowingBooleanSupplier<TEx> andThen(ThrowingPredicate<? super T, ? extends TEx> after) {
Objects.requireNonNull(after);
return () -> after.test(this.get());
}
default ThrowingRunnable<TEx> andThen(ThrowingConsumer<? super T, ? extends TEx> after) {
Objects.requireNonNull(after);
return () -> after.accept(this.get());
}
default Supplier<T> addHandler(Function<Throwable, ? extends T> handler) {
Objects.requireNonNull(handler);
return () -> {
try {
return this.get();
@ -16,4 +33,14 @@ public interface ThrowingSupplier<T, TEx extends Throwable> {
}
};
}
default Supplier<T> orThrow() {
return () -> {
try {
return this.get();
} catch (Throwable e) {
throw Try.runtimeException(e);
}
};
}
}

View File

@ -8,7 +8,27 @@ public class Try {
tr.addHandler(alternative).run();
}
public static <T> T orElse(ThrowingSupplier<T, ?> tr, Function<Throwable, T> alternative) {
public static <T> T orElse(ThrowingSupplier<T, ?> tr, Function<Throwable, ? extends T> alternative) {
return tr.addHandler(alternative).get();
}
public static void orThrow(ThrowingRunnable<?> tr) {
try {
tr.run();
} catch (Throwable e) {
throw runtimeException(e);
}
}
public static <T> T orThrow(ThrowingSupplier<T, ?> tr) {
try {
return tr.get();
} catch (Throwable e) {
throw runtimeException(e);
}
}
protected static RuntimeException runtimeException(Throwable t) {
return t instanceof RuntimeException e ? e : new RuntimeException(t);
}
}

View File

@ -15,6 +15,7 @@ import net.minecraft.text.Text;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;
@Environment(EnvType.CLIENT)
@ -32,7 +33,7 @@ public class MidnightConfigListWidget extends ElementListWidget<MidnightConfigLi
return this.width -7;
}
public void addButton(ClickableWidget button, ClickableWidget resetButton, Supplier<Boolean> resetVisible, Text text) {
public void addButton(ClickableWidget button, ClickableWidget resetButton, BooleanSupplier resetVisible, Text text) {
this.addEntry(new ConfigScreenEntry(button, text, resetVisible, resetButton));
}
@ -89,11 +90,11 @@ public class MidnightConfigListWidget extends ElementListWidget<MidnightConfigLi
public static class ConfigScreenEntry extends ConfigListEntryAbstract {
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
public final ClickableWidget button;
private final Supplier<Boolean> resetVisible;
private final BooleanSupplier resetVisible;
private final ClickableWidget resetButton;
private final Text text;
public ConfigScreenEntry(ClickableWidget button, Text text, Supplier<Boolean> resetVisible, ClickableWidget resetButton) {
public ConfigScreenEntry(ClickableWidget button, Text text, BooleanSupplier resetVisible, ClickableWidget resetButton) {
this.button = button;
this.resetVisible = resetVisible;
this.resetButton = resetButton;
@ -105,7 +106,7 @@ public class MidnightConfigListWidget extends ElementListWidget<MidnightConfigLi
button.y = y;
button.render(matrices, mouseX, mouseY, tickDelta);
drawTextWithShadow(matrices,textRenderer, text,12,y+5,0xFFFFFF);
if (resetVisible.get()) {
if (resetVisible.getAsBoolean()) {
resetButton.y = y;
resetButton.render(matrices, mouseX, mouseY, tickDelta);
}