com.healthmarketscience.common.util
Class Tuple2<ObjType0,ObjType1>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<Object>
          extended by com.healthmarketscience.common.util.Tuple1<ObjType0>
              extended by com.healthmarketscience.common.util.Tuple2<ObjType0,ObjType1>
All Implemented Interfaces:
Serializable, Cloneable, Iterable<Object>, Collection<Object>, List<Object>, Map.Entry<ObjType0,ObjType1>, RandomAccess
Direct Known Subclasses:
Tuple3

public class Tuple2<ObjType0,ObjType1>
extends Tuple1<ObjType0>
implements Cloneable, Serializable, Map.Entry<ObjType0,ObjType1>

Simple class for holding a pair of typed objects. Also implements Entry.Map. You can construct a Tuple2 from a Map.Entry, and equals(java.lang.Object) and hashCode() are both special-cased to handle Map.Entry.

Author:
James Ahlborn
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class com.healthmarketscience.common.util.Tuple1
Tuple1.Accessor, Tuple1.IterableIterator<Type>
 
Field Summary
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
Tuple2()
          Initializes a new Tuple2 with null objects.
Tuple2(Map.Entry<? extends ObjType0,? extends ObjType1> entry)
          Initializes a new Tuple2 based on the given Map.Entry.
Tuple2(ObjType0 obj0, ObjType1 obj1)
          Initializes a new Tuple2 with the given objects.
 
Method Summary
 Tuple2<ObjType0,ObjType1> clone()
          Makes a copy of this Tuple2.
static
<InObjType1>
Collection<InObjType1>
collection1(Collection<? extends Tuple2<?,InObjType1>> col)
          Get a Collection adapter for element 1 of the Tuples of the given Collection.
static
<InObjType0,InObjType1>
Tuple2<InObjType0,InObjType1>
create(InObjType0 obj0, InObjType1 obj1)
          Creates a new Tuple2 with the provided objects
static
<InObjType0,InObjType1>
Tuple2<InObjType0,InObjType1>
create(Map.Entry<InObjType0,InObjType1> entry)
          Creates a new Tuple2 with the Map.Entry's key and value as the objects.
 boolean equals(Object o)
          Indicates whether the given Object is equal to this Tuple2.
 Object[] get()
          Get the objects of this tuple as an array.
 Object get(int index)
          Get the value of this tuple identified by index, which must always be either 0 or 1, since this tuple class stores exactly two objects.
 ObjType1 get1()
          Returns the second object of this tuple.
 ObjType0 getKey()
          Get the first object of this tuple.
 ObjType1 getValue()
          Get the second object of this tuple.
 int hashCode()
          Returns a hash code for this Tuple2, based on the objects it contains.
static
<InObjType1>
Iterable<InObjType1>
iterable1(Iterable<? extends Tuple2<?,InObjType1>> iable)
          Get an Iterable adapter that returns an Iterator that iterates over element 1 of the Tuples within the given Iterable/Iterator.
 void set(ObjType0 obj0, ObjType1 obj1)
          Sets the values of this Tuple2.
 void set1(ObjType1 obj)
          Sets the second object of this tuple.
 ObjType1 setValue(ObjType1 newValue)
          Set the second object of this tuple.
 int size()
          Get the size of this tuple, which is always 2.
 
Methods inherited from class com.healthmarketscience.common.util.Tuple1
collection0, get0, iterable0, set, set0
 
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
 
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.List
add, add, addAll, addAll, clear, contains, containsAll, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, retainAll, set, subList, toArray, toArray
 

Constructor Detail

Tuple2

public Tuple2()
Initializes a new Tuple2 with null objects.


Tuple2

public Tuple2(ObjType0 obj0,
              ObjType1 obj1)
Initializes a new Tuple2 with the given objects. You may want to use create(InObjType0, InObjType1) instead, since it infers the type parameters based on its arguments.

Parameters:
obj0 - the first object to store in this tuple.
obj1 - the second object to store in this tuple.

Tuple2

public Tuple2(Map.Entry<? extends ObjType0,? extends ObjType1> entry)
Initializes a new Tuple2 based on the given Map.Entry.

Parameters:
entry - the entry base this Tuple2 on
Method Detail

create

public static <InObjType0,InObjType1> Tuple2<InObjType0,InObjType1> create(InObjType0 obj0,
                                                                           InObjType1 obj1)
Creates a new Tuple2 with the provided objects

