Class Tuple

  • All Implemented Interfaces:
    java.lang.Iterable<java.lang.Object>

    public class Tuple
    extends java.lang.Object
    implements java.lang.Iterable<java.lang.Object>

    A simple Tuple object. Trades strict type safety for flexibility. If you want a type-safe tuple you have to define a specific Tuple class for a 1-tuple, for a 2-tuple, for a 3-tuple ... up to n-tuple. This does not really scale. Most likely your uses will fall into a small enough range you could make a class for each, but this gets most of the joy without having to write lots of classes for each size.

    Author:
    Michael Grove
    • Constructor Summary

      Constructors 
      Constructor Description
      Tuple​(boolean theSafe, java.lang.Object... theData)
      Create a new Tuple
      Tuple​(java.lang.Object... theData)
      Create a new Tuple
      Tuple​(java.util.List<?> theData)
      Create a new Tuple
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T get​(int theIndex)
      Return the Tuple value at the given index.
      java.util.Iterator<java.lang.Object> iterator()
      int length()
      Return the length of this tuple.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • Tuple

        public Tuple​(java.util.List<?> theData)
        Create a new Tuple
        Parameters:
        theData - the tuple data
      • Tuple

        public Tuple​(java.lang.Object... theData)
        Create a new Tuple
        Parameters:
        theData - the tuple data
      • Tuple

        public Tuple​(boolean theSafe,
                     java.lang.Object... theData)
        Create a new Tuple
        Parameters:
        theSafe - true to enable safe mode, false otherwise
        theData - the tuple data
    • Method Detail

      • get

        public <T> T get​(int theIndex)
        Return the Tuple value at the given index. This will return the value as whatever it's being assigned to: Integer aInt = aTuple.get(2); Or you can explicitly request the type: return aTuple.<Boolean>get(0); This is done by default in "safe" mode. Safe mode will catch the possible ClassCastException and return null instead. You can disable safe mode to get the
        Type Parameters:
        T - the type the return value should be
        Parameters:
        theIndex - the indexed value of the tuple to retrieve
        Returns:
        the value at the index, or null if it's not the right type and safe mode is enabled
        Throws:
        java.lang.ClassCastException - thrown if you ask for a tuple element with a given type and it cannot be casted to that value (when safe mode is off).
        java.lang.IndexOutOfBoundsException - if you ask for a tuple element that does not exist
      • length

        public int length()
        Return the length of this tuple.
        Returns:
        the length
      • iterator

        public java.util.Iterator<java.lang.Object> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Object>