clean up and annotate generic utils

This commit is contained in:
dfsek
2021-07-07 20:38:16 -07:00
parent 6a1cc16dc3
commit e1abc67989
3 changed files with 74 additions and 2 deletions
@@ -1,5 +1,9 @@
package com.dfsek.terra.api.util.generic.either; 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.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
@@ -14,29 +18,39 @@ public final class Either<L, R> {
this.leftPresent = leftPresent; this.leftPresent = leftPresent;
} }
@NotNull
@Contract("_ -> new")
public static <L1, R1> Either<L1, R1> left(L1 left) { 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) { 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() { public Optional<L> getLeft() {
if(leftPresent) return Optional.of(left); if(leftPresent) return Optional.of(left);
return Optional.empty(); return Optional.empty();
} }
@NotNull
public Optional<R> getRight() { public Optional<R> getRight() {
if(!leftPresent) return Optional.of(right); if(!leftPresent) return Optional.of(right);
return Optional.empty(); return Optional.empty();
} }
@NotNull
@Contract("_ -> this")
public Either<L, R> ifLeft(Consumer<L> action) { public Either<L, R> ifLeft(Consumer<L> action) {
if(leftPresent) action.accept(left); if(leftPresent) action.accept(left);
return this; return this;
} }
@NotNull
@Contract("_ -> this")
public Either<L, R> ifRight(Consumer<R> action) { public Either<L, R> ifRight(Consumer<R> action) {
if(!leftPresent) action.accept(right); if(!leftPresent) action.accept(right);
return this; return this;
@@ -49,4 +63,19 @@ public final class Either<L, R> {
public boolean hasRight() { public boolean hasRight() {
return !leftPresent; 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; 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> { public final class ImmutablePair<L, R> {
private final L left; private final L left;
private final R right; private final R right;
@@ -11,6 +16,7 @@ public final class ImmutablePair<L, R> {
this.right = right; this.right = right;
} }
@Contract("_, _ -> new")
public static <L1, R1> ImmutablePair<L1, R1> of(L1 left, R1 right) { public static <L1, R1> ImmutablePair<L1, R1> of(L1 left, R1 right) {
return new ImmutablePair<>(left, right); return new ImmutablePair<>(left, right);
} }
@@ -23,12 +29,28 @@ public final class ImmutablePair<L, R> {
return left; return left;
} }
@Contract("-> new")
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <L1, R1> ImmutablePair<L1, R1> ofNull() { public static <L1, R1> ImmutablePair<L1, R1> ofNull() {
return (ImmutablePair<L1, R1>) NULL; return (ImmutablePair<L1, R1>) NULL;
} }
@NotNull
@Contract("-> new")
public Pair<L, R> mutable() { public Pair<L, R> mutable() {
return Pair.of(left, right); 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; 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> { public class Pair<L, R> {
private L left; private L left;
private R right; private R right;
@@ -9,6 +14,8 @@ public class Pair<L, R> {
this.right = right; this.right = right;
} }
@NotNull
@Contract("_, _ -> new")
public static <L1, R1> Pair<L1, R1> of(L1 left, R1 right) { public static <L1, R1> Pair<L1, R1> of(L1 left, R1 right) {
return new Pair<>(left, right); return new Pair<>(left, right);
} }
@@ -29,7 +36,21 @@ public class Pair<L, R> {
this.right = right; this.right = right;
} }
@Contract("-> new")
public ImmutablePair<L, R> immutable() { public ImmutablePair<L, R> immutable() {
return ImmutablePair.of(left, right); 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);
}
} }