Class Object2<E>

java.lang.Object
civitas.celestis.util.tuple.Object2<E>
Type Parameters:
E - The type of element to contain
All Implemented Interfaces:
BaseTuple<E>, Tuple<E>, Serializable, Iterable<E>
Direct Known Subclasses:
Decimal2, Integer2

public class Object2<E> extends Object implements Tuple<E>
A shallowly immutable pair of objects.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final E
    The first element of this tuple.
    protected final E
    The second element of this tuple.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Object2(Tuple<? extends E> t)
    Creates a new pair.
    Object2(E[] elements)
    Creates a new pair.
    Object2(E a, E b)
    Creates a new pair.
  • Method Summary

    Modifier and Type
    Method
    Description
    a()
    Returns the first element of this tuple.
    E[]
    Returns an array containing the elements of this tuple in their proper order.
    b()
    Returns the second element of this tuple.
    boolean
    Returns whether this tuple contains the provided object obj.
    boolean
    Returns whether this tuple contains multiple objects.
    boolean
    Checks for equality between this tuple and the provided object obj.
    void
    forEach(BiConsumer<? super Integer,? super E> a)
    Executes the provided action a once for each element of this tuple.
    void
    forEach(Consumer<? super E> a)
    Executes the provided action a once for each element of this tuple.
    get(int i)
    Returns the ith element of this tuple.
    int
    Returns the hash code of this instance.
    Returns an iterator over every element of this tuple.
    Returns an unmodifiable list containing the elements of this tuple in their proper order.
    <F> Tuple<F>
    map(Function<? super E,? extends F> f)
    Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
    Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
    Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
    mapToInt(ToIntFunction<? super E> f)
    Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
    Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
    <F, G> Tuple<G>
    merge(Tuple<F> t, BiFunction<? super E,? super F,? extends G> f)
    Between this tuple and the provided tuple t, this applies the merger function f to each corresponding pair of elements, then returns a new tuple containing the return values of the merger function f.
    int
    Returns the size of this tuple.
    Returns a stream whose source is the elements of this tuple.
    Serializes this tuple into a string.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    spliterator

    Methods inherited from interface civitas.celestis.util.tuple.Tuple

    filter, safeArray
  • Field Details

    • a

      protected final E a
      The first element of this tuple.
    • b

      protected final E b
      The second element of this tuple.
  • Constructor Details

    • Object2

      public Object2(E a, E b)
      Creates a new pair.
      Parameters:
      a - The first element of this pair
      b - The second element of this pair
    • Object2

      public Object2(@Nonnull E[] elements)
      Creates a new pair.
      Parameters:
      elements - An array containing the elements of this pair in AB order
      Throws:
      IllegalArgumentException - When the provided array's length is not 2
    • Object2

      public Object2(@Nonnull Tuple<? extends E> t)
      Creates a new pair.
      Parameters:
      t - The tuple of which to copy elements from
      Throws:
      IllegalArgumentException - When the provided tuple t's size is not 2
  • Method Details

    • size

      public int size()
      Returns the size of this tuple. (the number of components it has)
      Specified by:
      size in interface BaseTuple<E>
      Specified by:
      size in interface Tuple<E>
      Returns:
      The number of components this tuple has
    • contains

      public boolean contains(@Nullable Object obj)
      Returns whether this tuple contains the provided object obj.
      Specified by:
      contains in interface Tuple<E>
      Parameters:
      obj - The object of which to check for containment
      Returns:
      true if at least one element of this tuple is equal to the provided object obj
    • containsAll

      public boolean containsAll(@Nonnull Iterable<?> i)
      Returns whether this tuple contains multiple objects.
      Specified by:
      containsAll in interface Tuple<E>
      Parameters:
      i - The iterable object of which to check for containment
      Returns:
      true if this tuple contains every element of the iterable object
    • get

      public E get(int i) throws IndexOutOfBoundsException
      Returns the ith element of this tuple.
      Specified by:
      get in interface Tuple<E>
      Parameters:
      i - The index of the element to get
      Returns:
      The ith element of this tuple
      Throws:
      IndexOutOfBoundsException - When the index i is out of bounds
    • a

      public E a()
      Returns the first element of this tuple.
      Returns:
      The first element of this tuple
    • b

      public E b()
      Returns the second element of this tuple.
      Returns:
      The second element of this tuple
    • map

      @Nonnull public <F> Tuple<F> map(@Nonnull Function<? super E,? extends F> f)
      Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
      Specified by:
      map in interface Tuple<E>
      Type Parameters:
      F - The type of component to map this tuple to
      Parameters:
      f - The function of which to apply to each element of this tuple
      Returns:
      The resulting tuple
    • mapToDouble

      @Nonnull public DoubleTuple mapToDouble(@Nonnull ToDoubleFunction<? super E> f)
      Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
      Specified by:
      mapToDouble in interface Tuple<E>
      Parameters:
      f - The function of which to apply to each element of this tuple
      Returns:
      The resulting double tuple
    • mapToFloat

      @Nonnull public FloatTuple mapToFloat(@Nonnull ToFloatFunction<? super E> f)
      Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
      Specified by:
      mapToFloat in interface Tuple<E>
      Parameters:
      f - The function of which to apply to each element of this tuple
      Returns:
      The resulting double tuple
    • mapToLong

      @Nonnull public LongTuple mapToLong(@Nonnull ToLongFunction<? super E> f)
      Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
      Specified by:
      mapToLong in interface Tuple<E>
      Parameters:
      f - The function of which to apply to each element of this tuple
      Returns:
      The resulting long tuple
    • mapToInt

      @Nonnull public IntTuple mapToInt(@Nonnull ToIntFunction<? super E> f)
      Applies the provided mapper function f to each element of this tuple, then returns a new tuple containing the return values of the function f.
      Specified by:
      mapToInt in interface Tuple<E>
      Parameters:
      f - The function of which to apply to each element of this tuple
      Returns:
      The resulting int tuple
    • merge

      @Nonnull public <F, G> Tuple<G> merge(@Nonnull Tuple<F> t, @Nonnull BiFunction<? super E,? super F,? extends G> f) throws IllegalArgumentException
      Between this tuple and the provided tuple t, this applies the merger function f to each corresponding pair of elements, then returns a new tuple containing the return values of the merger function f. In other words, this merges the two tuples into a new tuple using the provided merger function f.
      Specified by:
      merge in interface Tuple<E>
      Type Parameters:
      F - The type of element to merge this tuple with
      G - The type of element to merge the two tuples to
      Parameters:
      t - The tuple of which to merge this tuple with
      f - The merger function to handle the merging of the two tuples
      Returns:
      The resulting tuple
      Throws:
      IllegalArgumentException - When the provided tuple t's size is different from that of this tuple's size
    • iterator

      @Nonnull public Iterator<E> iterator()
      Returns an iterator over every element of this tuple.
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Tuple<E>
      Returns:
      An iterator over every element of this tuple
    • forEach

      public void forEach(@Nonnull Consumer<? super E> a)
      Executes the provided action a once for each element of this tuple. A reference to the element is provided as the first parameter of the action.
      Specified by:
      forEach in interface Iterable<E>
      Specified by:
      forEach in interface Tuple<E>
      Parameters:
      a - The action to be performed for each element
    • forEach

      public void forEach(@Nonnull BiConsumer<? super Integer,? super E> a)
      Executes the provided action a once for each element of this tuple. The index of the element is provided as the first parameter of the action, and a reference to the element is provided as the second parameter of the action.
      Specified by:
      forEach in interface Tuple<E>
      Parameters:
      a - The action to be performed for each element
    • array

      @Nonnull public E[] array()
      Returns an array containing the elements of this tuple in their proper order.
      Specified by:
      array in interface Tuple<E>
      Returns:
      The array representation of this tuple
    • stream

      @Nonnull public Stream<E> stream()
      Returns a stream whose source is the elements of this tuple.
      Specified by:
      stream in interface Tuple<E>
      Returns:
      A stream whose source is the elements of this tuple
    • list

      @Nonnull public List<E> list()
      Returns an unmodifiable list containing the elements of this tuple in their proper order.
      Specified by:
      list in interface BaseTuple<E>
      Specified by:
      list in interface Tuple<E>
      Returns:
      The list representation of this tuple
    • equals

      public boolean equals(@Nullable Object obj)
      Checks for equality between this tuple and the provided object obj.
      Specified by:
      equals in interface Tuple<E>
      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare to
      Returns:
      true if the other object is also a tuple, and the number of elements, the order of the elements, and the elements' values are all equal
    • toString

      @Nonnull public String toString()
      Serializes this tuple into a string.
      Specified by:
      toString in interface Tuple<E>
      Overrides:
      toString in class Object
      Returns:
      The string representation of this tuple
    • hashCode

      public int hashCode()
      Returns the hash code of this instance.
      Overrides:
      hashCode in class Object
      Returns:
      The hash code of this instance