mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-10-26 22:10:22 +00:00
equals and hashcode for Either and Pair
This commit is contained in:
1 parent
56fb510d96
commit
92339c904d
3 files changed
+44
No files matched your search
@@ -49,4 +49,21 @@ public final class Either<L, R> {
|
|||||||
public boolean hasRight() {
|
public boolean hasRight() {
|
||||||
return !leftPresent;
|
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;
|
package com.dfsek.terra.api.util.generic.pair;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ImmutablePair<L, R> {
|
public class ImmutablePair<L, R> {
|
||||||
private final L left;
|
private final L left;
|
||||||
private final R right;
|
private final R right;
|
||||||
@@ -24,4 +26,15 @@ public class ImmutablePair<L, R> {
|
|||||||
public Pair<L, R> mutable() {
|
public Pair<L, R> mutable() {
|
||||||
return new Pair<>(left, right);
|
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;
|
package com.dfsek.terra.api.util.generic.pair;
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -32,4 +34,16 @@ public class Pair<L, R> {
|
|||||||
public ImmutablePair<L, R> immutable() {
|
public ImmutablePair<L, R> immutable() {
|
||||||
return new ImmutablePair<>(left, right);
|
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