I so wish there were tuples in Java.
The closest thing I can think of that’s type safe and Javaish is pasted below–but also saves time typing mundane data structures.
With this code, one can do:
Pair pair = Tuple.createPair(“oh hai”, 12345)
By the way–I hate this solution. It’s still too much tip-tapping on the keyboard.
package copy.paste;
public class Tuple {
public interface Single {
ONE get1();
}
public interface Pair extends Single {
TWO get2();
}
public interface Triple extends Pair {
THREE get3();
}
public interface Quad extends Triple {
FOUR get4();
}
public interface Quint extends Quad {
FIVE get5();
}
public static Single createSingle(final ONE one) {
return new Single() {
@Override public ONE get1() {
return one;
}
};
}
public static Pair createPair(final ONE one, final TWO two) {
return new Pair() {
@Override public ONE get1() {
return one;
}
@Override public TWO get2() {
return two;
}
};
}
public static Triple createTriple(final ONE one, final TWO two, final THREE three) {
return new Triple() {
@Override public ONE get1() {
return one;
}
@Override public TWO get2() {
return two;
}
@Override public THREE get3() {
return three;
}
};
}
public static Quad createQuad(final ONE one, final TWO two, final THREE three, final FOUR four) {
return new Quad() {
@Override public ONE get1() {
return one;
}
@Override public TWO get2() {
return two;
}
@Override public THREE get3() {
return three;
}
@Override public FOUR get4() {
return four;
}
};
}
public static Quint createQuint(final ONE one, final TWO two, final THREE three, final FOUR four, final FIVE five) {
return new Quint() {
@Override public ONE get1() {
return one;
}
@Override public TWO get2() {
return two;
}
@Override public THREE get3() {
return three;
}
@Override public FOUR get4() {
return four;
}
@Override public FIVE get5() {
return five;
}
};
}
}