Class Pair<A,​B>

  • Type Parameters:
    A - first object in pair
    B - second object in pair
    Direct Known Subclasses:
    ComparablePair

    public class Pair<A,​B>
    extends Object
    Custom pair class similar to C++. value
    Author:
    markr
    • Field Detail

      • first

        protected final A first
        The first object in this Pair
      • second

        protected final B second
        The second object in this Pair
    • Constructor Detail

      • Pair

        protected Pair​(A first,
                       B second)
        Constructor
        Parameters:
        first - first object in pair
        second - second object in pair
    • Method Detail

      • of

        public static <A,​B> Pair<A,​B> of​(A valueA,
                                                     B valueB)
        Factory method
        Type Parameters:
        A - type of valueA
        B - type of valueB
        Parameters:
        valueA - first
        valueB - second
        Returns:
        new pair
      • empty

        public static <A,​B> Pair<A,​B> empty()
        Create empty pair of nulls
        Type Parameters:
        A - typeA
        B - typeB
        Returns:
        null pair
      • copy

        public Pair<A,​B> copy()
        shallow copy of this pair
        Returns:
        shallow copy
      • first

        public A first()
        Get first object in pair
        Returns:
        first object in pair
      • second

        public B second()
        Get second object in pair
        Returns:
        second object in pair
      • anyIsNotNull

        public boolean anyIsNotNull()
        check if any of the two values is not null
        Returns:
        true when one of the entries is not null
      • bothNotNull

        public boolean bothNotNull()
        check if both values are not null
        Returns:
        true when both entries are not null
      • anyIsNull

        public boolean anyIsNull()
        check if any values are null
        Returns:
        true when any are null, false otherwise
      • isExactlyOneNonNull

        public boolean isExactlyOneNonNull()
        Returns:
        true when exactly one of the two is not null
      • getEarliestNonNull

        public Object getEarliestNonNull()
        Returns:
        earliest entry (first before second) that is nonNull, when both are null, null is returned
      • both

        public <T> void both​(Consumer<T> pairEntryConsumer)
        Apply consumer to both entries. Throws ClassCastException when pair contains entries not compatible with type parameter of consumer
        Type Parameters:
        T - consumer type assumed to be compatible with both pair entries
        Parameters:
        pairEntryConsumer - to apply
      • firstNotNull

        public boolean firstNotNull()
        Check if first is non null
        Returns:
        true when the case, false otherwise
      • secondNotNull

        public boolean secondNotNull()
        Check if second is non null
        Returns:
        true when the case, false otherwise
      • different

        public boolean different()
        Check if entries are not equal
        Returns:
        true when first does not equal second, false otherwise
      • bothNull

        public boolean bothNull()
        check both entries are null
        Returns:
        true when both null, false otherwise