mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-10 17:56:03 +00:00
clean up and annotate generic utils
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
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;
|
||||
|
||||
@@ -14,29 +18,39 @@ public final class Either<L, R> {
|
||||
this.leftPresent = leftPresent;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Contract("_ -> new")
|
||||
public static <L1, R1> Either<L1, R1> left(L1 left) {
|
||||
return new Either<>(left, null, true);
|
||||
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, right, false);
|
||||
return new Either<>(null, Objects.requireNonNull(right), false);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@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;
|
||||
@@ -49,4 +63,19 @@ public final class Either<L, R> {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
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 final L left;
|
||||
private final R right;
|
||||
@@ -11,6 +16,7 @@ public final class ImmutablePair<L, R> {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Contract("_, _ -> new")
|
||||
public static <L1, R1> ImmutablePair<L1, R1> of(L1 left, R1 right) {
|
||||
return new ImmutablePair<>(left, right);
|
||||
}
|
||||
@@ -23,12 +29,28 @@ public final class ImmutablePair<L, R> {
|
||||
return left;
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(left, right);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
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;
|
||||
@@ -9,6 +14,8 @@ public class Pair<L, R> {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Contract("_, _ -> new")
|
||||
public static <L1, R1> Pair<L1, R1> of(L1 left, R1 right) {
|
||||
return new Pair<>(left, right);
|
||||
}
|
||||
@@ -29,7 +36,21 @@ public class Pair<L, R> {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Contract("-> new")
|
||||
public ImmutablePair<L, R> immutable() {
|
||||
return ImmutablePair.of(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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(left, right);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user