Class OpenFunctional
java.lang.Object
cloud.opencode.base.functional.OpenFunctional
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 Summary
Modifier and TypeMethodDescriptionstatic <T> CompletableFuture<T> Run async on Virtual Thread 在虚拟线程上异步运行static <T> Try<T> asyncTimeout(Supplier<T> supplier, Duration timeout) Run async with timeout 带超时异步运行static <T,U, R> Case <T, R> Create a type case 创建类型分支static <T,U> CheckedBiConsumer <T, U> checkedBiConsumer(CheckedBiConsumer<T, U> c) Create a CheckedBiConsumer 创建可抛异常的双参消费者static <T,U, R> CheckedBiFunction <T, U, R> checkedBiFunction(CheckedBiFunction<T, U, R> f) Create a CheckedBiFunction 创建可抛异常的双参函数static <T> CheckedConsumer<T> Create a CheckedConsumer 创建可抛异常的消费者static CheckedRunnableCreate a CheckedRunnable 创建可抛异常的运行器static <A,B, C> Function <A, C> Compose two functions 组合两个函数static <R extends Record>
RcopyRecord(R record, Map<String, Object> modifications) Copy a record with modifications 带修改复制 recordcurry(BiFunction<A, B, C> f) Curry a BiFunction 柯里化 BiFunctionstatic <T> Try<T> Create a failed Try 创建失败的 Trystatic <E,T> Validation <E, T> invalid(E error) Create an invalid Validation 创建无效的 Validationstatic <T> Lazy<T> Create a Lazy computation 创建惰性计算static <T> LazyAsync<T> Create a lazy async computation 创建惰性异步计算static <L,R> Either <L, R> left(L value) Create a Left Either 创建 Left Eitherstatic <S,A> Lens <S, A> lens(Function<S, A> getter, BiFunction<S, A, S> setter) Create a lens 创建透镜lift(CheckedFunction<T, R> f) Lift a checked function to return Option 将可抛异常函数提升为返回 OptionliftTry(CheckedFunction<T, R> f) Lift a checked function to return Try 将可抛异常函数提升为返回 Trystatic <T> OpenMatch.Matcher<T> match(T value) Start pattern matching 开始模式匹配static <T,R> Function <T, R> Memoize a function 记忆化函数static <T,R> Function <T, R> Memoize a function with custom cache size 记忆化函数(自定义缓存大小)static <T> Option<T> none()Create a None Option 创建 None Optionstatic <T> Option<T> option(T value) Create an Option from nullable value 从可空值创建 Optionstatic <S,A> OptionalLens <S, A> optionalLens(Function<S, Optional<A>> getter, BiFunction<S, A, S> setter) Create an optional lens 创建可选透镜static <T,R> List <R> parallelMap(List<T> items, Function<T, R> mapper) Parallel map over a list 并行映射列表static <T> PipeUtil.Pipe<T> pipe(T value) Start a pipe chain 开始管道链static <T> Pipeline.PipelineBuilder<T> pipeline(T value) Start a pipeline with a value 以值开始管道recordLens(Class<R> recordClass, String componentName) Create a lens for a record component 为 record 组件创建透镜recordToMap(Record record) Convert record to map 将 record 转换为 mapstatic <L,R> Either <L, R> right(R value) Create a Right Either 创建 Right Eitherstatic <T> Option<T> some(T value) Create a Some Option 创建 Some Optionstatic <T> Try<T> success(T value) Create a successful Try 创建成功的 Trystatic <T> Try<T> tryOf(CheckedSupplier<T> supplier) Create a Try from a supplier 从供应商创建 Trystatic <T,R> Pattern <T, R> typePattern(Class<R> type) Create a type pattern 创建类型模式static <T,U, R> BiFunction <T, U, R> uncheckedBiFunction(CheckedBiFunction<T, U, R> f) Convert a CheckedBiFunction to a standard BiFunction 将 CheckedBiFunction 转换为标准 BiFunctionstatic <T> Consumer<T> Convert CheckedConsumer to standard Consumer 将 CheckedConsumer 转换为标准 Consumerstatic RunnableConvert CheckedRunnable to standard Runnable 将 CheckedRunnable 转换为标准 Runnablestatic Unitunit()Get the Unit instance 获取 Unit 实例static <E,T> Validation <E, T> valid(T value) Create a valid Validation 创建有效的 Validationstatic <T,R> Case <T, R> Create a predicate case 创建谓词分支
-
Method Details
-
tryOf
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
Create a successful Try 创建成功的 Try- Type Parameters:
T- result type - 结果类型- Parameters:
value- the value - 值- Returns:
- successful Try
-
failure
-
left
Create a Left Either 创建 Left Either- Type Parameters:
L- left type - 左类型R- right type - 右类型- Parameters:
value- the left value - 左值- Returns:
- Left Either
-
right
Create a Right Either 创建 Right Either- Type Parameters:
L- left type - 左类型R- right type - 右类型- Parameters:
value- the right value - 右值- Returns:
- Right Either
-
some
Create a Some Option 创建 Some Option- Type Parameters:
T- value type - 值类型- Parameters:
value- the value (non-null) - 值(非空)- Returns:
- Some Option
-
none
Create a None Option 创建 None Option- Type Parameters:
T- value type - 值类型- Returns:
- None Option
-
option
Create an Option from nullable value 从可空值创建 Option- Type Parameters:
T- value type - 值类型- Parameters:
value- nullable value - 可空值- Returns:
- Some if non-null, None otherwise
-
valid
Create a valid Validation 创建有效的 Validation- Type Parameters:
E- error type - 错误类型T- value type - 值类型- Parameters:
value- the value - 值- Returns:
- valid Validation
-
invalid
Create an invalid Validation 创建无效的 Validation- Type Parameters:
E- error type - 错误类型T- value type - 值类型- Parameters:
error- the error - 错误- Returns:
- invalid Validation
-
lazy
-
match
Start pattern matching 开始模式匹配- Type Parameters:
T- value type - 值类型- Parameters:
value- value to match - 要匹配的值- Returns:
- Matcher for fluent API
-
typePattern
-
caseOf
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
-
compose
-
curry
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
-
memoize
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
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
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
Convert a CheckedBiFunction to a standard BiFunction 将 CheckedBiFunction 转换为标准 BiFunctionChecked 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
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 - 可选 gettersetter- setter function - setter 函数- Returns:
- optional lens
-
pipeline
Start a pipeline with a value 以值开始管道- Type Parameters:
T- value type - 值类型- Parameters:
value- starting value - 起始值- Returns:
- pipeline builder
-
pipe
Start a pipe chain 开始管道链- Type Parameters:
T- value type - 值类型- Parameters:
value- starting value - 起始值- Returns:
- pipe holder
-
async
Run async on Virtual Thread 在虚拟线程上异步运行- Type Parameters:
T- result type - 结果类型- Parameters:
supplier- computation - 计算- Returns:
- CompletableFuture
-
asyncTimeout
-
lazyAsync
-
parallelMap
-
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
-
recordToMap
-
checkedConsumer
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
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
Create a CheckedRunnable 创建可抛异常的运行器- Parameters:
r- the checked runnable - 可抛异常运行器- Returns:
- CheckedRunnable
- Since:
- JDK 25, opencode-base-functional V1.0.3
-
uncheckedRunnable
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
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
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
Get the Unit instance 获取 Unit 实例- Returns:
- Unit.INSTANCE
- Since:
- JDK 25, opencode-base-functional V1.0.3
-