Implement Result.equals() and hashcode()
This commit is contained in:
parent
7e92129dfc
commit
24a233db5c
2 changed files with 21 additions and 0 deletions
|
@ -24,6 +24,7 @@ package net.kemitix.mon.result;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import net.kemitix.mon.maybe.Maybe;
|
import net.kemitix.mon.maybe.Maybe;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
@ -73,6 +74,16 @@ class Err<T> implements Result<T> {
|
||||||
throw error;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("Result.Error{error=%s}", error);
|
return String.format("Result.Error{error=%s}", error);
|
||||||
|
|
|
@ -8,6 +8,16 @@ import org.junit.Test;
|
||||||
|
|
||||||
public class ResultTest implements WithAssertions {
|
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
|
@Test
|
||||||
public void createSuccess_isSuccess() {
|
public void createSuccess_isSuccess() {
|
||||||
//when
|
//when
|
||||||
|
|
Loading…
Reference in a new issue