Class RecordUtil

java.lang.Object
cloud.opencode.base.functional.record.RecordUtil

public final class RecordUtil extends Object
RecordUtil - Utilities for working with Java Records RecordUtil - 用于处理 Java Record 的工具

Provides reflection-based and functional utilities for manipulating Java Records. Includes component access, modification, lens creation, and conversion utilities.

提供基于反射和函数式的工具来操作 Java Record。包括组件访问、 修改、透镜创建和转换工具。

Features | 主要功能:

  • Component introspection - 组件内省
  • Value extraction - 值提取
  • Record copying with modifications - 带修改的 Record 复制
  • Automatic lens generation - 自动透镜生成
  • Map conversion - Map 转换

Usage Examples | 使用示例:

record Person(String name, int age) {}

// Get component names
List<String> names = RecordUtil.componentNames(Person.class);
// ["name", "age"]

// Get component values
Person person = new Person("Alice", 30);
List<Object> values = RecordUtil.componentValues(person);
// ["Alice", 30]

// Copy with modification
Person older = RecordUtil.copy(person, Map.of("age", 31));
// Person[name=Alice, age=31]

// Create lens for component
Lens<Person, String> nameLens = RecordUtil.lens(Person.class, "name");
String name = nameLens.get(person);
Person renamed = nameLens.set(person, "Bob");

// Convert to/from Map
Map<String, Object> map = RecordUtil.toMap(person);
Person fromMap = RecordUtil.fromMap(Person.class, map);

// Check if class is record
boolean isRecord = RecordUtil.isRecord(Person.class); // true

Performance | 性能特性:

  • Reflection: Cached accessors - 反射: 缓存访问器
  • Copy: O(n) where n = components - 复制: O(n) n = 组件数
  • Lens: O(1) after creation - 透镜: 创建后 O(1)

