Class OpenFunctional

java.lang.Object
cloud.opencode.base.functional.OpenFunctional

public final class OpenFunctional extends Object
OpenFunctional - Unified entry point for functional programming utilities OpenFunctional - 函数式编程工具的统一入口点

Provides convenient static methods to access all functional programming features in the opencode-base-functional module. This is the main entry point for using functional utilities.

提供便捷的静态方法来访问 opencode-base-functional 模块中的所有函数式编程功能。 这是使用函数式工具的主入口点。

Module Overview | 模块概览:

  • Monad types: Try, Either, Option, Validation, Lazy - Monad 类型
  • Pattern matching: OpenMatch with fluent API - 模式匹配
  • Function utilities: compose, curry, memoize - 函数工具
  • Optics: Lens for immutable updates - 透镜用于不可变更新
  • Pipeline: Data transformation chains - 管道用于数据转换链
  • Async: Virtual Thread utilities - 异步虚拟线程工具
  • Record: Record manipulation utilities - Record 操作工具

Usage Examples | 使用示例:

// Try monad for exception handling
Try<Integer> result = OpenFunctional.tryOf(() -> Integer.parseInt(input));

// Either for error handling
Either<String, User> user = OpenFunctional.right(new User("Alice"));

// Option for nullable values
Option<String> name = OpenFunctional.some("value");

// Pattern matching
String type = OpenFunctional.match(value)
    .caseOf(String.class, s -> "String: " + s)
    .caseOf(Integer.class, n -> "Number: " + n)
    .orElse(o -> "Unknown");

// Function composition
Function<String, Integer> parseAndDouble = OpenFunctional.compose(
    Integer::parseInt,
    n -> n * 2
);

// Lens for immutable updates
Lens<Person, String> nameLens = OpenFunctional.lens(
    Person::name,
    (p, n) -> new Person(n, p.age())
);

// Async with Virtual Threads
CompletableFuture<Data> future = OpenFunctional.async(() -> fetchData());

// Pipeline transformations
String result = OpenFunctional.pipe("  hello  ")
    .then(String::trim)
    .then(String::toUpperCase)
    .get();

Design Philosophy | 设计理念:

  • Immutability - All transformations return new values - 不可变性 - 所有转换返回新值
  • Type safety - Generic types prevent runtime errors - 类型安全 - 泛型防止运行时错误
  • Null safety - Option/Try prevent NPEs - 空值安全 - Option/Try 防止 NPE
  • Composability - Functions and types compose naturally - 可组合性 - 函数和类型自然组合

Security | 安全性:

  • Thread-safe: All methods are thread-safe - 线程安全: 所有方法线程安全
  • No side effects: Pure functions by default - 无副作用: 默认纯函数

