mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-11 18:26:08 +00:00
convert a bunch of stuff to new APIs
This commit is contained in:
@@ -7,17 +7,16 @@
|
||||
|
||||
package com.dfsek.terra.api.command;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import com.dfsek.terra.api.Handle;
|
||||
import com.dfsek.terra.api.entity.Entity;
|
||||
import com.dfsek.terra.api.entity.Player;
|
||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||
|
||||
|
||||
public interface CommandSender extends Handle {
|
||||
void sendMessage(String message);
|
||||
|
||||
Optional<Entity> getEntity();
|
||||
Maybe<Entity> getEntity();
|
||||
|
||||
Optional<Player> getPlayer();
|
||||
Maybe<Player> getPlayer();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.dfsek.terra.api.command.arguments;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||
|
||||
import io.leangen.geantyref.TypeToken;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.incendo.cloud.component.CommandComponent;
|
||||
@@ -100,20 +102,16 @@ public class RegistryArgument {
|
||||
|
||||
Registry<R> registry = registryFunction.apply(commandContext);
|
||||
|
||||
Optional<R> result;
|
||||
try {
|
||||
result = registry.get(RegistryKey.parse(input));
|
||||
} catch(IllegalArgumentException e) {
|
||||
try {
|
||||
result = registry.getByID(input);
|
||||
} catch(IllegalArgumentException e1) {
|
||||
return ArgumentParseResult.failure(e1);
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
String finalInput = input;
|
||||
return registry.get(RegistryKey.parse(input))
|
||||
.map(ArgumentParseResult::success)
|
||||
.orElse(ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + input)));
|
||||
.orJust(() ->
|
||||
registry.getByID(finalInput).collect(
|
||||
left -> ArgumentParseResult.failure(new IllegalArgumentException(left)),
|
||||
ArgumentParseResult::success
|
||||
))
|
||||
.get(() -> ArgumentParseResult.failure(new NoSuchEntryException("No such entry: " + finalInput)));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
package com.dfsek.terra.api.registry;
|
||||
|
||||
import com.dfsek.tectonic.api.loader.type.TypeLoader;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.data.types.Either;
|
||||
import com.dfsek.terra.api.util.generic.data.types.Maybe;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -32,7 +36,7 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
* @return Value matching the identifier, {@code null} if no value is present.
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
Optional<T> get(@NotNull RegistryKey key);
|
||||
Maybe<T> get(@NotNull RegistryKey key);
|
||||
|
||||
/**
|
||||
* Check if the registry contains a value.
|
||||
@@ -82,13 +86,13 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
return getType().getRawType();
|
||||
}
|
||||
|
||||
default Optional<T> getByID(String id) {
|
||||
default Either<String, T> getByID(String id) {
|
||||
return getByID(id, map -> {
|
||||
if(map.isEmpty()) return Optional.empty();
|
||||
if(map.isEmpty()) return Either.left("No such element.");
|
||||
if(map.size() == 1) {
|
||||
return map.values().stream().findFirst(); // only one value.
|
||||
return Either.right(map.values().stream().findFirst().get()); // only one value.
|
||||
}
|
||||
throw new IllegalArgumentException("ID \"" + id + "\" is ambiguous; matches: " + map
|
||||
return Either.<String, T>left("ID \"" + id + "\" is ambiguous; matches: " + map
|
||||
.keySet()
|
||||
.stream()
|
||||
.map(RegistryKey::toString)
|
||||
@@ -102,9 +106,9 @@ public interface Registry<T> extends TypeLoader<T> {
|
||||
|
||||
Map<RegistryKey, T> getMatches(String id);
|
||||
|
||||
default Optional<T> getByID(String attempt, Function<Map<RegistryKey, T>, Optional<T>> reduction) {
|
||||
default Either<String, T> getByID(String attempt, Function<Map<RegistryKey, T>, Either<String, T>> reduction) {
|
||||
if(attempt.contains(":")) {
|
||||
return get(RegistryKey.parse(attempt));
|
||||
return get(RegistryKey.parse(attempt)).toEither("No such value.");
|
||||
}
|
||||
return reduction.apply(getMatches(attempt));
|
||||
}
|
||||
|
||||
@@ -26,6 +26,15 @@ public final class FunctionUtils {
|
||||
return either.collect(Function.identity(), Function.identity());
|
||||
}
|
||||
|
||||
public static <T extends Throwable, U> U throw_(T e) throws T {
|
||||
throw e;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <E extends Throwable, U> U sneakyThrow(Throwable e) throws E {
|
||||
throw (E) e;
|
||||
}
|
||||
|
||||
public static <T, U> Function<T, Either<Exception, U>> liftTry(Function<T, U> f) {
|
||||
return s -> {
|
||||
try {
|
||||
|
||||
@@ -51,9 +51,9 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
@Override
|
||||
<R1> Either<L, R1> mapRight(Function<R, R1> f);
|
||||
|
||||
Optional<L> getLeft();
|
||||
Maybe<L> getLeft();
|
||||
|
||||
Optional<R> getRight();
|
||||
Maybe<R> getRight();
|
||||
|
||||
boolean isLeft();
|
||||
|
||||
@@ -63,6 +63,11 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
|
||||
<U> U collect(Function<L, U> left, Function<R, U> right);
|
||||
|
||||
@SuppressWarnings("Convert2MethodRef")
|
||||
default <T extends Throwable> R collectThrow(Function<L, T> left) throws T{
|
||||
return mapLeft(left).collect(l -> FunctionUtils.sneakyThrow(l), Function.identity());
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked" })
|
||||
@NotNull
|
||||
@Contract("_ -> new")
|
||||
@@ -87,13 +92,13 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<L> getLeft() {
|
||||
return Optional.of(value);
|
||||
public Maybe<L> getLeft() {
|
||||
return Maybe.just(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<R> getRight() {
|
||||
return Optional.empty();
|
||||
public Maybe<R> getRight() {
|
||||
return Maybe.nothing();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -142,13 +147,13 @@ public interface Either<L, R> extends Monad<R, Either<?, ?>>, BiFunctor<L, R, Ei
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<L> getLeft() {
|
||||
return Optional.empty();
|
||||
public Maybe<L> getLeft() {
|
||||
return Maybe.nothing();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<R> getRight() {
|
||||
return Optional.of(value);
|
||||
public Maybe<R> getRight() {
|
||||
return Maybe.just(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -2,7 +2,9 @@ package com.dfsek.terra.api.util.generic.data.types;
|
||||
|
||||
import com.dfsek.terra.api.util.generic.control.Monad;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
@@ -21,6 +23,7 @@ public interface Maybe<T> extends Monad<T, Maybe<?>> {
|
||||
Optional<T> toOptional();
|
||||
|
||||
<L> Either<L, T> toEither(L l);
|
||||
|
||||
T get(Supplier<T> def);
|
||||
|
||||
boolean isJust();
|
||||
@@ -32,12 +35,45 @@ public interface Maybe<T> extends Monad<T, Maybe<?>> {
|
||||
return get(() -> def);
|
||||
}
|
||||
|
||||
default Maybe<T> consume(Consumer<T> c) {
|
||||
return map(m -> {
|
||||
c.accept(m);
|
||||
return m;
|
||||
});
|
||||
}
|
||||
|
||||
default Maybe<T> ifNothing(Runnable r) {
|
||||
if(!isJust()) {
|
||||
r.run();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
default <T1> Maybe<T1> overwrite(Maybe<T1> m) {
|
||||
return bind(ignore -> m);
|
||||
}
|
||||
|
||||
Maybe<T> or(Supplier<Maybe<T>> or);
|
||||
|
||||
default Maybe<T> orJust(Supplier<T> or) {
|
||||
return or(() -> just(or.get()));
|
||||
}
|
||||
|
||||
|
||||
@Deprecated
|
||||
default <X extends Throwable> T orThrow() {
|
||||
return get(() -> { throw new NoSuchElementException("No value present."); });
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
default <X extends Throwable> T orThrow(Supplier<X> e) throws X {
|
||||
if(isJust()) {
|
||||
return orThrow();
|
||||
}
|
||||
throw e.get();
|
||||
}
|
||||
|
||||
|
||||
default Maybe<T> or(Maybe<T> or) {
|
||||
return or(() -> or);
|
||||
}
|
||||
@@ -54,6 +90,11 @@ public interface Maybe<T> extends Monad<T, Maybe<?>> {
|
||||
return op.map(Maybe::just).orElseGet(Maybe::nothing);
|
||||
}
|
||||
|
||||
static <T> Maybe<T> ofNullable(T t) {
|
||||
if(t == null) return nothing();
|
||||
return just(t);
|
||||
}
|
||||
|
||||
static <T1> Maybe<T1> just(T1 t) {
|
||||
record Just<T>(T value) implements Maybe<T> {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user