mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-08 08:46:13 +00:00
equals and hashcode for Either and Pair
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user