Security | 安全性:

  • Thread-safe: Yes (utility methods) - 线程安全: 是 (工具方法)
  • Reflection: Uses standard accessors - 反射: 使用标准访问器
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • isRecord

      public static boolean isRecord(Class<?> clazz)
      Check if a class is a record 检查类是否为 record
      Parameters:
      clazz - the class - 类
      Returns:
      true if record
    • isRecordInstance

      public static boolean isRecordInstance(Object obj)
      Check if an object is a record instance 检查对象是否为 record 实例
      Parameters:
      obj - the object - 对象
      Returns:
      true if record instance
    • componentNames

      public static List<String> componentNames(Class<? extends Record> recordClass)
      Get the names of record components 获取 record 组件的名称
      Parameters:
      recordClass - the record class - record 类
      Returns:
      list of component names
    • componentTypes

      public static List<Class<?>> componentTypes(Class<? extends Record> recordClass)
      Get the types of record components 获取 record 组件的类型
      Parameters:
      recordClass - the record class - record 类
      Returns:
      list of component types
    • componentCount

      public static int componentCount(Class<? extends Record> recordClass)
      Get record component count 获取 record 组件数量
      Parameters:
      recordClass - the record class - record 类
      Returns:
      number of components
    • componentValues

      public static List<Object> componentValues(Record record)
      Get all component values from a record 从 record 获取所有组件值
      Parameters:
      record - the record - record
      Returns:
      list of values in component order
    • getComponent

      public static <T> T getComponent(Record record, String componentName)
      Get a specific component value by name 按名称获取特定组件值
      Type Parameters:
      T - value type - 值类型
      Parameters:
      record - the record - record
      componentName - component name - 组件名称
      Returns:
      the value
      Throws:
      IllegalArgumentException - if component not found
    • getComponentTry

      public static <T> Try<T> getComponentTry(Record record, String componentName)
      Get component value as Try 获取组件值为 Try
      Type Parameters:
      T - value type - 值类型
      Parameters:
      record - the record - record
      componentName - component name - 组件名称
      Returns:
      Try containing value
    • copy

      public static <R extends Record> R copy(R record, Map<String,Object> modifications)
      Copy a record with modifications 带修改复制 record
      Type Parameters:
      R - record type - record 类型
      Parameters:
      record - the record - record
      modifications - map of component name to new value - 组件名到新值的映射
      Returns:
      new record with modifications
    • copyWith

      public static <R extends Record> R copyWith(R record, String componentName, Object newValue)
      Copy a record modifying single component 复制 record 修改单个组件
      Type Parameters:
      R - record type - record 类型
      Parameters:
      record - the record - record
      componentName - component to modify - 要修改的组件
      newValue - new value - 新值
      Returns:
      new record with modification
    • copyTransforming

      public static <R extends Record, T> R copyTransforming(R record, String componentName, Function<T,T> transformer)
      Copy a record transforming a component 复制 record 转换组件
      Type Parameters:
      R - record type - record 类型
      T - component type - 组件类型
      Parameters:
      record - the record - record
      componentName - component to transform - 要转换的组件
      transformer - transformation function - 转换函数
      Returns:
      new record with transformed component
    • lens

      public static <R extends Record, T> Lens<R,T> lens(Class<R> recordClass, String componentName)
      Create a lens for a record component 为 record 组件创建透镜
      Type Parameters:
      R - record type - record 类型
      T - component type - 组件类型
      Parameters:
      recordClass - the record class - record 类
      componentName - component name - 组件名称
      Returns:
      lens for the component
    • lenses

      public static <R extends Record> Map<String, Lens<R,?>> lenses(Class<R> recordClass)
      Create lenses for all components 为所有组件创建透镜
      Type Parameters:
      R - record type - record 类型
      Parameters:
      recordClass - the record class - record 类
      Returns:
      map of component name to lens
    • toMap

      public static Map<String,Object> toMap(Record record)
      Convert record to Map 将 record 转换为 Map
      Parameters:
      record - the record - record
      Returns:
      map of component names to values
    • fromMap

      public static <R extends Record> R fromMap(Class<R> recordClass, Map<String,Object> map)
      Create record from Map 从 Map 创建 record
      Type Parameters:
      R - record type - record 类型
      Parameters:
      recordClass - the record class - record 类
      map - map of component names to values - 组件名到值的映射
      Returns:
      new record
    • toMapTry

      public static Try<Map<String,Object>> toMapTry(Record record)
      Convert record to Map as Try 将 record 转换为 Map 作为 Try
      Parameters:
      record - the record - record
      Returns:
      Try containing map
    • fromMapTry

      public static <R extends Record> Try<R> fromMapTry(Class<R> recordClass, Map<String,Object> map)
      Create record from Map as Try 从 Map 创建 record 作为 Try
      Type Parameters:
      R - record type - record 类型
      Parameters:
      recordClass - the record class - record 类
      map - map of values - 值的映射
      Returns:
      Try containing record
    • diff

      public static Map<String,Object[]> diff(Record r1, Record r2)
      Compare two records by components 按组件比较两个 record
      Parameters:
      r1 - first record - 第一个 record
      r2 - second record - 第二个 record
      Returns:
      map of differing components
    • factory

      public static <R extends Record> Function<Object[],R> factory(Class<R> recordClass)
      Create a factory function for a record type 为 record 类型创建工厂函数
      Type Parameters:
      R - record type - record 类型
      Parameters:
      recordClass - the record class - record 类
      Returns:
      factory function accepting array of values
    • factory2

      public static <R extends Record, A, B> BiFunction<A,B,R> factory2(Class<R> recordClass)
      Create a two-argument factory for records with 2 components 为有 2 个组件的 record 创建双参数工厂
      Type Parameters:
      R - record type - record 类型
      A - first component type - 第一个组件类型
      B - second component type - 第二个组件类型
      Parameters:
      recordClass - the record class - record 类
      Returns:
      BiFunction factory
    • hasComponent

      public static boolean hasComponent(Class<? extends Record> recordClass, String componentName)
      Check if record has component with name 检查 record 是否有指定名称的组件
      Parameters:
      recordClass - the record class - record 类
      componentName - component name - 组件名称
      Returns:
      true if component exists