001package Torello.Java.Additional;
002
003
004/**
005 * This simple generic-class allows a function to return <B STYLE='color:red'>two</B> objects as a
006 * result, instead of just one.   This is not always so useful, and can make code confusing.
007 * However there are some instances where the only alternative would be to create an entirely new 
008 * class/object, when only a single method result would use that object.
009 * 
010 * <EMBED CLASS=globalDefs DATA-KIND=Ret DATA-N=2>
011 * <EMBED CLASS='external-html' DATA-FILE-ID=IMMUTABLE_RET>
012 * @param <A> The type of the <B STYLE='color:red'>first</B> member-field ('{@link #a}').
013 * @param <B> The type of the <B STYLE='color:red'>second</B> member-field ('{@link #b}').
014 */
015public class Ret2<A, B>
016    extends RetN
017    implements java.io.Serializable, Cloneable
018{
019    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID>  */
020    protected static final long serialVersionUID = 1;
021
022    /** This holds a pointer the first response object. */
023    public final A a;
024
025    /** This holds a pointer to the second response object. */
026    public final B b;
027
028    /** Constructs this object */
029    public Ret2(A a, B b)
030    {
031        this.a = a;
032        this.b = b;
033    }
034
035    /**
036     * Returns {@code '2'}, indicating how many fields are declared by this class.
037     * @return As an instance of {@code Ret2}, this method returns {@code '2'};
038     */
039    public int n() { return 2; }
040
041    // Super-class uses this for toString, equals, and hashCode
042    // There is an optimization, so if this is requested multiple times, it is saved in a
043    // transient field.
044
045    final Object[] asArrayInternal()
046    { return new Object[] { a, b }; }
047
048    public Ret2<A, B> clone()
049    { return new Ret2<>(this.a, this.b); }
050
051    /**
052     * <EMBED CLASS=defs DATA-TEXT="Integer">
053     * <EMBED CLASS='external-html' DATA-FILE-ID=MULTI_TYPE_EX_COMMON>
054     * <EMBED CLASS='external-html' DATA-FILE-ID=MULTI_TYPE_GET_EXAMPLE>
055     */
056    public Object get(final int i)
057    {
058        // Throws Exception if i not 1 or 2
059        CHECK_GET(i);
060        return (i == 1) ? a : b;
061    }
062
063    public Tuple2<A, B> toModifiable()
064    { return new Tuple2<>(this.a, this.b); }
065}