Created
August 23, 2022 14:19
-
-
Save Garciat/a641b22aa00c4dfe234d71ff0f403ec6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.stages; | |
import java.util.Optional; | |
interface Person<Name, Age> { | |
Name name(); | |
Age age(); | |
} | |
record Person_Any<Name, Age>(Name name, Age age) implements Person<Name, Age> {} | |
interface Person_Name<T, R> { | |
default <Age> Person<R, Age> apply(Person<T, Age> person) { | |
return new Person_Any<>(applyName(person.name()), person.age()); | |
} | |
R applyName(T name); | |
} | |
record Natural(Integer value) {} | |
record PersonAgeStage1() { | |
public <Name> Person<Name, Integer> apply(Person<Name, Optional<String>> person) { | |
return new Result<>(person.name(), person.age().map(Integer::parseInt).orElse(0)); | |
} | |
record Result<Name>(Name name, Integer age) implements Person<Name, Integer> {} | |
} | |
record PersonAgeStage2() { | |
public <Name> Person<Name, Natural> apply(Person<Name, Integer> person) { | |
return new Result<>(person.name(), new Natural(person.age())); | |
} | |
record Result<Name>(Name name, Natural age) implements Person<Name, Natural> {} | |
} | |
record PersonNameStage1() { | |
public <Age> Person<String, Age> apply(Person<Optional<String>, Age> person) { | |
return new Result<>(person.name().orElse(""), person.age()); | |
} | |
record Result<Age>(String name, Age age) implements Person<String, Age> {} | |
} | |
record PersonNameStage1_Alt() implements Person_Name<Optional<String>, String> { | |
@Override | |
public String applyName(Optional<String> name) { | |
return name.orElse(""); | |
} | |
} | |
record PersonRaw(Optional<String> name, Optional<String> age) | |
implements Person<Optional<String>, Optional<String>> {} | |
record PersonFinal(String name, Natural age) implements Person<String, Natural> { | |
static PersonFinal of(Person<String, Natural> person) { | |
return new PersonFinal(person.name(), person.age()); | |
} | |
} | |
record PersonProcess( | |
PersonAgeStage1 ageStage1, | |
PersonAgeStage2 ageStage2, | |
PersonNameStage1 nameStage1 | |
) { | |
public PersonFinal apply(PersonRaw person) { | |
return PersonFinal.of(ageStage2.apply(ageStage1.apply(nameStage1.apply(person)))); | |
} | |
} | |
public class Stages { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment