Class ImmutablePair<L,​R>

  • Type Parameters:
    L - the type of the left element
    R - the type of the right element
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<ImmutablePair<L,​R>>, java.util.Map.Entry<L,​R>

    public class ImmutablePair<L,​R>
    extends java.lang.Object
    implements java.util.Map.Entry<L,​R>, java.io.Serializable, java.lang.Comparable<ImmutablePair<L,​R>>
    Immutable representation of a 2-tuple of elements. 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. ThreadSafe if both paired objects are thread-safe
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected L left  
      protected R right  
    • Constructor Summary

      Constructors 
      Constructor Description
      ImmutablePair​(L left, R right)
      Create a pair of elements.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected int compare​(java.lang.Comparable original, java.lang.Comparable other)  
      int compareTo​(ImmutablePair<L,​R> o)
      Compares the pair based on the left element followed by the right element.
      boolean equals​(java.lang.Object obj)  
      L getKey()  
      L getLeft()  
      R getRight()  
      R getValue()  
      int hashCode()  
      R setValue​(R value)
      This is not allowed since the pair itself is immutable.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

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

      • left

        protected L left
      • right

        protected R right
    • Constructor Detail

      • ImmutablePair

        public ImmutablePair​(L left,
                             R right)
        Create a pair of elements.
        Parameters:
        left - the left element
        right - the right element
    • Method Detail

      • getLeft

        public L getLeft()
        Returns:
        the left element
      • getRight

        public R getRight()
        Returns:
        the right element
      • getKey

        public final L getKey()
        Specified by:
        getKey in interface java.util.Map.Entry<L,​R>
      • getValue

        public R getValue()
        Specified by:
        getValue in interface java.util.Map.Entry<L,​R>
      • setValue

        public R setValue​(R value)
        This is not allowed since the pair itself is immutable.
        Specified by:
        setValue in interface java.util.Map.Entry<L,​R>
        Returns:
        never
        Throws:
        java.lang.UnsupportedOperationException
      • compareTo

        public int compareTo​(ImmutablePair<L,​R> o)
        Compares the pair based on the left element followed by the right element. The types must be Comparable.
        Specified by:
        compareTo in interface java.lang.Comparable<L>
        Parameters:
        other - the other pair, not null
        Returns:
        negative if this is less, zero if equal, positive if greater
      • compare

        protected int compare​(java.lang.Comparable original,
                              java.lang.Comparable other)
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Map.Entry<L,​R>
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map.Entry<L,​R>
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object