001package Torello.Java.Additional;
002
003import java.lang.reflect.Field;
004
005/**
006 * The classes {@code Tuple1} and {@link Tuple0} are here merely for the convenience and the
007 * possibility that an unknown sized tuple might be needed, and on occasion that unknown sized 
008 * reference would contain only one (or even zero) elements.
009 * 
010 * <BR /><BR />There are otherwise very few uses for a 1 or 0 sized {@code Tuple}.
011 * 
012 * <EMBED CLASS=globalDefs DATA-KIND=Tuple DATA-N=1>
013 * <EMBED CLASS='external-html' DATA-FILE-ID=MUTABLE_TUPLE>
014 * @param <A> The type of the <B STYLE='color:red'>only</B> member-field ('{@link #a}').
015 */
016public class Tuple1<A>
017    extends TupleN
018    implements java.io.Serializable, Cloneable
019{
020    /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID>  */
021    protected static final long serialVersionUID = 1;
022
023    /** This holds a pointer the first and only field / instance. */
024    public A a;
025
026    /** Constructs this object */
027    public Tuple1(A a) { this.a = a; }
028
029    /** Constructs this object.  Initializes the field to null. */
030    public Tuple1() { this.a = null; }
031
032    /**
033     * Returns {@code '1'}, indicating how many fields are declared by this class.
034     * @return As an instance of {@code Tuple1}, this method returns {@code '1'};
035     */
036    public int n() { return 1; }
037
038    // Super-class uses this for toString, equals, and hashCode
039    // There is an optimization, so if this is requested multiple times, it is saved in a
040    // transient field.
041
042    final Object[] asArrayInternal()
043    { return new Object[] { a }; }
044
045    public Tuple1<A> clone()
046    { return new Tuple1<>(this.a); }
047
048    /**
049     * Returns this {@code Tuple1} instance's only field - field {@link #a}.  This method may only
050     * be invoked on an input value of {@code '1'}, or else it throws.  A {@code Tuple1} instance
051     * has only a single field that may be retrieved.
052     * 
053     * <BR /><BR />This method must be included to properly, fully, implement the ancestor 
054     * {@link MultiType} interface.
055     * 
056     * @param i This must be passed {@code '1'}, or else this method throws IOOBEX.
057     * 
058     * @returns The reference contained by field {@link #a}.  If this method returns, its returned
059     * type will always be {@link <A>}.
060     * 
061     * @throws IndexOutOfBoundsException As this {@code Tuple1} instance has but a single field,
062     * <I>this exceptions throws for all values of {@code 'i'} - except {@code '1'}</I>.
063     */
064    public A get(final int i)
065    {
066        if (i != 1) throw new IndexOutOfBoundsException(
067            "This is an instance of Tuple1, and therefore '1' is the only valid value which may " +
068            "be passed to 'i'"
069        );
070
071        return a;
072    }
073
074    public Ret1<A> toImmutable()
075    { return new Ret1<>(this.a); }
076}
077