Record Class Triple<A,B,C>

java.lang.Object
java.lang.Record
cloud.opencode.base.core.tuple.Triple<A,B,C>
Type Parameters:
A - first element type - 第一个元素类型
B - second element type - 第二个元素类型
C - third element type - 第三个元素类型
Record Components:
first - first element - 第一个元素
second - second element - 第二个元素
third - third element - 第三个元素
All Implemented Interfaces:
Serializable

public record Triple<A,B,C>(A first, B second, C third) extends Record implements Serializable
Triple - Immutable three-element tuple (Record implementation) 三元组 - 不可变的三元素元组(Record 实现)

Immutable container for three related values.

不可变的三值容器,可用于返回三个相关值。

Features | 主要功能:

  • Immutable storage for three values - 不可变的三值存储
  • Multiple aliases (first/second/third, left/middle/right) - 多种别名访问
  • Element mapping and transformation - 元素映射和转换
  • Extract to Pair (first two or last two) - 提取为 Pair

Usage Examples | 使用示例:

Triple<String, Integer, Boolean> triple = Triple.of("name", 25, true);
String first = triple.first();
Triple<String, String, Boolean> mapped = triple.mapSecond(String::valueOf);
Pair<String, Integer> pair = triple.toFirstPair();

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 线程安全: 是 (不可变 Record)
  • Null-safe: Allows null values - 空值安全: 允许 null 值
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Triple(A first, B second, C third)
    Creates an instance of a Triple record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Checks if all values are non-null 检查是否都非 null
    <R> R
    apply(TriFunction<? super A, ? super B, ? super C, ? extends R> function)
    Applies a tri-function 应用三元函数
    static <A,B,C> Triple<A,B,C>
    Creates an empty Triple 创建空三元组
    final boolean
    Indicates whether some other object is "equal to" this one.
    Returns the value of the first record component.
    final int
    Returns a hash code value for this object.
    boolean
    Checks if it contains a null value 检查是否包含 null 值
    Gets the left value (alias for first) 获取左值(第一个元素的别名)
    <T,U,V> Triple<T,U,V>
    map(Function<? super A, ? extends T> firstMapper, Function<? super B, ? extends U> secondMapper, Function<? super C, ? extends V> thirdMapper)
    Maps all elements simultaneously 同时映射所有元素
    <T> Triple<T,B,C>
    mapFirst(Function<? super A, ? extends T> mapper)
    Maps the first element 映射第一个元素
    <T> Triple<A,T,C>
    mapSecond(Function<? super B, ? extends T> mapper)
    Maps the second element 映射第二个元素
    <T> Triple<A,B,T>
    mapThird(Function<? super C, ? extends T> mapper)
    Maps the third element 映射第三个元素
    Gets the middle value (alias for second) 获取中值(第二个元素的别名)
    static <A,B,C> Triple<A,B,C>
    of(A first, B second, C third)
    Creates a Triple 创建三元组
    Gets the right value (alias for third) 获取右值(第三个元素的别名)
    Returns the value of the second record component.
    Returns the value of the third record component.
    Converts to an array 转换为数组
    Extracts the first two elements as a Pair 提取前两个元素为 Pair
    Extracts the last two elements as a Pair 提取后两个元素为 Pair
    Returns a string representation of this record class.

    Methods inherited from class Object

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

    • Triple

      public Triple(A first, B second, C third)
      Creates an instance of a Triple record class.
      Parameters:
      first - the value for the first record component
      second - the value for the second record component
      third - the value for the third record component
  • Method Details

    • of

      public static <A,B,C> Triple<A,B,C> of(A first, B second, C third)
      Creates a Triple 创建三元组
      Type Parameters:
      A - first element type | 第一个元素类型
      B - second element type | 第二个元素类型
      C - third element type | 第三个元素类型
      Parameters:
      first - the first element | 第一个元素
      second - the second element | 第二个元素
      third - the third element | 第三个元素
      Returns:
      the Triple | 三元组
    • empty

      public static <A,B,C> Triple<A,B,C> empty()
      Creates an empty Triple 创建空三元组
      Type Parameters:
      A - first element type | 第一个元素类型
      B - second element type | 第二个元素类型
      C - third element type | 第三个元素类型
      Returns:
      an empty Triple | 空三元组
    • left

      public A left()
      Gets the left value (alias for first) 获取左值(第一个元素的别名)
      Returns:
      the first element | 第一个元素
    • middle

      public B middle()
      Gets the middle value (alias for second) 获取中值(第二个元素的别名)
      Returns:
      the second element | 第二个元素
    • right

      public C right()
      Gets the right value (alias for third) 获取右值(第三个元素的别名)
      Returns:
      the third element | 第三个元素
    • mapFirst

      public <T> Triple<T,B,C> mapFirst(Function<? super A, ? extends T> mapper)
      Maps the first element 映射第一个元素
      Type Parameters:
      T - new type | 新类型
      Parameters:
      mapper - the mapper function | 映射函数
      Returns:
      a new Triple | 新三元组
    • mapSecond

      public <T> Triple<A,T,C> mapSecond(Function<? super B, ? extends T> mapper)
      Maps the second element 映射第二个元素
      Type Parameters:
      T - new type | 新类型
      Parameters:
      mapper - the mapper function | 映射函数
      Returns:
      a new Triple | 新三元组
    • mapThird

      public <T> Triple<A,B,T> mapThird(Function<? super C, ? extends T> mapper)
      Maps the third element 映射第三个元素
      Type Parameters:
      T - new type | 新类型
      Parameters:
      mapper - the mapper function | 映射函数
      Returns:
      a new Triple | 新三元组
    • map

      public <T,U,V> Triple<T,U,V> map(Function<? super A, ? extends T> firstMapper, Function<? super B, ? extends U> secondMapper, Function<? super C, ? extends V> thirdMapper)
      Maps all elements simultaneously 同时映射所有元素
      Type Parameters:
      T - new first element type | 新第一个元素类型
      U - new second element type | 新第二个元素类型
      V - new third element type | 新第三个元素类型
      Parameters:
      firstMapper - first element mapper function | 第一个元素映射函数
      secondMapper - second element mapper function | 第二个元素映射函数
      thirdMapper - third element mapper function | 第三个元素映射函数
      Returns:
      a new Triple | 新三元组
    • apply

      public <R> R apply(TriFunction<? super A, ? super B, ? super C, ? extends R> function)
      Applies a tri-function 应用三元函数
      Type Parameters:
      R - result type | 结果类型
      Parameters:
      function - the tri-function | 三元函数
      Returns:
      the function result | 函数结果
    • toFirstPair

      public Pair<A,B> toFirstPair()
      Extracts the first two elements as a Pair 提取前两个元素为 Pair
      Returns:
      Pair
    • toLastPair

      public Pair<B,C> toLastPair()
      Extracts the last two elements as a Pair 提取后两个元素为 Pair
      Returns:
      Pair
    • hasNull

      public boolean hasNull()
      Checks if it contains a null value 检查是否包含 null 值
      Returns:
      true if any value is null | 如果任一值为 null 返回 true
    • allNonNull

      public boolean allNonNull()
      Checks if all values are non-null 检查是否都非 null
      Returns:
      true if all values are non-null | 如果都非 null 返回 true
    • toArray

      public Object[] toArray()
      Converts to an array 转换为数组
      Returns:
      an array containing three elements | 包含三个元素的数组
    • toString

      public 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.
    • first

      public A first()
      Returns the value of the first record component.
      Returns:
      the value of the first record component
    • second

      public B second()
      Returns the value of the second record component.
      Returns:
      the value of the second record component
    • third

      public C third()
      Returns the value of the third record component.
      Returns:
      the value of the third record component