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