Skip to content

Instantly share code, notes, and snippets.

@paulk-asert
Created April 8, 2022 13:21
Show Gist options
  • Save paulk-asert/8baee9b03a9178c7c3ff58dab691cf1c to your computer and use it in GitHub Desktop.
Save paulk-asert/8baee9b03a9178c7c3ff58dab691cf1c to your computer and use it in GitHub Desktop.
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