Skip to content

Instantly share code, notes, and snippets.

@pythoneer
Last active August 2, 2017 18:23
Show Gist options
  • Save pythoneer/a9515751647cfbe4f95bd274160e7459 to your computer and use it in GitHub Desktop.
Save pythoneer/a9515751647cfbe4f95bd274160e7459 to your computer and use it in GitHub Desktop.
java_generic_concrete
public class Main {
//a container holding all kinds of stuff
interface Container<F,M,L> {
boolean contains(F f, M m, L l);
F first();
M middle();
L last();
}
//a builder that can build containers
interface ContainerBuilder<S extends ContainerBuilder<S, F, M, L>, F, M, L> { //<- WTF .. wow!
S first(F f);
S middle(M m);
S last(L l);
Container<F, M, L> build();
}
public static class IntContainer implements Container<Integer, Integer, Integer> {
private int first;
private int middle;
private int last;
public IntContainer(int first, int middle, int last) {
this.first = first;
this.middle = middle;
this.last = last;
}
@Override
public boolean contains(Integer first, Integer middle, Integer last) {
return this.first == first && this.middle == middle && this.last == last;
}
@Override
public Integer first() {
return first;
}
@Override
public Integer middle() {
return middle;
}
@Override
public Integer last() {
return last;
}
}
public static class IntContainerBuilder implements ContainerBuilder<IntContainerBuilder, Integer, Integer, Integer> {
private int first;
private int middle;
private int last;
@Override
public IntContainerBuilder first(Integer integer) {
this.first = integer;
return this;
}
@Override
public IntContainerBuilder middle(Integer integer) {
this.middle = integer;
return this;
}
@Override
public IntContainerBuilder last(Integer integer) {
this.last = integer;
return this;
}
@Override
public Container<Integer, Integer, Integer> build() {
return new IntContainer(this.first, this.middle, this.last);
}
}
//all those types F M L ... i only need F
public static <F, M, L> F first(Container<F, M, L> container) {
return container.first();
}
//all those types F M L ... i only need M
public static <F, M, L> M middle(Container<F, M, L> container) {
return container.middle();
}
//all those types F M L ... i only need L
public static <F, M, L> L last(Container<F, M, L> container) {
return container.last();
}
public static <F, M, L> boolean contains(Container<F, M, L> container, F first, M middle, L last) {
return container.contains(first, middle, last);
}
public static void main(String[] args) {
final Container<Integer, Integer, Integer> ic = new IntContainerBuilder().first(1).middle(2).last(3).build();
final int lastItem = last(ic);
System.out.println("last: " + lastItem);
final boolean containsItems = contains(ic, 1, 2, 3);
System.out.println("contains: " + containsItems);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment