Class Ret2<A,​B>

  • Type Parameters:
    A - The type of the first member-field ('a').
    B - The type of the second member-field ('b').
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable

    public class Ret2<A,​B>
    extends RetN
    implements java.io.Serializable, java.lang.Cloneable
    This simple generic-class allows a function to return two 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.

    Immutable, Read-Only
    Classes which inherit Ancestor-Class RetN will always contains fields declared using the 'final' modifier. These classes are, therefore, "Unmodifiable Tuples" - meaning their contents can never change once they have been instantiated.

    Remember: The classes Ret2 and Tuple2 completely identical, with the sole difference being that Ret2 fields are all declared final, but the Tuple2 fields are not!
    See Also:
    Serialized Form


    • Field Summary

       
      Serializable ID
      Modifier and Type Field
      protected static long serialVersionUID
       
      Instance Fields
      Modifier and Type Field
      A a
      B b
    • Constructor Summary

      Constructors 
      Constructor Description
      Ret2​(A a, B b)
      Constructs this object
    • Method Summary

       
      Retrieve the Number of Fields in this Class
      Modifier and Type Method
      int n()
       
      Retrieve a Field by Field-Number
      Modifier and Type Method
      Object get​(int i)
       
      Convert this instance to a Read-Write Tuple, of the same size
      Modifier and Type Method
      Tuple2<A,​B> toModifiable()
       
      Methods: interface java.lang.Cloneable
      Modifier and Type Method
      Ret2<A,​B> clone()
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • serialVersionUID

        🡇     🗕  🗗  🗖
        protected static final long serialVersionUID
        This fulfils the SerialVersion UID requirement for all classes that implement Java's interface java.io.Serializable. Using the Serializable Implementation offered by java is very easy, and can make saving program state when debugging a lot easier. It can also be used in place of more complicated systems like "hibernate" to store data as well.
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
         protected static final long serialVersionUID = 1;
        
    • Method Detail

      • n

        🡅  🡇     🗕  🗗  🗖
        public int n()
        Returns '2', indicating how many fields are declared by this class.
        Specified by:
        n in class MultiType
        Returns:
        As an instance of Ret2, this method returns '2';
        Code:
        Exact Method Body:
         return 2;
        
      • clone

        🡅  🡇     🗕  🗗  🗖
        public Ret2<A,​Bclone()
        Description copied from class: MultiType
        Clones the contents of 'this' instance of.
        Specified by:
        clone in class MultiType
        Returns:
        A clone copy of 'this' object.
        Code:
        Exact Method Body:
         return new Ret2<>(this.a, this.b);
        
      • get

        🡅  🡇     🗕  🗗  🗖
        public java.lang.Object get​(int i)
        This method will retreive the ith field of this instance. This can be useful when you wish to use the Ancestor / Parent classses: RetN or MultiType for handling 'this' instance.

        As a reminder, the fields inside this class have all been declared public! Referencing this instance' fields, directly, will always work in place of calling any of the get(...) methods.

        Type Casting:
        This method can make for easier typing. When using either of the super-classes of this class - namely RetN or MultiType - you simply do not have to type (into your computer's keyboard) all of the Field Generic Types (the stuff between the '<' and '>' less-than & greater-than symbols).

        However, anytime one of these ancestor classes are used instead of the actual Ret2 instance, you will have to use one of the provided 'get' methods to retrieve a field inside the Ret2 instance. This is because the parent-ancestor classes do not actually have any fields to retrieve.

        There are three 'get' methods provided in Root-Ancestor Class MultiType. Each of these three 'getters' allows for casting (this) sub-class' returned fields in a slightly different way. When you aren't referencing the actual instance, itself, but rather one of the two Parent-Tuple Classes, you must use one of their getters, and you will have to cast the fields that you wish to use, eventually!

        Example:
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // No Cast Necessary
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
        // This line makes for longer typing, but DOES NOT REQUIRE A CAST TO RETRIEVE FIELDS.
        // Here the example Multi-Type Identifier is used: Ret2.
        
        Ret2<String, Integer> ref2 = some_function_returns_2();
        String neededStrField = ref2.a;
        
        
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Less Keyboard-Typing (Less Eyesores), but Requires a Cast
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        
        // If the Ret2 super-class RetN is used to reference this class data,
        // YOU WILL NEED TO CAST THE FIELDS BEFORE YOU CAN THEM, AS IN THE EXAMPLES BELOW:
        
        RetN refN = some_function_returns2();
        
        // Version #1: (this method) - simple 'get'
        @SuppressWarnings("unchecked")
        String neededStrField = (String) refN.get(1);
        
        // Version #1: The second Method-Parameter is a 'Class<T>' Type-Parameter
        // Note, again, that passing a '1' to the first parameter retrieves Ret2.a
        
        String neededStrField = refN.get(1, String.class);
        
        // Version #3: "GET" (in all caps) utilizes Java "Type-Inferencing" - the Return-Type
        // is specified by the left side of the assignments statement.
        
        String needesStrField = refN.GET(1);
        
        Specified by:
        get in class MultiType
        Parameters:
        i - This specifies which field of the instance is being requested.

        IMPORTANT: Unlike a Java array, when a '1' is passed to this parameter, it is requesting the first field in this instance. Passing a value of '0' shall cause an IndexOutOfBoundsException throw.
        Returns:
        This returns the ith field of this class.
        See Also:
        MultiType.get(int, Class), MultiType.GET(int)
        Code:
        Exact Method Body:
         // Throws Exception if i not 1 or 2
         CHECK_GET(i);
         return (i == 1) ? a : b;
        
      • toModifiable

        🡅     🗕  🗗  🗖
        public Tuple2<A,​BtoModifiable()
        Description copied from class: RetN
        Converts 'this' Read-Only RetN instance into a Read-Write TupleN.
        Specified by:
        toModifiable in class RetN
        Returns:
        A TupleN of the same size, which is a modifiable type whose fields are not decorated with the 'final' modifier - as the fields in this class are.
        Code:
        Exact Method Body:
         return new Tuple2<>(this.a, this.b);