Parameters:
obj0 - the first object to store in this tuple.
obj1 - the second object to store in this tuple.
Returns:
a new Tuple2 that contains the provided objects

create

public static <InObjType0,InObjType1> Tuple2<InObjType0,InObjType1> create(Map.Entry<InObjType0,InObjType1> entry)
Creates a new Tuple2 with the Map.Entry's key and value as the objects.

Parameters:
entry - the Map.Entry from which a new Tuple2 will be created.
Returns:
a new Tuple2 whose first object is the given Map.Entry's key, and whose second object is the Map.Entry's value.

iterable1

public static <InObjType1> Iterable<InObjType1> iterable1(Iterable<? extends Tuple2<?,InObjType1>> iable)
Get an Iterable adapter that returns an Iterator that iterates over element 1 of the Tuples within the given Iterable/Iterator.

Returns:
an Iterable adapter that returns an Iterator that iterates over element 1 of the Tuples within the given Iterable/Iterator.

collection1

public static <InObjType1> Collection<InObjType1> collection1(Collection<? extends Tuple2<?,InObjType1>> col)
Get a Collection adapter for element 1 of the Tuples of the given Collection. The returned Collection supports element removal if the given Collection supports element removal, but does not support element addition.

Returns:
a Collection adapter for element 1 of the Tuples of the given Collection.

get1

public final ObjType1 get1()
Returns the second object of this tuple.

Returns:
the second object of this tuple.

set1

public final void set1(ObjType1 obj)
Sets the second object of this tuple.

Parameters:
obj - the object to set as the second object of this tuple.

get

public Object[] get()
Get the objects of this tuple as an array.

Overrides:
get in class Tuple1<ObjType0>
Returns:
the objects of this tuple as an array.

get

public Object get(int index)
Get the value of this tuple identified by index, which must always be either 0 or 1, since this tuple class stores exactly two objects.

Specified by:
get in interface List<Object>
Overrides:
get in class Tuple1<ObjType0>
Parameters:
index - the index of the object to retrieve. Must be 0 or 1.
Returns:
the object in this tuple with the given index.

set

public final void set(ObjType0 obj0,
                      ObjType1 obj1)
Sets the values of this Tuple2.

Parameters:
obj0 - the object to use as the first object of this tuple.
obj1 - the object to use as the second object of this tuple.

getKey

public ObjType0 getKey()
Get the first object of this tuple.

Specified by:
getKey in interface Map.Entry<ObjType0,ObjType1>
Returns:
the first object of this tuple.

getValue

public ObjType1 getValue()
Get the second object of this tuple.

Specified by:
getValue in interface Map.Entry<ObjType0,ObjType1>
Returns:
the second object of this tuple.

setValue

public ObjType1 setValue(ObjType1 newValue)
Set the second object of this tuple.

Specified by:
setValue in interface Map.Entry<ObjType0,ObjType1>
Parameters:
newValue - the object to set as the second object of this tuple.
Returns:
the object that was previously the second value of this tuple.

size

public int size()
Get the size of this tuple, which is always 2.

Specified by:
size in interface Collection<Object>
Specified by:
size in interface List<Object>
Overrides:
size in class Tuple1<ObjType0>
Returns:
2

clone

public Tuple2<ObjType0,ObjType1> clone()
Makes a copy of this Tuple2.

Overrides:
clone in class Tuple1<ObjType0>
Returns:
a copy of this Tuple2.

equals

public boolean equals(Object o)
Indicates whether the given Object is equal to this Tuple2.

Specified by:
equals in interface Collection<Object>
Specified by:
equals in interface List<Object>
Specified by:
equals in interface Map.Entry<ObjType0,ObjType1>
Overrides:
equals in class Tuple1<ObjType0>
Returns:
  • true if the given Object refers to this Tuple2.
  • true if the given Object is an instance of Tuple2 for which the contained objects are equal to this Tuple2's objects.
  • true if the given Object is an instance of Entry.Map, and the Object's key and value are equal to this Tuple2's key and value, respectively.
  • false otherwise.

  • hashCode

    public int hashCode()
    Returns a hash code for this Tuple2, based on the objects it contains.

    Specified by:
    hashCode in interface Collection<Object>
    Specified by:
    hashCode in interface List<Object>
    Specified by:
    hashCode in interface Map.Entry<ObjType0,ObjType1>
    Overrides:
    hashCode in class Tuple1<ObjType0>
    Returns:
    a hash code for this Tuple2.


    Copyright © 2006-2016 Health Market Science. All Rights Reserved.