Implement Result.equals() and hashcode()

This commit is contained in:
Paul Campbell 2018-06-25 22:51:18 +01:00
parent 7e92129dfc
commit 24a233db5c
2 changed files with 21 additions and 0 deletions

View file

@ -24,6 +24,7 @@ package net.kemitix.mon.result;
import lombok.RequiredArgsConstructor;
import net.kemitix.mon.maybe.Maybe;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
@ -73,6 +74,16 @@ class Err<T> implements Result<T> {
throw error;
}
@Override
public boolean equals(Object other) {
return other instanceof Err && Objects.equals(error, ((Err) other).error);
}
@Override
public int hashCode() {
return Objects.hash(error);
}
@Override
public String toString() {
return String.format("Result.Error{error=%s}", error);

View file

@ -8,6 +8,16 @@ import org.junit.Test;
public class ResultTest implements WithAssertions {
@Test
public void equality() {
assertThat(Result.ok(1)).isEqualTo(Result.ok(1));
assertThat(Result.ok(1)).isNotEqualTo(Result.ok(2));
final RuntimeException runtimeException = new RuntimeException();
assertThat(Result.ok(1)).isNotEqualTo(Result.error(runtimeException));
assertThat(Result.error(runtimeException)).isEqualTo(Result.error(runtimeException));
assertThat(Result.ok(1).equals("1")).isFalse();
}
@Test
public void createSuccess_isSuccess() {
//when