Record Class Triple<T>

java.lang.Object
java.lang.Record
com.yetnt.utils.tuple.Triple<T>
Type Parameters:
T - The type of the elements in the triple.
Record Components:
v1 - The first value
v2 - The second value
v3 - The third value

public record Triple<T>(T v1, T v2, T v3) extends Record
An immutable record representing a tuple of three elements of the same type.

From J3Engine

Author:
Lehlogonolo Poole
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final T
    The field for the v1 record component.
    private final T
    The field for the v2 record component.
    private final T
    The field for the v3 record component.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Triple(T v1, T v2, T v3)
    Creates an instance of a Triple record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    static <T, V> void
    forEachPair(Triple<T> t1, Triple<V> t2, BiConsumer<T,V> forEachConsumer)
    Applies the given consumer to each positional pair of the two input triples.
    static <T> Triple<T>
    from(List<T> list)
    Creates a new Triple record from a given list
    final int
    Returns a hash code value for this object.
    <T2> Triple<T2>
    map(Function<T,T2> mapper)
    Applies a given mapping function to each element of this Triple, returning a new Triple with the transformed elements.
    static <T, U, V> Triple<V>
    mapPair(Triple<T> t1, Triple<U> t2, BiFunction<T,U,V> mapper)
    Maps each positional pair of the two input triples back into the same position of a new triple.
    <K> K
    mapTo(TriFunction<T,T,T,K> map)
    Applies the given function to the triple to return a single value.
    Creates a stream of the 3 elements
    Converts this Triple into an ArrayList containing its three elements.
    final String
    Returns a string representation of this record class.
    v1()
    Returns the value of the v1 record component.
    v2()
    Returns the value of the v2 record component.
    v3()
    Returns the value of the v3 record component.

    Methods inherited from class java.lang.Object

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

    • v1

      private final T v1
      The field for the v1 record component.
    • v2

      private final T v2
      The field for the v2 record component.
    • v3

      private final T v3
      The field for the v3 record component.
  • Constructor Details

    • Triple

      public Triple(T v1, T v2, T v3)
      Creates an instance of a Triple record class.
      Parameters:
      v1 - the value for the v1 record component
      v2 - the value for the v2 record component
      v3 - the value for the v3 record component
  • Method Details

    • from

      public static <T> Triple<T> from(List<T> list) throws IllegalArgumentException
      Creates a new Triple record from a given list
      Type Parameters:
      T - The type held by the list
      Parameters:
      list - The list. Should have at least 3 elements
      Returns:
      A new Triple with the 3 elements
      Throws:
      IllegalArgumentException - If the list is empty or the list has less than 3 elements.
    • mapTo

      public <K> K mapTo(TriFunction<T,T,T,K> map)
      Applies the given function to the triple to return a single value.
      Type Parameters:
      K - The type of the single value to return
      Parameters:
      map - The mapper function
      Returns:
      A single value
    • toArrayList

      public ArrayList<T> toArrayList()
      Converts this Triple into an ArrayList containing its three elements. The order of elements in the list will be v1, v2, v3.
      Returns:
      An ArrayList containing the elements of this Triple.
    • map

      public <T2> Triple<T2> map(Function<T,T2> mapper)
      Applies a given mapping function to each element of this Triple, returning a new Triple with the transformed elements.
      Type Parameters:
      T2 - The type of the elements in the new Triple after mapping.
      Parameters:
      mapper - The function to apply to each element.
      Returns:
      A new Triple containing the results of applying the mapper function to each element.
    • stream

      public Stream<T> stream()
      Creates a stream of the 3 elements

      This is functionally identical to calling

      
           Triple<String> triple = new Triple("hello", "world", "!");
           Stream<String> str = Stream.of(triple.v1(), triple.v2(), triple.v3());
           

      Returns:
      A stream of the input stuff
    • forEachPair

      public static <T, V> void forEachPair(Triple<T> t1, Triple<V> t2, BiConsumer<T,V> forEachConsumer)
      Applies the given consumer to each positional pair of the two input triples.

      This is if, you were to take v1 of each Triple, and consume them with a BiConsumer, applied to each argument in one call.

      Type Parameters:
      T - The type the first triple holds
      V - The type the second triple holds
      Parameters:
      t1 - The first triple
      t2 - The second triple
      forEachConsumer - The consumer to apply to each positional argument
    • mapPair

      public static <T, U, V> Triple<V> mapPair(Triple<T> t1, Triple<U> t2, BiFunction<T,U,V> mapper)
      Maps each positional pair of the two input triples back into the same position of a new triple.

      This is if you were to take v1 of each Triple, apply the map then save that as v1 of a new triple, applied to each argument in one call.

      
       Triple<Integer> a = new Triple<>(1, 2, 3);
       Triple<Integer> b = new Triple<>(10, 20, 30);
      
       Triple<Integer> result =
           TripleUtils.mapPair(a, b, Integer::sum);
      
       // (11, 22, 33)
       
      Type Parameters:
      T - The type held by the first triple
      U - The type held by the second triple
      V - The type held by the new returned triple
      Parameters:
      t1 - The first triple
      t2 - The second triple
      mapper - The map to apply to each positional element of the triple
      Returns:
      A new triple
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • v1

      public T v1()
      Returns the value of the v1 record component.
      Returns:
      the value of the v1 record component
    • v2

      public T v2()
      Returns the value of the v2 record component.
      Returns:
      the value of the v2 record component
    • v3

      public T v3()
      Returns the value of the v3 record component.
      Returns:
      the value of the v3 record component