Skip to content

Commit

Permalink
minor update
Browse files Browse the repository at this point in the history
  • Loading branch information
jabrena committed May 31, 2024
1 parent 09696f2 commit 64b62c8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 6 additions & 9 deletions training/src/main/java/info/jab/fp/sealed/AgeProblemExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
public class AgeProblemExample {

record Age(Integer age) {}

// these are the potential problems
sealed interface AgeProblem permits InvalidAge, NotLegalAdult {
String getMessage();
}

record InvalidAge() implements AgeProblem {

Expand All @@ -25,11 +20,13 @@ public String getMessage() {
}
}

//Either<AgeProblem, Age> problem1 = Either.left(new InvalidAge());
//Either<AgeProblem, Age> problem2 = Either.left(new NotLegalAdult());
//Either<AgeProblem, Age> success = Either.right(new Age(20));
// these are the potential problems
sealed interface AgeProblem permits InvalidAge, NotLegalAdult {
String getMessage();
}

public Either<AgeProblem, Age> validateAge(Integer age) {
// validation returns either problems or the constructed value
public Either<AgeProblem, Age> validatAge(Integer age) {
return switch (age) {
case Integer a when a < 0 -> Either.left(new InvalidAge());
case Integer a when a < 18 -> Either.left(new NotLegalAdult());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public void should_notValidate() {

AgeProblemExample example = new AgeProblemExample();

var result = example.validateAge(-1);
var result = example.validatAge(-1);

assertThat(result).isInstanceOf(Either.class);
assertThat(result.isLeft()).isTrue();

var result2 = example.validateAge(17);
var result2 = example.validatAge(17);
assertThat(result2).isInstanceOf(Either.class);
assertThat(result2.isLeft()).isTrue();

Expand All @@ -45,7 +45,7 @@ public void should_validate() {

AgeProblemExample example = new AgeProblemExample();

var result = example.validateAge(18);
var result = example.validatAge(18);

assertThat(result).isInstanceOf(Either.class);
assertThat(result.isRight()).isTrue();
Expand Down

0 comments on commit 64b62c8

Please sign in to comment.