Class Tuple

java.lang.Object
com.complexible.common.util.Tuple
All Implemented Interfaces:
Iterable<Object>

public class Tuple extends Object implements Iterable<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 Details

    • Tuple

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

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

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

    • 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:
      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).
      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 Iterator<Object> iterator()
      Specified by:
      iterator in interface Iterable<Object>