Features | 主要功能:

  • Functional programming entry point with Try, Either, Option monads - 函数式编程入口,提供Try、Either、Option单子
  • Pattern matching with type-safe case expressions - 类型安全的模式匹配
  • Lazy evaluation and memoization support - 惰性求值与记忆化支持
  • Pipeline composition for function chaining - 管道组合用于函数链式调用
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • tryOf

      public static <T> Try<T> tryOf(CheckedSupplier<T> supplier)
      Create a Try from a supplier 从供应商创建 Try
      Type Parameters:
      T - result type - 结果类型
      Parameters:
      supplier - computation that may throw - 可能抛出的计算
      Returns:
      Try containing result or exception
    • success

      public static <T> Try<T> success(T value)
      Create a successful Try 创建成功的 Try
      Type Parameters:
      T - result type - 结果类型
      Parameters:
      value - the value - 值
      Returns:
      successful Try
    • failure

      public static <T> Try<T> failure(Throwable throwable)
      Create a failed Try 创建失败的 Try
      Type Parameters:
      T - result type - 结果类型
      Parameters:
      throwable - the exception - 异常
      Returns:
      failed Try
    • left

      public static <L,R> Either<L,R> left(L value)
      Create a Left Either 创建 Left Either
      Type Parameters:
      L - left type - 左类型
      R - right type - 右类型
      Parameters:
      value - the left value - 左值
      Returns:
      Left Either
    • right

      public static <L,R> Either<L,R> right(R value)
      Create a Right Either 创建 Right Either
      Type Parameters:
      L - left type - 左类型
      R - right type - 右类型
      Parameters:
      value - the right value - 右值
      Returns:
      Right Either
    • some

      public static <T> Option<T> some(T value)
      Create a Some Option 创建 Some Option
      Type Parameters:
      T - value type - 值类型
      Parameters:
      value - the value (non-null) - 值(非空)
      Returns:
      Some Option
    • none

      public static <T> Option<T> none()
      Create a None Option 创建 None Option
      Type Parameters:
      T - value type - 值类型
      Returns:
      None Option
    • option

      public static <T> Option<T> option(T value)
      Create an Option from nullable value 从可空值创建 Option
      Type Parameters:
      T - value type - 值类型
      Parameters:
      value - nullable value - 可空值
      Returns:
      Some if non-null, None otherwise
    • valid

      public static <E,T> Validation<E,T> valid(T value)
      Create a valid Validation 创建有效的 Validation
      Type Parameters:
      E - error type - 错误类型
      T - value type - 值类型
      Parameters:
      value - the value - 值
      Returns:
      valid Validation
    • invalid

      public static <E,T> Validation<E,T> invalid(E error)
      Create an invalid Validation 创建无效的 Validation
      Type Parameters:
      E - error type - 错误类型
      T - value type - 值类型
      Parameters:
      error - the error - 错误
      Returns:
      invalid Validation
    • lazy

      public static <T> Lazy<T> lazy(Supplier<T> supplier)
      Create a Lazy computation 创建惰性计算
      Type Parameters:
      T - result type - 结果类型
      Parameters:
      supplier - deferred computation - 延迟的计算
      Returns:
      Lazy container
    • match

      public static <T> OpenMatch.Matcher<T> match(T value)
      Start pattern matching 开始模式匹配
      Type Parameters:
      T - value type - 值类型
      Parameters:
      value - value to match - 要匹配的值
      Returns:
      Matcher for fluent API
    • typePattern

      public static <T,R> Pattern<T,R> typePattern(Class<R> type)
      Create a type pattern 创建类型模式
      Type Parameters:
      T - input type - 输入类型
      R - matched type - 匹配的类型
      Parameters:
      type - type to match - 要匹配的类型
      Returns:
      type pattern
    • caseOf

      public static <T,U,R> Case<T,R> caseOf(Class<U> type, Function<? super U, ? extends R> action)
      Create a type case 创建类型分支
      Type Parameters:
      T - input type - 输入类型
      U - matched type - 匹配的类型
      R - result type - 结果类型
      Parameters:
      type - type to match - 要匹配的类型
      action - action on match - 匹配时的动作
      Returns:
      type case
    • when

      public static <T,R> Case<T,R> when(Predicate<? super T> predicate, Function<? super T, ? extends R> action)
      Create a predicate case 创建谓词分支
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      predicate - condition - 条件
      action - action on match - 匹配时的动作
      Returns:
      predicate case
    • compose

      public static <A,B,C> Function<A,C> compose(Function<A,B> f, Function<B,C> g)
      Compose two functions 组合两个函数
      Type Parameters:
      A - input type - 输入类型
      B - intermediate type - 中间类型
      C - output type - 输出类型
      Parameters:
      f - first function - 第一个函数
      g - second function - 第二个函数
      Returns:
      composed function
    • curry

      public static <A,B,C> Function<A, Function<B,C>> curry(BiFunction<A,B,C> f)
      Curry a BiFunction 柯里化 BiFunction
      Type Parameters:
      A - first argument type - 第一个参数类型
      B - second argument type - 第二个参数类型
      C - result type - 结果类型
      Parameters:
      f - the function - 函数
      Returns:
      curried function
    • memoize

      public static <T,R> Function<T,R> memoize(Function<T,R> f)
      Memoize a function 记忆化函数
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      f - the function - 函数
      Returns:
      memoized function
    • memoize

      public static <T,R> Function<T,R> memoize(Function<T,R> f, int maxSize)
      Memoize a function with custom cache size 记忆化函数(自定义缓存大小)
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      f - the function - 函数
      maxSize - maximum cache size (LRU eviction) - 最大缓存大小(LRU 驱逐)
      Returns:
      memoized function with bounded cache
    • checkedBiFunction

      public static <T,U,R> CheckedBiFunction<T,U,R> checkedBiFunction(CheckedBiFunction<T,U,R> f)
      Create a CheckedBiFunction 创建可抛异常的双参函数

      Allows using lambdas that throw checked exceptions with BiFunction-like operations.

      允许在 BiFunction 类操作中使用抛出受检异常的 lambda。

      Type Parameters:
      T - first input type - 第一个输入类型
      U - second input type - 第二个输入类型
      R - result type - 结果类型
      Parameters:
      f - the checked function - 可抛异常函数
      Returns:
      CheckedBiFunction
    • checkedBiConsumer

      public static <T,U> CheckedBiConsumer<T,U> checkedBiConsumer(CheckedBiConsumer<T,U> c)
      Create a CheckedBiConsumer 创建可抛异常的双参消费者

      Allows using lambdas that throw checked exceptions with BiConsumer-like operations.

      允许在 BiConsumer 类操作中使用抛出受检异常的 lambda。

      Type Parameters:
      T - first input type - 第一个输入类型
      U - second input type - 第二个输入类型
      Parameters:
      c - the checked consumer - 可抛异常消费者
      Returns:
      CheckedBiConsumer
    • uncheckedBiFunction

      public static <T,U,R> BiFunction<T,U,R> uncheckedBiFunction(CheckedBiFunction<T,U,R> f)
      Convert a CheckedBiFunction to a standard BiFunction 将 CheckedBiFunction 转换为标准 BiFunction

      Checked exceptions are wrapped in RuntimeException.

      受检异常被包装为 RuntimeException。

      Type Parameters:
      T - first input type - 第一个输入类型
      U - second input type - 第二个输入类型
      R - result type - 结果类型
      Parameters:
      f - the checked function - 可抛异常函数
      Returns:
      standard BiFunction
    • lens

      public static <S,A> Lens<S,A> lens(Function<S,A> getter, BiFunction<S,A,S> setter)
      Create a lens 创建透镜
      Type Parameters:
      S - source type - 源类型
      A - target type - 目标类型
      Parameters:
      getter - getter function - getter 函数
      setter - setter function - setter 函数
      Returns:
      lens
    • optionalLens

      public static <S,A> OptionalLens<S,A> optionalLens(Function<S, Optional<A>> getter, BiFunction<S,A,S> setter)
      Create an optional lens 创建可选透镜
      Type Parameters:
      S - source type - 源类型
      A - target type - 目标类型
      Parameters:
      getter - optional getter - 可选 getter
      setter - setter function - setter 函数
      Returns:
      optional lens
    • pipeline

      public static <T> Pipeline.PipelineBuilder<T> pipeline(T value)
      Start a pipeline with a value 以值开始管道
      Type Parameters:
      T - value type - 值类型
      Parameters:
      value - starting value - 起始值
      Returns:
      pipeline builder
    • pipe

      public static <T> PipeUtil.Pipe<T> pipe(T value)
      Start a pipe chain 开始管道链
      Type Parameters:
      T - value type - 值类型
      Parameters:
      value - starting value - 起始值
      Returns:
      pipe holder
    • async

      public static <T> CompletableFuture<T> async(Supplier<T> supplier)
      Run async on Virtual Thread 在虚拟线程上异步运行
      Type Parameters:
      T - result type - 结果类型
      Parameters:
      supplier - computation - 计算
      Returns:
      CompletableFuture
    • asyncTimeout

      public static <T> Try<T> asyncTimeout(Supplier<T> supplier, Duration timeout)
      Run async with timeout 带超时异步运行
      Type Parameters:
      T - result type - 结果类型
      Parameters:
      supplier - computation - 计算
      timeout - timeout duration - 超时时长
      Returns:
      Try containing result
    • lazyAsync

      public static <T> LazyAsync<T> lazyAsync(Supplier<T> supplier)
      Create a lazy async computation 创建惰性异步计算
      Type Parameters:
      T - result type - 结果类型
      Parameters:
      supplier - computation - 计算
      Returns:
      LazyAsync container
    • parallelMap

      public static <T,R> List<R> parallelMap(List<T> items, Function<T,R> mapper)
      Parallel map over a list 并行映射列表
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      items - items to process - 要处理的项目
      mapper - function to apply - 要应用的函数
      Returns:
      list of results
    • recordLens

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

      public static <R extends Record> R copyRecord(R record, Map<String,Object> modifications)
      Copy a record with modifications 带修改复制 record
      Type Parameters:
      R - record type - record 类型
      Parameters:
      record - the record - record
      modifications - modifications map - 修改映射
      Returns:
      copied record
    • recordToMap

      public static Map<String,Object> recordToMap(Record record)
      Convert record to map 将 record 转换为 map
      Parameters:
      record - the record - record
      Returns:
      map of components
    • checkedConsumer

      public static <T> CheckedConsumer<T> checkedConsumer(CheckedConsumer<T> c)
      Create a CheckedConsumer 创建可抛异常的消费者
      Type Parameters:
      T - input type - 输入类型
      Parameters:
      c - the checked consumer - 可抛异常消费者
      Returns:
      CheckedConsumer
      Since:
      JDK 25, opencode-base-functional V1.0.3
    • uncheckedConsumer

      public static <T> Consumer<T> uncheckedConsumer(CheckedConsumer<T> c)
      Convert CheckedConsumer to standard Consumer 将 CheckedConsumer 转换为标准 Consumer
      Type Parameters:
      T - input type - 输入类型
      Parameters:
      c - the checked consumer - 可抛异常消费者
      Returns:
      standard Consumer
      Since:
      JDK 25, opencode-base-functional V1.0.3
    • checkedRunnable

      public static CheckedRunnable checkedRunnable(CheckedRunnable r)
      Create a CheckedRunnable 创建可抛异常的运行器
      Parameters:
      r - the checked runnable - 可抛异常运行器
      Returns:
      CheckedRunnable
      Since:
      JDK 25, opencode-base-functional V1.0.3
    • uncheckedRunnable

      public static Runnable uncheckedRunnable(CheckedRunnable r)
      Convert CheckedRunnable to standard Runnable 将 CheckedRunnable 转换为标准 Runnable
      Parameters:
      r - the checked runnable - 可抛异常运行器
      Returns:
      standard Runnable
      Since:
      JDK 25, opencode-base-functional V1.0.3
    • lift

      public static <T,R> Function<T, Option<R>> lift(CheckedFunction<T,R> f)
      Lift a checked function to return Option 将可抛异常函数提升为返回 Option
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      f - the checked function - 可抛异常函数
      Returns:
      function returning Option
      Since:
      JDK 25, opencode-base-functional V1.0.3
    • liftTry

      public static <T,R> Function<T,Try<R>> liftTry(CheckedFunction<T,R> f)
      Lift a checked function to return Try 将可抛异常函数提升为返回 Try
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      f - the checked function - 可抛异常函数
      Returns:
      function returning Try
      Since:
      JDK 25, opencode-base-functional V1.0.3
    • unit

      public static Unit unit()
      Get the Unit instance 获取 Unit 实例
      Returns:
      Unit.INSTANCE
      Since:
      JDK 25, opencode-base-functional V1.0.3