equals and hashcode for Either and Pair

This commit is contained in:
dfsek
2021-04-22 11:57:14 -07:00
parent 56fb510d96
commit 92339c904d
3 changed files with 44 additions and 0 deletions

View File

@@ -49,4 +49,21 @@ public final class Either<L, R> {
public boolean hasRight() {
return !leftPresent;
}
@Override
public int hashCode() {
if(hasLeft()) return left.hashCode();
return right.hashCode();
}
@Override
public boolean equals(Object o) {
if(!(o instanceof Either<?, ?>)) return false;
Either<?, ?> that = (Either<?, ?>) o;
if(hasLeft() && that.hasLeft()) return left.equals(that.left);
if(hasRight() && that.hasRight()) return right.equals(that.right);
return false;
}
}

View File

@@ -1,5 +1,7 @@
package com.dfsek.terra.api.util.generic.pair;
import java.util.Objects;
public class ImmutablePair<L, R> {
private final L left;
private final R right;
@@ -24,4 +26,15 @@ public class ImmutablePair<L, R> {
public Pair<L, R> mutable() {
return new Pair<>(left, right);
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object o) {
if(!(o instanceof ImmutablePair)) return false;
ImmutablePair<?, ?> that = (ImmutablePair<?, ?>) o;
return that.left.equals(left) && that.right.equals(right);
}
}

View File

@@ -1,5 +1,7 @@
package com.dfsek.terra.api.util.generic.pair;
import java.util.Objects;
public class Pair<L, R> {
private L left;
private R right;
@@ -32,4 +34,16 @@ public class Pair<L, R> {
public ImmutablePair<L, R> immutable() {
return new ImmutablePair<>(left, right);
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object o) {
if(!(o instanceof Pair)) return false;
Pair<?, ?> that = (Pair<?, ?>) o;
return that.left.equals(left) && that.right.equals(right);
}
}