-
-
Save paulk-asert/8baee9b03a9178c7c3ff58dab691cf1c 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
import java.util.List; | |
sealed interface ListState {} | |
interface Empty extends ListState { } | |
interface NonEmpty extends ListState { } | |
class MyList<ListStatus,E> { | |
public List<E> getDelegate() { | |
return delegate; | |
} | |
private List<E> delegate; | |
private MyList(List<E> delegate) { | |
this.delegate = delegate; | |
} | |
public static <E> MyList<Empty,E> of() { | |
return new MyList<>(List.of()); | |
} | |
public static <E> MyList<NonEmpty,E> of(E e1) { | |
return new MyList<>(List.of(e1)); | |
} | |
// other factory methods ... | |
public static <E> MyList<Empty,E> addEmpty(MyList<Empty,E> one, MyList<Empty,E> two) { | |
return one; | |
} | |
public static <E> MyList<NonEmpty,E> addToEmpty(MyList<Empty,E> one, MyList<NonEmpty,E> two) { | |
return two; | |
} | |
public static <E> MyList<NonEmpty,E> addEmptyTo(MyList<NonEmpty,E> one, MyList<Empty,E> two) { | |
return one; | |
} | |
public static void main(String[] args) { | |
System.out.println(MyList.addEmpty(MyList.of(), MyList.of())); | |
System.out.println(MyList.addToEmpty(MyList.of(), MyList.of(1))); | |
System.out.println(MyList.addEmptyTo(MyList.of(1), MyList.of())); | |
} | |
// other methods which use delegate | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment