Skip to content

Instantly share code, notes, and snippets.

@Koboo
Created June 11, 2021 07:05
Show Gist options
  • Save Koboo/4db6e126d441ab236dab10d471e5ddfc to your computer and use it in GitHub Desktop.
Save Koboo/4db6e126d441ab236dab10d471e5ddfc to your computer and use it in GitHub Desktop.
Simple class to "fake" Tuple functionality from C# in java
public interface Tuple {
static <T extends Tuple, T1, T2> T create(T1 item1, T2 item2) {
return (T) new Two<>(item1, item2);
}
static <T extends Tuple, T1, T2, T3> T create(T1 item1, T2 item2, T3 item3) {
return (T) new Three<>(item1, item2, item3);
}
static <T extends Tuple, T1, T2, T3, T4> T create(T1 item1, T2 item2, T3 item3, T4 item4) {
return (T) new Four<>(item1, item2, item3, item4);
}
class Two<T1, T2> implements Tuple {
private final T1 item1;
private final T2 item2;
public Two(T1 item1, T2 item2) {
this.item1 = item1;
this.item2 = item2;
}
public T1 getItem1() {
return item1;
}
public T2 getItem2() {
return item2;
}
}
class Three<T1, T2, T3> extends Two<T1, T2> {
private final T3 item3;
public Three(T1 item1, T2 item2, T3 item3) {
super(item1, item2);
this.item3 = item3;
}
public T3 getItem3() {
return item3;
}
}
class Four<T1, T2, T3, T4> extends Three<T1, T2, T3> {
private final T4 item4;
public Four(T1 item1, T2 item2, T3 item3, T4 item4) {
super(item1, item2, item3);
this.item4 = item4;
}
public T4 getItem4() {
return item4;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment