move generic utils to util project

This commit is contained in:
dfsek
2021-09-19 11:30:49 -07:00
parent 554369be31
commit 33ab4c5bfd
5 changed files with 0 additions and 0 deletions

View File

@@ -1,10 +0,0 @@
package com.dfsek.terra.api.util.generic;
import java.util.function.Supplier;
public final class Construct {
public static <T> T construct(Supplier<T> in) {
return in.get();
}
}

View File

@@ -1,26 +0,0 @@
package com.dfsek.terra.api.util.generic;
import java.util.function.Supplier;
public final class Lazy<T> {
private final Supplier<T> valueSupplier;
private T value;
private boolean got = false;
private Lazy(Supplier<T> valueSupplier) {
this.valueSupplier = valueSupplier;
}
public static <T> Lazy<T> lazy(Supplier<T> valueSupplier) {
return new Lazy<>(valueSupplier);
}
public T value() {
if(!got && value == null) {
got = true;
value = valueSupplier.get();
}
return value;
}
}

View File

@@ -1,82 +0,0 @@
package com.dfsek.terra.api.util.generic.either;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
public final class Either<L, R> {
private final L left;
private final R right;
private final boolean leftPresent;
private Either(L left, R right, boolean leftPresent) {
this.left = left;
this.right = right;
this.leftPresent = leftPresent;
}
@NotNull
@Contract("_ -> new")
public static <L1, R1> Either<L1, R1> left(L1 left) {
return new Either<>(Objects.requireNonNull(left), null, true);
}
@NotNull
@Contract("_ -> new")
public static <L1, R1> Either<L1, R1> right(R1 right) {
return new Either<>(null, Objects.requireNonNull(right), false);
}
@NotNull
@Contract("_ -> this")
public Either<L, R> ifLeft(Consumer<L> action) {
if(leftPresent) action.accept(left);
return this;
}
@NotNull
@Contract("_ -> this")
public Either<L, R> ifRight(Consumer<R> action) {
if(!leftPresent) action.accept(right);
return this;
}
@NotNull
public Optional<L> getLeft() {
if(leftPresent) return Optional.of(left);
return Optional.empty();
}
@NotNull
public Optional<R> getRight() {
if(!leftPresent) return Optional.of(right);
return Optional.empty();
}
public boolean hasLeft() {
return leftPresent;
}
public boolean hasRight() {
return !leftPresent;
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Either)) return false;
Either<?, ?> that = (Either<?, ?>) obj;
return (this.leftPresent && that.leftPresent && Objects.equals(this.left, that.left))
|| (!this.leftPresent && !that.leftPresent && Objects.equals(this.right, that.right));
}
}

View File

@@ -1,56 +0,0 @@
package com.dfsek.terra.api.util.generic.pair;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public final class ImmutablePair<L, R> {
private static final ImmutablePair<?, ?> NULL = new ImmutablePair<>(null, null);
private final L left;
private final R right;
private ImmutablePair(L left, R right) {
this.left = left;
this.right = right;
}
@Contract("_, _ -> new")
public static <L1, R1> ImmutablePair<L1, R1> of(L1 left, R1 right) {
return new ImmutablePair<>(left, right);
}
@Contract("-> new")
@SuppressWarnings("unchecked")
public static <L1, R1> ImmutablePair<L1, R1> ofNull() {
return (ImmutablePair<L1, R1>) NULL;
}
@NotNull
@Contract("-> new")
public Pair<L, R> mutable() {
return Pair.of(left, right);
}
public R getRight() {
return right;
}
public L getLeft() {
return left;
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof ImmutablePair)) return false;
ImmutablePair<?, ?> that = (ImmutablePair<?, ?>) obj;
return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right);
}
}

View File

@@ -1,57 +0,0 @@
package com.dfsek.terra.api.util.generic.pair;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class Pair<L, R> {
private L left;
private R right;
private Pair(L left, R right) {
this.left = left;
this.right = right;
}
@NotNull
@Contract("_, _ -> new")
public static <L1, R1> Pair<L1, R1> of(L1 left, R1 right) {
return new Pair<>(left, right);
}
@Contract("-> new")
public ImmutablePair<L, R> immutable() {
return ImmutablePair.of(left, right);
}
public L getLeft() {
return left;
}
public void setLeft(L left) {
this.left = left;
}
public R getRight() {
return right;
}
public void setRight(R right) {
this.right = right;
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Pair)) return false;
Pair<?, ?> that = (Pair<?, ?>) obj;
return Objects.equals(this.left, that.left) && Objects.equals(this.right, that.right);
}
}