Class Pair<A,​B>

  • Type Parameters:
    A - the first element type
    B - the second element type
    All Implemented Interfaces:
    Tuple, java.io.Serializable, java.lang.Comparable<Pair<A,​B>>, Bean, ImmutableBean

    public final class Pair<A,​B>
    extends java.lang.Object
    implements ImmutableBean, Tuple, java.lang.Comparable<Pair<A,​B>>, java.io.Serializable
    An immutable pair consisting of two elements.

    This implementation refers to the elements as 'first' and 'second'. The elements cannot be null.

    Although the implementation is immutable, there is no restriction on the objects that may be stored. If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.

    Some primitive specializations of this class are provided, such as DoublesPair.

    This class is immutable and thread-safe if the stored objects are immutable.

    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Pair.Meta<A,​B>
      The meta-bean for Pair.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(Pair<A,​B> other)
      Compares the pair based on the first element followed by the second element.
      com.google.common.collect.ImmutableList<java.lang.Object> elements()
      Gets the elements from this pair as a list.
      boolean equals​(java.lang.Object obj)  
      A getFirst()
      Gets the first element in this pair.
      B getSecond()
      Gets the second element in this pair.
      int hashCode()  
      static Pair.Meta meta()
      The meta-bean for Pair.
      Pair.Meta<A,​B> metaBean()  
      static <R,​S>
      Pair.Meta<R,​S>
      metaPair​(java.lang.Class<R> cls1, java.lang.Class<S> cls2)
      The meta-bean for Pair.
      static <A,​B>
      Pair<A,​B>
      of​(A first, B second)
      Obtains a pair inferring the types.
      int size()
      Gets the number of elements held by this pair.
      java.lang.String toString()
      Gets the pair using a standard string format.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • of

        public static <A,​B> Pair<A,​B> of​(A first,
                                                     B second)
        Obtains a pair inferring the types.
        Type Parameters:
        A - the first element type
        B - the second element type
        Parameters:
        first - the first element
        second - the second element
        Returns:
        a pair formed from the two parameters
      • size

        public int size()
        Gets the number of elements held by this pair.
        Specified by:
        size in interface Tuple
        Returns:
        size 2
      • elements

        public com.google.common.collect.ImmutableList<java.lang.Object> elements()
        Gets the elements from this pair as a list.

        The list returns each element in the pair in order.

        Specified by:
        elements in interface Tuple
        Returns:
        the elements as an immutable list
      • compareTo

        public int compareTo​(Pair<A,​B> other)
        Compares the pair based on the first element followed by the second element.

        The element types must be Comparable.

        Specified by:
        compareTo in interface java.lang.Comparable<A>
        Parameters:
        other - the other pair
        Returns:
        negative if this is less, zero if equal, positive if greater
        Throws:
        java.lang.ClassCastException - if either object is not comparable
      • toString

        public java.lang.String toString()
        Gets the pair using a standard string format.

        The standard format is '[$first, $second]'. Spaces around the values are trimmed.

        Overrides:
        toString in class java.lang.Object
        Returns:
        the pair as a string
      • meta

        public static Pair.Meta meta()
        The meta-bean for Pair.
        Returns:
        the meta-bean, not null
      • metaPair

        public static <R,​S> Pair.Meta<R,​S> metaPair​(java.lang.Class<R> cls1,
                                                                java.lang.Class<S> cls2)
        The meta-bean for Pair.
        Type Parameters:
        R - the first generic type
        S - the second generic type
        Parameters:
        cls1 - the first generic type
        cls2 - the second generic type
        Returns:
        the meta-bean, not null
      • getFirst

        public A getFirst()
        Gets the first element in this pair.
        Returns:
        the value of the property, not null
      • getSecond

        public B getSecond()
        Gets the second element in this pair.
        Returns:
        the value of the property, not null
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object