001package Torello.Java.Additional;
002
003import java.lang.reflect.Field;
004
005/**
006 * This simple generic-class allows for storing <B STYLE='color:red'>two</B> objects with a 
007 * single reference.
008 *
009 * <EMBED CLASS=globalDefs DATA-KIND=Tuple DATA-N=2>
010 * <EMBED CLASS='external-html' DATA-FILE-ID=MUTABLE_TUPLE>
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 */
014public class Tuple2<A, B>
015    extends TupleN
016    implements java.io.Serializable, Cloneable
017{
018    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID>  */
019    protected static final long serialVersionUID = 1;
020
021    /** This holds a pointer the first field / instance. */
022    public A a;
023
024    /** This holds a pointer to the second field / instance. */
025    public B b;
026
027    /** Constructs this object */
028    public Tuple2(A a, B b)
029    {
030        this.a = a;
031        this.b = b;
032    }
033
034    /** Constructs this object. Initializes both fields to null. */
035    public Tuple2()
036    {
037        this.a = null;
038        this.b = null;
039    }
040
041    /**
042     * Returns {@code '2'}, indicating how many fields are declared by this class.
043     * @return As an instance of {@code Tuple1}, this method returns {@code '2'};
044     */
045    public int n() { return 2; }
046
047    // Super-class uses this for toString, equals, and hashCode
048    // There is an optimization, so if this is requested multiple times, it is saved in a
049    // transient field.
050
051    final Object[] asArrayInternal()
052    { return new Object[] { a, b }; }
053
054    public Tuple2<A, B> clone()
055    { return new Tuple2<>(this.a, this.b); }
056
057    /**
058     * <EMBED CLASS=defs DATA-TEXT="Integer">
059     * <EMBED CLASS='external-html' DATA-FILE-ID=MULTI_TYPE_EX_COMMON>
060     * <EMBED CLASS='external-html' DATA-FILE-ID=MULTI_TYPE_GET_EXAMPLE>
061     */
062    public Object get(final int i)
063    {
064        // Throws Exception if i not 1 or 2
065        CHECK_GET(i);
066        return (i == 1) ? a : b;
067    }
068
069    public Ret2<A, B> toImmutable()
070    { return new Ret2<>(this.a, this.b); }
071}