[main] Add MultiAccessFileSystem as thread-"safe" wrapper around file systems

This commit is contained in:
Johannes Frohnmeyer 2022-06-27 14:26:52 +02:00
parent 9e0711efc7
commit 34c431efc1
Signed by: Johannes
GPG Key ID: E76429612C2929F4
17 changed files with 427 additions and 265 deletions

View File

@ -39,8 +39,12 @@ public class LazySupplier<T> implements Supplier<T> {
initialized = false;
}
@Override @Contract(pure = true) @NotNull public T get() {
if (!initialized) cache = supplier.get();
@Override
public @NotNull T get() {
if (!initialized) {
cache = supplier.get();
initialized = true;
}
return cache;
}
@ -51,7 +55,8 @@ public class LazySupplier<T> implements Supplier<T> {
* @param after A backing function for the new lazy supplier
* @return A new lazy supplier
*/
@Contract(pure = true) @NotNull public LazySupplier<T> andThen(@NotNull Function<LazySupplier<T>, T> after) {
@Contract(value = "_ -> new", pure = true)
public @NotNull LazySupplier<T> andThen(@NotNull Function<LazySupplier<T>, T> after) {
return new LazySupplier<>(() -> after.apply(this));
}
}

View File

@ -0,0 +1,148 @@
package io.gitlab.jfronny.commons.io;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.nio.file.*;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.spi.FileSystemProvider;
import java.util.*;
public class MultiAccessFileSystem implements Closeable {
@Contract(value = "_ -> new", pure = true)
public static @NotNull MultiAccessFileSystem wrap(FileSystem delegate) {
return new MultiAccessFileSystem(delegate);
}
@Contract("_ -> new")
public static @NotNull MultiAccessFileSystem create(URI uri) throws IOException {
return wrap(FileSystems.getFileSystem(uri));
}
@Contract("_, _ -> new")
public static @NotNull MultiAccessFileSystem create(URI uri, ClassLoader loader) throws IOException {
return wrap(FileSystems.newFileSystem(uri, Map.of(), loader));
}
@Contract("_, _ -> new")
public static @NotNull MultiAccessFileSystem create(URI uri, Map<String, ?> env) throws IOException {
return wrap(FileSystems.newFileSystem(uri, env));
}
@Contract("_, _, _ -> new")
public static @NotNull MultiAccessFileSystem create(URI uri, Map<String, ?> env, ClassLoader loader) throws IOException {
return wrap(FileSystems.newFileSystem(uri, env, loader));
}
@Contract("_ -> new")
public static @NotNull MultiAccessFileSystem create(Path path) throws IOException {
return wrap(FileSystems.newFileSystem(path));
}
@Contract("_, _ -> new")
public static @NotNull MultiAccessFileSystem create(Path path, ClassLoader loader) throws IOException {
return wrap(FileSystems.newFileSystem(path, loader));
}
@Contract("_, _ -> new")
public static @NotNull MultiAccessFileSystem create(Path path, Map<String, ?> env) throws IOException {
return wrap(FileSystems.newFileSystem(path, env));
}
@Contract("_, _, _ -> new")
public static @NotNull MultiAccessFileSystem create(Path path, Map<String, ?> env, ClassLoader loader) throws IOException {
return wrap(FileSystems.newFileSystem(path, env, loader));
}
private final FileSystem delegate;
private final List<Lens> lenses = new LinkedList<>();
private boolean closed = false;
private MultiAccessFileSystem(FileSystem delegate) {
this.delegate = delegate;
}
@Override
public synchronized void close() throws IOException {
if (!lenses.isEmpty()) throw new IOException("Attempted to close multi-access file system with live lenses");
closed = true;
delegate.close();
}
public boolean isClosed() {
return closed;
}
public synchronized FileSystem createLens() throws IOException {
if (closed) throw new ClosedFileSystemException();
Lens lens = new Lens();
lenses.add(lens);
return lens;
}
private class Lens extends FileSystem {
@Override
public FileSystemProvider provider() {
return delegate.provider();
}
@Override
public void close() throws IOException {
lenses.remove(this);
if (lenses.isEmpty()) MultiAccessFileSystem.this.close();
}
@Override
public boolean isOpen() {
return delegate.isOpen();
}
@Override
public boolean isReadOnly() {
return delegate.isReadOnly();
}
@Override
public String getSeparator() {
return delegate.getSeparator();
}
@Override
public Iterable<Path> getRootDirectories() {
return delegate.getRootDirectories();
}
@Override
public Iterable<FileStore> getFileStores() {
return delegate.getFileStores();
}
@Override
public Set<String> supportedFileAttributeViews() {
return delegate.supportedFileAttributeViews();
}
@Override
public @NotNull Path getPath(String s, String... strings) {
return delegate.getPath(s, strings);
}
@Override
public PathMatcher getPathMatcher(String s) {
return delegate.getPathMatcher(s);
}
@Override
public UserPrincipalLookupService getUserPrincipalLookupService() {
return delegate.getUserPrincipalLookupService();
}
@Override
public WatchService newWatchService() throws IOException {
return delegate.newWatchService();
}
}
}

View File

@ -14,8 +14,7 @@ public class SerializerHolder {
* Get the current serializer. Configure via setInstance
* @return The current serializer
*/
@NotNull
public static Serializer getInstance() {
public static @NotNull Serializer getInstance() {
if (instance == null) throw new RuntimeException("No serializer was configured but one was requested");
return instance;
}

View File

@ -15,8 +15,8 @@ public class Coerce {
* @param tr A throwing biConsumer
* @return The throwing biConsumer
*/
@Contract(pure = true) @NotNull
public static <T, U, TEx extends Throwable> ThrowingBiConsumer<T, U, TEx> biConsumer(@NotNull ThrowingBiConsumer<T, U, TEx> tr) {
@Contract(value = "_ -> param1", pure = true)
public static <T, U, TEx extends Throwable> @NotNull ThrowingBiConsumer<T, U, TEx> biConsumer(@NotNull ThrowingBiConsumer<T, U, TEx> tr) {
return tr;
}
@ -25,8 +25,8 @@ public class Coerce {
* @param tr A throwing biFunction
* @return The throwing biFunction
*/
@Contract(pure = true) @NotNull
public static <T, U, R, TEx extends Throwable> ThrowingBiFunction<T, U, R, TEx> biFunction(@NotNull ThrowingBiFunction<T, U, R, TEx> tr) {
@Contract(value = "_ -> param1", pure = true)
public static <T, U, R, TEx extends Throwable> @NotNull ThrowingBiFunction<T, U, R, TEx> biFunction(@NotNull ThrowingBiFunction<T, U, R, TEx> tr) {
return tr;
}
@ -35,8 +35,8 @@ public class Coerce {
* @param tr A throwing booleanSupplier
* @return The throwing booleanSupplier
*/
@Contract(pure = true) @NotNull
public static <TEx extends Throwable> ThrowingBooleanSupplier<TEx> booleanSupplier(@NotNull ThrowingBooleanSupplier<TEx> tr) {
@Contract(value = "_ -> param1", pure = true)
public static <TEx extends Throwable> @NotNull ThrowingBooleanSupplier<TEx> booleanSupplier(@NotNull ThrowingBooleanSupplier<TEx> tr) {
return tr;
}
@ -45,8 +45,8 @@ public class Coerce {
* @param tr A throwing consumer
* @return The throwing consumer
*/
@Contract(pure = true) @NotNull
public static <T, TEx extends Throwable> ThrowingConsumer<T, TEx> consumer(@NotNull ThrowingConsumer<T, TEx> tr) {
@Contract(value = "_ -> param1", pure = true)
public static <T, TEx extends Throwable> @NotNull ThrowingConsumer<T, TEx> consumer(@NotNull ThrowingConsumer<T, TEx> tr) {
return tr;
}
@ -55,8 +55,8 @@ public class Coerce {
* @param tr A throwing function
* @return The throwing function
*/
@Contract(pure = true) @NotNull
public static <T, R, TEx extends Throwable> ThrowingFunction<T, R, TEx> function(@NotNull ThrowingFunction<T, R, TEx> tr) {
@Contract(value = "_ -> param1", pure = true)
public static <T, R, TEx extends Throwable> @NotNull ThrowingFunction<T, R, TEx> function(@NotNull ThrowingFunction<T, R, TEx> tr) {
return tr;
}
@ -65,8 +65,8 @@ public class Coerce {
* @param tr A throwing predicate
* @return The throwing predicate
*/
@Contract(pure = true) @NotNull
public static <T, TEx extends Throwable> ThrowingPredicate<T, TEx> predicate(@NotNull ThrowingPredicate<T, TEx> tr) {
@Contract(value = "_ -> param1", pure = true)
public static <T, TEx extends Throwable> @NotNull ThrowingPredicate<T, TEx> predicate(@NotNull ThrowingPredicate<T, TEx> tr) {
return tr;
}
@ -75,8 +75,8 @@ public class Coerce {
* @param tr A throwing runnable
* @return The throwing runnable
*/
@Contract(pure = true) @NotNull
public static <TEx extends Throwable> ThrowingRunnable<TEx> runnable(@NotNull ThrowingRunnable<TEx> tr) {
@Contract(value = "_ -> param1", pure = true)
public static <TEx extends Throwable> @NotNull ThrowingRunnable<TEx> runnable(@NotNull ThrowingRunnable<TEx> tr) {
return tr;
}
@ -85,8 +85,8 @@ public class Coerce {
* @param tr A throwing supplier
* @return The throwing supplier
*/
@Contract(pure = true) @NotNull
public static <T, TEx extends Throwable> ThrowingSupplier<T, TEx> supplier(@NotNull ThrowingSupplier<T, TEx> tr) {
@Contract(value = "_ -> param1", pure = true)
public static <T, TEx extends Throwable> @NotNull ThrowingSupplier<T, TEx> supplier(@NotNull ThrowingSupplier<T, TEx> tr) {
return tr;
}

View File

@ -14,39 +14,39 @@ import java.util.function.Function;
public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
void accept(T var1, U var2) throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingConsumer<Tuple<T, U>, TEx> fromTuple() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingConsumer<Tuple<T, U>, TEx> fromTuple() {
return (t) -> this.accept(t.left(), t.right());
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingConsumer<V, ? super TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> right) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingConsumer<V, ? super TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return (t) -> this.accept(left.apply(t), right.apply(t));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V, W> ThrowingBiConsumer<V, W, TEx> biCompose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull ThrowingFunction<? super W, ? extends U, ? extends TEx> right) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default <V, W> @NotNull ThrowingBiConsumer<V, W, TEx> biCompose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull 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));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiConsumer<V, U, TEx> composeLeft(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingBiConsumer<V, U, TEx> composeLeft(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.accept(before.apply(l), r);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiConsumer<T, V, TEx> composeRight(@NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingBiConsumer<T, V, TEx> composeRight(@NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.accept(l, before.apply(r));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingBiConsumer<? super T, ? super U, ? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingBiConsumer<? super T, ? super U, ? extends TEx> after) {
Objects.requireNonNull(after);
return (l, r) -> {
this.accept(l, r);
@ -54,8 +54,8 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingRunnable<? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingRunnable<? extends TEx> after) {
Objects.requireNonNull(after);
return (l, r) -> {
this.accept(l, r);
@ -63,8 +63,8 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiConsumer<T, U> addHandler(@NotNull Consumer<Throwable> handler) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull BiConsumer<T, U> addHandler(@NotNull Consumer<Throwable> handler) {
Objects.requireNonNull(handler);
return (l, r) -> {
try {
@ -75,8 +75,9 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiConsumer<T, U> addHandler(@NotNull Class<TEx> exception, @NotNull Consumer<TEx> handler) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull BiConsumer<T, U> addHandler(@NotNull Class<TEx> exception, @NotNull Consumer<TEx> handler) {
Objects.requireNonNull(exception);
Objects.requireNonNull(handler);
return (l, r) -> {
try {
@ -89,13 +90,14 @@ public interface ThrowingBiConsumer<T, U, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiConsumer<T, U> orThrow() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull 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) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <TEx1 extends Throwable> @NotNull ThrowingBiConsumer<T, U, TEx1> orThrow(@NotNull Function<Throwable, TEx1> generator) {
Objects.requireNonNull(generator);
return (l, r) -> {
try {
this.accept(l, r);

View File

@ -13,51 +13,51 @@ import java.util.function.Function;
public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
R apply(T var1, U var2) throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingFunction<Tuple<T, U>, R, TEx> fromTuple() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingFunction<Tuple<T, U>, R, TEx> fromTuple() {
return (t) -> this.apply(t.left(), t.right());
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingFunction<V, R, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> right) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingFunction<V, R, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> right) {
Objects.requireNonNull(left);
Objects.requireNonNull(right);
return (t) -> this.apply(left.apply(t), right.apply(t));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V, W> ThrowingBiFunction<V, W, R, TEx> biCompose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull ThrowingFunction<? super W, ? extends U, ? extends TEx> right) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default <V, W> @NotNull ThrowingBiFunction<V, W, R, TEx> biCompose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> left, @NotNull 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));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiFunction<V, U, R, TEx> composeLeft(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingBiFunction<V, U, R, TEx> composeLeft(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.apply(before.apply(l), r);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiFunction<T, V, R, TEx> composeRight(@NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingBiFunction<T, V, R, TEx> composeRight(@NotNull ThrowingFunction<? super V, ? extends U, ? extends TEx> before) {
Objects.requireNonNull(before);
return (l, r) -> this.apply(l, before.apply(r));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiFunction<T, U, V, TEx> andThen(@NotNull ThrowingFunction<? super R, ? extends V, ? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingBiFunction<T, U, V, TEx> andThen(@NotNull ThrowingFunction<? super R, ? extends V, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t, u) -> after.apply(this.apply(t, u));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingConsumer<? super R, ? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingBiConsumer<T, U, TEx> andThen(@NotNull ThrowingConsumer<? super R, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t, u) -> after.accept(this.apply(t, u));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiFunction<T, U, R> addHandler(@NotNull Function<Throwable, ? extends R> handler) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull BiFunction<T, U, R> addHandler(@NotNull Function<Throwable, ? extends R> handler) {
Objects.requireNonNull(handler);
return (t, u) -> {
try {
@ -68,8 +68,9 @@ public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiFunction<T, U, R> addHandler(@NotNull Class<TEx> exception, @NotNull Function<TEx, ? extends R> handler) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull BiFunction<T, U, R> addHandler(@NotNull Class<TEx> exception, @NotNull Function<TEx, ? extends R> handler) {
Objects.requireNonNull(exception);
Objects.requireNonNull(handler);
return (t, u) -> {
try {
@ -82,13 +83,14 @@ public interface ThrowingBiFunction<T, U, R, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BiFunction<T, U, R> orThrow() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull 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) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <TEx1 extends Throwable> @NotNull ThrowingBiFunction<T, U, R, TEx1> orThrow(@NotNull Function<Throwable, TEx1> generator) {
Objects.requireNonNull(generator);
return (t, u) -> {
try {
return this.apply(t, u);

View File

@ -13,54 +13,54 @@ import java.util.function.Predicate;
public interface ThrowingBooleanSupplier<TEx extends Throwable> {
boolean get() throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <T> ThrowingPredicate<T, TEx> and(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <T> @NotNull ThrowingPredicate<T, TEx> and(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() && other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> and(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingBooleanSupplier<TEx> and(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() && other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <T> ThrowingPredicate<T, TEx> or(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <T> @NotNull ThrowingPredicate<T, TEx> or(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() || other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> or(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingBooleanSupplier<TEx> or(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() || other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <T> ThrowingPredicate<T, TEx> xor(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <T> @NotNull ThrowingPredicate<T, TEx> xor(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.get() ^ other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> xor(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingBooleanSupplier<TEx> xor(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return () -> this.get() ^ other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> negate() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingBooleanSupplier<TEx> negate() {
return () -> !this.get();
}
@Contract(pure = true) @NotNull
static <TEx extends Throwable> ThrowingBooleanSupplier<TEx> not(@NotNull ThrowingBooleanSupplier<TEx> target) {
@Contract(value = "_ -> new", pure = true)
static <TEx extends Throwable> @NotNull ThrowingBooleanSupplier<TEx> not(@NotNull ThrowingBooleanSupplier<TEx> target) {
return Objects.requireNonNull(target).negate();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BooleanSupplier addHandler(@NotNull Predicate<Throwable> handler) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull BooleanSupplier addHandler(@NotNull Predicate<Throwable> handler) {
Objects.requireNonNull(handler);
return () -> {
try {
@ -71,8 +71,9 @@ public interface ThrowingBooleanSupplier<TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BooleanSupplier addHandler(@NotNull Class<TEx> exception, @NotNull Predicate<TEx> handler) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull BooleanSupplier addHandler(@NotNull Class<TEx> exception, @NotNull Predicate<TEx> handler) {
Objects.requireNonNull(exception);
Objects.requireNonNull(handler);
return () -> {
try {
@ -85,13 +86,14 @@ public interface ThrowingBooleanSupplier<TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default BooleanSupplier orThrow() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull BooleanSupplier orThrow() {
return orThrow(Try::runtimeException)::get;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingBooleanSupplier<TEx1> orThrow(Function<Throwable, TEx1> generator) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <TEx1 extends Throwable> @NotNull ThrowingBooleanSupplier<TEx1> orThrow(@NotNull Function<Throwable, TEx1> generator) {
Objects.requireNonNull(generator);
return () -> {
try {
return this.get();

View File

@ -12,20 +12,20 @@ import java.util.function.Function;
public interface ThrowingConsumer<T, TEx extends Throwable> {
void accept(T var1) throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingConsumer<V, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingConsumer<V, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (t) -> this.accept(before.apply(t));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingRunnable<TEx> compose(@NotNull ThrowingSupplier<? extends T, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingRunnable<TEx> compose(@NotNull ThrowingSupplier<? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return () -> this.accept(before.get());
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingConsumer<T, TEx> andThen(@NotNull ThrowingConsumer<? super T, ? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingConsumer<T, TEx> andThen(@NotNull ThrowingConsumer<? super T, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> {
this.accept(t);
@ -33,8 +33,8 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingConsumer<T, TEx> andThen(@NotNull ThrowingRunnable<? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingConsumer<T, TEx> andThen(@NotNull ThrowingRunnable<? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> {
this.accept(t);
@ -42,8 +42,8 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingBiConsumer<T, V, TEx> compound(@NotNull ThrowingConsumer<? super V, ? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingBiConsumer<T, V, TEx> compound(@NotNull ThrowingConsumer<? super V, ? extends TEx> other) {
Objects.requireNonNull(other);
return (l, r) -> {
this.accept(l);
@ -51,8 +51,8 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Consumer<T> addHandler(@NotNull Consumer<Throwable> handler) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Consumer<T> addHandler(@NotNull Consumer<Throwable> handler) {
Objects.requireNonNull(handler);
return (t) -> {
try {
@ -63,8 +63,9 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Consumer<T> addHandler(@NotNull Class<TEx> exception, @NotNull Consumer<TEx> handler) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Consumer<T> addHandler(@NotNull Class<TEx> exception, @NotNull Consumer<TEx> handler) {
Objects.requireNonNull(exception);
Objects.requireNonNull(handler);
return (t) -> {
try {
@ -77,13 +78,14 @@ public interface ThrowingConsumer<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Consumer<T> orThrow() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull 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) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <TEx1 extends Throwable> @NotNull ThrowingConsumer<T, TEx1> orThrow(@NotNull Function<Throwable, TEx1> generator) {
Objects.requireNonNull(generator);
return (t) -> {
try {
this.accept(t);

View File

@ -11,32 +11,32 @@ import java.util.function.Function;
public interface ThrowingFunction<T, R, TEx extends Throwable> {
R apply(T var1) throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingSupplier<R, TEx> compose(@NotNull ThrowingSupplier<? extends T, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingSupplier<R, TEx> compose(@NotNull ThrowingSupplier<? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return () -> this.apply(before.get());
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingFunction<V, R, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingFunction<V, R, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (v) -> this.apply(before.apply(v));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingFunction<T, V, TEx> andThen(@NotNull ThrowingFunction<? super R, ? extends V, ? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingFunction<T, V, TEx> andThen(@NotNull ThrowingFunction<? super R, ? extends V, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> after.apply(this.apply(t));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingConsumer<T, TEx> andThen(@NotNull ThrowingConsumer<? super R, ? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingConsumer<T, TEx> andThen(@NotNull ThrowingConsumer<? super R, ? extends TEx> after) {
Objects.requireNonNull(after);
return (t) -> after.accept(this.apply(t));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Function<T, R> addHandler(@NotNull Function<Throwable, ? extends R> handler) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Function<T, R> addHandler(@NotNull Function<Throwable, ? extends R> handler) {
Objects.requireNonNull(handler);
return (t) -> {
try {
@ -47,8 +47,9 @@ public interface ThrowingFunction<T, R, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Function<T, R> addHandler(@NotNull Class<TEx> exception, Function<TEx, ? extends R> handler) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Function<T, R> addHandler(@NotNull Class<TEx> exception, @NotNull Function<TEx, ? extends R> handler) {
Objects.requireNonNull(exception);
Objects.requireNonNull(handler);
return (t) -> {
try {
@ -61,13 +62,14 @@ public interface ThrowingFunction<T, R, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Function<T, R> orThrow() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull 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) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <TEx1 extends Throwable> @NotNull ThrowingFunction<T, R, TEx1> orThrow(@NotNull Function<Throwable, TEx1> generator) {
Objects.requireNonNull(generator);
return (t) -> {
try {
return this.apply(t);

View File

@ -13,71 +13,71 @@ import java.util.function.Predicate;
public interface ThrowingPredicate<T, TEx extends Throwable> {
boolean test(T var1) throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingPredicate<V, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingPredicate<V, TEx> compose(@NotNull ThrowingFunction<? super V, ? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return (t) -> this.test(before.apply(t));
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingBooleanSupplier<TEx> compose(@NotNull ThrowingSupplier<? extends T, ? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingBooleanSupplier<TEx> compose(@NotNull ThrowingSupplier<? extends T, ? extends TEx> before) {
Objects.requireNonNull(before);
return () -> this.test(before.get());
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingPredicate<T, TEx> and(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingPredicate<T, TEx> and(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.test(t) && other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingPredicate<T, TEx> and(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingPredicate<T, TEx> and(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.test(t) && other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingPredicate<T, TEx> or(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingPredicate<T, TEx> or(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.test(t) || other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingPredicate<T, TEx> or(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingPredicate<T, TEx> or(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.test(t) || other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingPredicate<T, TEx> xor(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingPredicate<T, TEx> xor(@NotNull ThrowingPredicate<? super T, ? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.test(t) ^ other.test(t);
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingPredicate<T, TEx> xor(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingPredicate<T, TEx> xor(@NotNull ThrowingBooleanSupplier<? extends TEx> other) {
Objects.requireNonNull(other);
return (t) -> this.test(t) ^ other.get();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingPredicate<T, TEx> negate() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingPredicate<T, TEx> negate() {
return (t) -> !this.test(t);
}
@Contract(pure = true) @NotNull
static <T> ThrowingPredicate<T, Throwable> isEqual(@Nullable Object targetRef) {
@Contract(value = "_ -> new", pure = true)
static <T> @NotNull ThrowingPredicate<T, Throwable> isEqual(@Nullable Object targetRef) {
return null == targetRef ? Objects::isNull : targetRef::equals;
}
@Contract(pure = true) @NotNull
static <T, TEx extends Throwable> ThrowingPredicate<T, TEx> not(@NotNull ThrowingPredicate<T, TEx> target) {
@Contract(value = "_ -> new", pure = true)
static <T, TEx extends Throwable> @NotNull ThrowingPredicate<T, TEx> not(@NotNull ThrowingPredicate<T, TEx> target) {
return Objects.requireNonNull(target).negate();
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Predicate<T> addHandler(@NotNull Predicate<Throwable> handler) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Predicate<T> addHandler(@NotNull Predicate<Throwable> handler) {
Objects.requireNonNull(handler);
return (r) -> {
try {
@ -88,8 +88,9 @@ public interface ThrowingPredicate<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Predicate<T> addHandler(@NotNull Class<TEx> exception, @NotNull Predicate<TEx> handler) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Predicate<T> addHandler(@NotNull Class<TEx> exception, @NotNull Predicate<TEx> handler) {
Objects.requireNonNull(exception);
Objects.requireNonNull(handler);
return (r) -> {
try {
@ -102,13 +103,14 @@ public interface ThrowingPredicate<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Predicate<T> orThrow() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull 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) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <TEx1 extends Throwable> @NotNull ThrowingPredicate<T, TEx1> orThrow(@NotNull Function<Throwable, TEx1> generator) {
Objects.requireNonNull(generator);
return (r) -> {
try {
return this.test(r);

View File

@ -12,8 +12,8 @@ import java.util.function.Function;
public interface ThrowingRunnable<TEx extends Throwable> {
void run() throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingRunnable<TEx> compose(@NotNull ThrowingRunnable<? extends TEx> before) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingRunnable<TEx> compose(@NotNull ThrowingRunnable<? extends TEx> before) {
Objects.requireNonNull(before);
return () -> {
before.run();
@ -21,8 +21,8 @@ public interface ThrowingRunnable<TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingRunnable<TEx> andThen(@NotNull ThrowingRunnable<? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingRunnable<TEx> andThen(@NotNull ThrowingRunnable<? extends TEx> after) {
Objects.requireNonNull(after);
return () -> {
this.run();
@ -30,8 +30,8 @@ public interface ThrowingRunnable<TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Runnable addHandler(@NotNull Consumer<Throwable> handler) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Runnable addHandler(@NotNull Consumer<Throwable> handler) {
Objects.requireNonNull(handler);
return () -> {
try {
@ -42,8 +42,9 @@ public interface ThrowingRunnable<TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Runnable addHandler(@NotNull Class<TEx> exception, @NotNull Consumer<TEx> handler) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Runnable addHandler(@NotNull Class<TEx> exception, @NotNull Consumer<TEx> handler) {
Objects.requireNonNull(exception);
Objects.requireNonNull(handler);
return () -> {
try {
@ -56,13 +57,14 @@ public interface ThrowingRunnable<TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Runnable orThrow() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Runnable orThrow() {
return orThrow(Try::runtimeException)::run;
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <TEx1 extends Throwable> ThrowingRunnable<TEx1> orThrow(Function<Throwable, TEx1> generator) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <TEx1 extends Throwable> @NotNull ThrowingRunnable<TEx1> orThrow(@NotNull Function<Throwable, TEx1> generator) {
Objects.requireNonNull(generator);
return () -> {
try {
this.run();

View File

@ -12,20 +12,20 @@ import java.util.function.Supplier;
public interface ThrowingSupplier<T, TEx extends Throwable> {
T get() throws TEx;
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default <V> ThrowingSupplier<V, TEx> andThen(@NotNull ThrowingFunction<? super T, ? extends V, ? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <V> @NotNull ThrowingSupplier<V, TEx> andThen(@NotNull ThrowingFunction<? super T, ? extends V, ? extends TEx> after) {
Objects.requireNonNull(after);
return () -> after.apply(this.get());
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default ThrowingRunnable<TEx> andThen(@NotNull ThrowingConsumer<? super T, ? extends TEx> after) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull ThrowingRunnable<TEx> andThen(@NotNull ThrowingConsumer<? super T, ? extends TEx> after) {
Objects.requireNonNull(after);
return () -> after.accept(this.get());
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Supplier<T> addHandler(@NotNull Function<Throwable, ? extends T> handler) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Supplier<T> addHandler(@NotNull Function<Throwable, ? extends T> handler) {
Objects.requireNonNull(handler);
return () -> {
try {
@ -36,8 +36,9 @@ public interface ThrowingSupplier<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Supplier<T> addHandler(@NotNull Class<TEx> exception, @NotNull Function<TEx, ? extends T> handler) {
@Contract(value = "_, _ -> new", pure = true) @ApiStatus.NonExtendable
default @NotNull Supplier<T> addHandler(@NotNull Class<TEx> exception, @NotNull Function<TEx, ? extends T> handler) {
Objects.requireNonNull(exception);
Objects.requireNonNull(handler);
return () -> {
try {
@ -50,13 +51,14 @@ public interface ThrowingSupplier<T, TEx extends Throwable> {
};
}
@Contract(pure = true) @NotNull @ApiStatus.NonExtendable
default Supplier<T> orThrow() {
@Contract(value = "-> new", pure = true) @ApiStatus.NonExtendable
default @NotNull 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) {
@Contract(value = "_ -> new", pure = true) @ApiStatus.NonExtendable
default <TEx1 extends Throwable> @NotNull ThrowingSupplier<T, TEx1> orThrow(@NotNull Function<Throwable, TEx1> generator) {
Objects.requireNonNull(generator);
return () -> {
try {
return this.get();

View File

@ -16,7 +16,6 @@ public class Try {
* @param tr The method that should be run
* @param alternative A method to run if tr fails
*/
@Contract(pure = true)
public static void orElse(@NotNull ThrowingRunnable<?> tr, @NotNull Consumer<Throwable> alternative) {
Objects.requireNonNull(tr).addHandler(alternative).run();
}
@ -26,8 +25,7 @@ public class Try {
* @param tr The method that should be run
* @param alternative A method to run if tr fails, should return a default value
*/
@Contract(pure = true) @NotNull
public static <T> T orElse(@NotNull ThrowingSupplier<T, ?> tr, @NotNull Function<Throwable, ? extends T> alternative) {
public static <T> @NotNull T orElse(@NotNull ThrowingSupplier<T, ?> tr, @NotNull Function<Throwable, ? extends T> alternative) {
return Objects.requireNonNull(tr).addHandler(alternative).get();
}
@ -35,7 +33,6 @@ public class Try {
* Run a method or throw a runtime exception (aka just ignore that it might throw something)
* @param tr The method that should be run
*/
@Contract(pure = true)
public static void orThrow(@NotNull ThrowingRunnable<?> tr) {
Objects.requireNonNull(tr).orThrow().run();
}
@ -44,8 +41,7 @@ public class Try {
* Run a method or throw a runtime exception (aka just ignore that it might throw something)
* @param tr The method that should be run
*/
@Contract(pure = true) @NotNull
public static <T> T orThrow(@NotNull ThrowingSupplier<T, ?> tr) {
public static <T> @NotNull T orThrow(@NotNull ThrowingSupplier<T, ?> tr) {
return Objects.requireNonNull(tr).orThrow().get();
}
@ -55,8 +51,8 @@ public class Try {
* @param handler A method to run if tr fails
* @return A normal BiConsumer
*/
@Contract(pure = true) @NotNull
public static <T, U> BiConsumer<T, U> handle(@NotNull ThrowingBiConsumer<T, U, ?> tr, @NotNull Consumer<Throwable> handler) {
@Contract(pure = true)
public static <T, U> @NotNull BiConsumer<T, U> handle(@NotNull ThrowingBiConsumer<T, U, ?> tr, @NotNull Consumer<Throwable> handler) {
return Objects.requireNonNull(tr).addHandler(handler);
}
@ -66,8 +62,8 @@ public class Try {
* @param handler A method to run if tr fails, should return a default value
* @return A normal BiFunction
*/
@Contract(pure = true) @NotNull
public static <T, U, R> BiFunction<T, U, R> handle(@NotNull ThrowingBiFunction<T, U, R, ?> tr, @NotNull Function<Throwable, ? extends R> handler) {
@Contract(pure = true)
public static <T, U, R> @NotNull BiFunction<T, U, R> handle(@NotNull ThrowingBiFunction<T, U, R, ?> tr, @NotNull Function<Throwable, ? extends R> handler) {
return Objects.requireNonNull(tr).addHandler(handler);
}
@ -77,8 +73,8 @@ public class Try {
* @param handler A method to run if tr fails
* @return A normal Consumer
*/
@Contract(pure = true) @NotNull
public static <T> Consumer<T> handle(@NotNull ThrowingConsumer<T, ?> tr, @NotNull Consumer<Throwable> handler) {
@Contract(pure = true)
public static <T> @NotNull Consumer<T> handle(@NotNull ThrowingConsumer<T, ?> tr, @NotNull Consumer<Throwable> handler) {
return Objects.requireNonNull(tr).addHandler(handler);
}
@ -88,8 +84,8 @@ public class Try {
* @param handler A method to run if tr fails, should return a default value
* @return A normal Function
*/
@Contract(pure = true) @NotNull
public static <T, R> Function<T, R> handle(@NotNull ThrowingFunction<T, R, ?> tr, @NotNull Function<Throwable, ? extends R> handler) {
@Contract(pure = true)
public static <T, R> @NotNull Function<T, R> handle(@NotNull ThrowingFunction<T, R, ?> tr, @NotNull Function<Throwable, ? extends R> handler) {
return Objects.requireNonNull(tr).addHandler(handler);
}
@ -99,8 +95,8 @@ public class Try {
* @param handler A method to run if tr fails
* @return A normal Runnable
*/
@Contract(pure = true) @NotNull
public static Runnable handle(@NotNull ThrowingRunnable<?> tr, @NotNull Consumer<Throwable> handler) {
@Contract(pure = true)
public static @NotNull Runnable handle(@NotNull ThrowingRunnable<?> tr, @NotNull Consumer<Throwable> handler) {
return Objects.requireNonNull(tr).addHandler(handler);
}
@ -110,8 +106,8 @@ public class Try {
* @param handler A method to run if tr fails, should return a default value
* @return A normal Supplier
*/
@Contract(pure = true) @NotNull
public static <T> Supplier<T> handle(@NotNull ThrowingSupplier<T, ?> tr, @NotNull Function<Throwable, ? extends T> handler) {
@Contract(pure = true)
public static <T> @NotNull Supplier<T> handle(@NotNull ThrowingSupplier<T, ?> tr, @NotNull Function<Throwable, ? extends T> handler) {
return Objects.requireNonNull(tr).addHandler(handler);
}
@ -120,8 +116,8 @@ public class Try {
* @param t A throwable to convert
* @return A runtime exception
*/
@Contract(pure = true) @NotNull
protected static RuntimeException runtimeException(@NotNull Throwable t) {
@Contract(pure = true)
protected static @NotNull RuntimeException runtimeException(@NotNull Throwable t) {
Objects.requireNonNull(t);
return t instanceof RuntimeException e ? e : new RuntimeException(t);
}

View File

@ -8,38 +8,38 @@ import java.util.Objects;
import java.util.function.Function;
public record Quadruple<T1, T2, T3, T4>(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3, @Nullable T4 val4) {
@Contract(pure = true) @NotNull
public static <T1, T2, T3, T4> Quadruple<T1, T2, T3, T4> of(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3, @Nullable T4 val4) {
@Contract(value = "_, _, _, _ -> new", pure = true)
public static <T1, T2, T3, T4> @NotNull Quadruple<T1, T2, T3, T4> of(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3, @Nullable T4 val4) {
return new Quadruple<>(val1, val2, val3, val4);
}
@Contract(pure = true) @NotNull
public static <T1, T2, T3, T4> Quadruple<T1, T2, T3, T4> of(@NotNull Tuple<T1, T2> left, @NotNull Tuple<T3, T4> right) {
@Contract(value = "_, _ -> new", pure = true)
public static <T1, T2, T3, T4> @NotNull Quadruple<T1, T2, T3, T4> of(@NotNull Tuple<T1, T2> left, @NotNull Tuple<T3, T4> right) {
return new Quadruple<>(left.left(), left.right(), right.left(), right.right());
}
@Contract(pure = true) @NotNull
public Tuple<Tuple<T1, T2>, Tuple<T3, T4>> slice() {
@Contract(value = "-> new", pure = true)
public @NotNull Tuple<Tuple<T1, T2>, Tuple<T3, T4>> slice() {
return new Tuple<>(new Tuple<>(val1, val2), new Tuple<>(val3, val4));
}
@Contract(pure = true) @NotNull
public <T> Quadruple<T, T2, T3, T4> map1(@NotNull Function<T1, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Quadruple<T, T2, T3, T4> map1(@NotNull Function<T1, T> mapper) {
return new Quadruple<>(Objects.requireNonNull(mapper).apply(val1), val2, val3, val4);
}
@Contract(pure = true) @NotNull
public <T> Quadruple<T1, T, T3, T4> map2(@NotNull Function<T2, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Quadruple<T1, T, T3, T4> map2(@NotNull Function<T2, T> mapper) {
return new Quadruple<>(val1, Objects.requireNonNull(mapper).apply(val2), val3, val4);
}
@Contract(pure = true) @NotNull
public <T> Quadruple<T1, T2, T, T4> map3(@NotNull Function<T3, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Quadruple<T1, T2, T, T4> map3(@NotNull Function<T3, T> mapper) {
return new Quadruple<>(val1, val2, Objects.requireNonNull(mapper).apply(val3), val4);
}
@Contract(pure = true) @NotNull
public <T> Quadruple<T1, T2, T3, T> map4(@NotNull Function<T4, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Quadruple<T1, T2, T3, T> map4(@NotNull Function<T4, T> mapper) {
return new Quadruple<>(val1, val2, val3, Objects.requireNonNull(mapper).apply(val4));
}
}

View File

@ -1,17 +1,17 @@
package io.gitlab.jfronny.commons.tuple;
import io.gitlab.jfronny.commons.throwable.ThrowingPredicate;
import io.gitlab.jfronny.commons.throwable.ThrowingSupplier;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public record Single<T1>(@Nullable T1 val) {
@Contract(pure = true) @NotNull
public static <T1> Single<T1> of(@Nullable T1 val) {
@Contract(value = "_ -> new", pure = true)
public static <T1> @NotNull Single<T1> of(@Nullable T1 val) {
return new Single<>(val);
}
@ -25,28 +25,28 @@ public record Single<T1>(@Nullable T1 val) {
return val != null;
}
@Contract(pure = true) @NotNull
public Predicate<T1> asEqualsPredicate() {
@Contract(value = "-> new", pure = true)
public @NotNull ThrowingPredicate<T1, RuntimeException> asEqualsPredicate() {
return val == null ? Objects::isNull : val::equals;
}
@Contract(pure = true) @NotNull
public Predicate<T1> asInstanceEqualsPredicate() {
@Contract(value = "-> new", pure = true)
public @NotNull ThrowingPredicate<T1, RuntimeException> asInstanceEqualsPredicate() {
return v -> v == val;
}
@Contract(pure = true) @NotNull
public Supplier<T1> asSupplier() {
@Contract(value = "-> new", pure = true)
public @NotNull ThrowingSupplier<T1, RuntimeException> asSupplier() {
return () -> val;
}
@Contract(pure = true) @NotNull
public <T> Single<T> map(@NotNull Function<T1, T> mapper) {
@Contract(value = "_, -> new", pure = true)
public <T> @NotNull Single<T> map(@NotNull Function<T1, T> mapper) {
return new Single<>(Objects.requireNonNull(mapper).apply(val));
}
@Contract(pure = true) @Nullable
public T1 get() {
@Contract(pure = true)
public @Nullable T1 get() {
return val;
}
}

View File

@ -8,48 +8,48 @@ import java.util.Objects;
import java.util.function.Function;
public record Triple<T1, T2, T3>(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3) {
@Contract(pure = true) @NotNull
public static <T1, T2, T3> Triple<T1, T2, T3> of(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3) {
@Contract(value = "_, _, _ -> new", pure = true)
public static <T1, T2, T3> @NotNull Triple<T1, T2, T3> of(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3) {
return new Triple<>(val1, val2, val3);
}
@Contract(pure = true) @NotNull
public <T> Triple<T, T2, T3> map1(@NotNull Function<T1, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Triple<T, T2, T3> map1(@NotNull Function<T1, T> mapper) {
return new Triple<>(Objects.requireNonNull(mapper).apply(val1), val2, val3);
}
@Contract(pure = true) @NotNull
public <T> Triple<T1, T, T3> map2(@NotNull Function<T2, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Triple<T1, T, T3> map2(@NotNull Function<T2, T> mapper) {
return new Triple<>(val1, Objects.requireNonNull(mapper).apply(val2), val3);
}
@Contract(pure = true) @NotNull
public <T> Triple<T1, T2, T> map3(@NotNull Function<T3, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Triple<T1, T2, T> map3(@NotNull Function<T3, T> mapper) {
return new Triple<>(val1, val2, Objects.requireNonNull(mapper).apply(val3));
}
@Contract(pure = true) @NotNull
public Triple<T2, T3, T1> lShift() {
@Contract(value = "-> new", pure = true)
public @NotNull Triple<T2, T3, T1> lShift() {
return new Triple<>(val2, val3, val1);
}
@Contract(pure = true) @NotNull
public Triple<T3, T1, T2> rShift() {
@Contract(value = "-> new", pure = true)
public @NotNull Triple<T3, T1, T2> rShift() {
return new Triple<>(val3, val1, val2);
}
@Contract(pure = true) @NotNull
public Triple<T2, T1, T3> swapL() {
@Contract(value = "-> new", pure = true)
public @NotNull Triple<T2, T1, T3> swapL() {
return new Triple<>(val2, val1, val3);
}
@Contract(pure = true) @NotNull
public Triple<T1, T3, T2> swapR() {
@Contract(value = "-> new", pure = true)
public @NotNull Triple<T1, T3, T2> swapR() {
return new Triple<>(val1, val3, val2);
}
@Contract(pure = true) @NotNull
public Triple<T3, T2, T1> reverse() {
@Contract(value = "-> new", pure = true)
public @NotNull Triple<T3, T2, T1> reverse() {
return new Triple<>(val3, val2, val1);
}
}

View File

@ -8,49 +8,49 @@ import java.util.*;
import java.util.function.Function;
public record Tuple<T1, T2>(@Nullable T1 left, @Nullable T2 right) {
@Contract(pure = true) @NotNull
public static <T1> Single<T1> of(@Nullable T1 val) {
@Contract(value = "_ -> new", pure = true)
public static <T1> @NotNull Single<T1> of(@Nullable T1 val) {
return new Single<>(val);
}
@Contract(pure = true) @NotNull
public static <T1, T2> Tuple<T1, T2> of(@Nullable T1 left, @Nullable T2 right) {
@Contract(value = "_, _ -> new", pure = true)
public static <T1, T2> @NotNull Tuple<T1, T2> of(@Nullable T1 left, @Nullable T2 right) {
return new Tuple<>(left, right);
}
@Contract(pure = true) @NotNull
public static <T1, T2, T3> Triple<T1, T2, T3> of(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3) {
@Contract(value = "_, _, _ -> new", pure = true)
public static <T1, T2, T3> @NotNull Triple<T1, T2, T3> of(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3) {
return new Triple<>(val1, val2, val3);
}
@Contract(pure = true) @NotNull
public static <T1, T2, T3, T4> Quadruple<T1, T2, T3, T4> of(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3, @Nullable T4 val4) {
@Contract(value = "_, _, _, _ -> new", pure = true)
public static <T1, T2, T3, T4> @NotNull Quadruple<T1, T2, T3, T4> of(@Nullable T1 val1, @Nullable T2 val2, @Nullable T3 val3, @Nullable T4 val4) {
return new Quadruple<>(val1, val2, val3, val4);
}
@Contract(pure = true) @NotNull
public static <T1, T2> Tuple<T1, T2> from(@NotNull Map.Entry<T1, T2> entry) {
@Contract(value = "_ -> new", pure = true)
public static <T1, T2> @NotNull Tuple<T1, T2> from(@NotNull Map.Entry<T1, T2> entry) {
Objects.requireNonNull(entry);
return new Tuple<>(entry.getKey(), entry.getValue());
}
@Contract(pure = true) @NotNull
public <T> Tuple<T, T2> mapLeft(@NotNull Function<T1, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Tuple<T, T2> mapLeft(@NotNull Function<T1, T> mapper) {
return new Tuple<>(Objects.requireNonNull(mapper).apply(left), right);
}
@Contract(pure = true) @NotNull
public <T> Tuple<T1, T> mapRight(@NotNull Function<T2, T> mapper) {
@Contract(value = "_ -> new", pure = true)
public <T> @NotNull Tuple<T1, T> mapRight(@NotNull Function<T2, T> mapper) {
return new Tuple<>(left, Objects.requireNonNull(mapper).apply(right));
}
@Contract(pure = true) @NotNull
public Tuple<T2, T1> swap() {
@Contract(value = "-> new", pure = true)
public @NotNull Tuple<T2, T1> swap() {
return new Tuple<>(right, left);
}
@Contract(pure = true)
public static <T1, T2> Set<Tuple<T1, T2>> from(@NotNull Map<T1, T2> map) {
@Contract(value = "_ -> new", pure = true)
public static <T1, T2> @NotNull Set<Tuple<T1, T2>> from(@NotNull Map<T1, T2> map) {
Objects.requireNonNull(map);
return new AbstractSet<>() {
@Override
@ -74,15 +74,13 @@ public record Tuple<T1, T2>(@Nullable T1 left, @Nullable T2 right) {
return false;
}
@NotNull
@Override
public Iterator<Tuple<T1, T2>> iterator() {
public @NotNull Iterator<Tuple<T1, T2>> iterator() {
return map.entrySet().stream().map(Tuple::from).iterator();
}
@NotNull
@Override
public Object[] toArray() {
public @NotNull Object @NotNull [] toArray() {
return map.entrySet().stream().map(Tuple::from).toArray();
}