1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | package Torello.Java.Additional; /** * This simple generic-class allows a function to return <B STYLE='color:red'>two</B> objects as a * result, instead of just one. This is not always so useful, and can make code confusing. * However there are some instances where the only alternative would be to create an entirely new * class/object, when only a single method result would use that object. * * <EMBED CLASS=globalDefs DATA-KIND=Ret DATA-N=2> * <EMBED CLASS='external-html' DATA-FILE-ID=IMMUTABLE_RET> * @param <A> The type of the <B STYLE='color:red'>first</B> member-field ('{@link #a}'). * @param <B> The type of the <B STYLE='color:red'>second</B> member-field ('{@link #b}'). */ public class Ret2<A, B> extends RetN implements java.io.Serializable, Cloneable { /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */ protected static final long serialVersionUID = 1; /** This holds a pointer the first response object. */ public final A a; /** This holds a pointer to the second response object. */ public final B b; /** Constructs this object */ public Ret2(A a, B b) { this.a = a; this.b = b; } /** * Returns {@code '2'}, indicating how many fields are declared by this class. * @return As an instance of {@code Ret2}, this method returns {@code '2'}; */ public int n() { return 2; } // Super-class uses this for toString, equals, and hashCode // There is an optimization, so if this is requested multiple times, it is saved in a // transient field. final Object[] asArrayInternal() { return new Object[] { a, b }; } public Ret2<A, B> clone() { return new Ret2<>(this.a, this.b); } /** * <EMBED CLASS=defs DATA-TEXT="Integer"> * <EMBED CLASS='external-html' DATA-FILE-ID=MULTI_TYPE_EX_COMMON> * <EMBED CLASS='external-html' DATA-FILE-ID=MULTI_TYPE_GET_EXAMPLE> */ public Object get(final int i) { // Throws Exception if i not 1 or 2 CHECK_GET(i); return (i == 1) ? a : b; } public Tuple2<A, B> toModifiable() { return new Tuple2<>(this.a, this.b); } } |