Class PipeUtil
java.lang.Object
cloud.opencode.base.functional.pipeline.PipeUtil
PipeUtil - Utility methods for pipeline operations
PipeUtil - 管道操作的工具方法
Provides static utility methods for common pipeline operations, including pipe operator simulation, conditional execution, and collection transformations.
提供常见管道操作的静态工具方法,包括管道操作符模拟、条件执行 和集合转换。
Features | 主要功能:
- Pipe operator (|>) simulation - 管道操作符模拟
- Conditional execution - 条件执行
- Tap/peek operations - 窥视操作
- Collection utilities - 集合工具
Usage Examples | 使用示例:
// Pipe operator style
String result = PipeUtil.pipe(" hello ")
.then(String::trim)
.then(String::toUpperCase)
.then(s -> s + "!")
.get();
// Conditional execution
String cleaned = PipeUtil.when(input != null, input, String::trim);
// Tap for side effects
String value = PipeUtil.tap(computeValue(), System.out::println);
// Transform collection
List<String> names = PipeUtil.transform(users, User::getName);
// Chain transformations
Function<String, Integer> parseLength = PipeUtil.chain(
String::trim,
String::length
);
Security | 安全性:
- Thread-safe: Yes (stateless methods) - 线程安全: 是 (无状态方法)
- Null-safe: Handles null with when/whenNonNull - 空值安全: 使用 when/whenNonNull 处理
Performance | 性能特性:
- Time complexity: O(1) for pipe/tap/when; O(n) for transform/filterAndTransform where n is collection size - 时间复杂度: pipe/tap/when 为 O(1);transform/filterAndTransform 为 O(n),n 为集合大小
- Space complexity: O(n) for collection operations, O(1) for scalar operations - 空间复杂度: 集合操作 O(n),标量操作 O(1)
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classPipe - Value holder for pipe operations Pipe - 管道操作的值持有者 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,R> R Apply a value to a function (reverse application) 将值应用于函数(反向应用)static <T,U, R> Function <T, R> Chain two functions 链接两个函数static <T,U, V, R> Function <T, R> chain(Function<? super T, ? extends U> first, Function<? super U, ? extends V> second, Function<? super V, ? extends R> third) Chain three functions 链接三个函数static <T,R> List <R> filterAndTransform(Collection<T> collection, Predicate<? super T> filter, Function<? super T, ? extends R> mapper) Filter and transform a collection 过滤并转换集合static <T> List<T> filterNonNull(Collection<T> collection) Filter a collection removing nulls 过滤集合移除 nullstatic <T> PipeUtil.Pipe<T> pipe(T value) Start a pipe chain with a value 以值开始管道链static <T> UnaryOperator<T> sequence(UnaryOperator<T>... transformations) Create a transformation that applies multiple transformations in order 创建按顺序应用多个转换的转换static <T> TExecute side effect and return value 执行副作用并返回值static <T> TtapIfPresent(T value, Consumer<? super T> consumer) Execute side effect if value is non-null 如果值非空则执行副作用static <T,R> List <R> transform(Collection<T> collection, Function<? super T, ? extends R> mapper) Transform a collection 转换集合static <T> Twhen(boolean condition, T value, UnaryOperator<T> function) Execute transformation conditionally 条件执行转换static <T> TwhenMatches(T value, Predicate<? super T> predicate, UnaryOperator<T> function) Execute transformation if predicate matches 如果谓词匹配则执行转换static <T,R> R whenNonNull(T value, Function<? super T, ? extends R> function) Execute transformation if value is non-null 如果值非空则执行转换static <T,R> R whenNonNullOrElse(T value, Function<? super T, ? extends R> function, R defaultValue) Execute transformation if value is non-null, with default 如果值非空则执行转换,带默认值
-
Method Details
-
pipe
Start a pipe chain with a value 以值开始管道链Simulates the pipe operator (|>) found in functional languages.
模拟函数式语言中的管道操作符 (|>)。
- Type Parameters:
T- value type - 值类型- Parameters:
value- starting value - 起始值- Returns:
- pipe holder for chaining
-
when
Execute transformation conditionally 条件执行转换- Type Parameters:
T- value type - 值类型- Parameters:
condition- condition to check - 要检查的条件value- value to transform - 要转换的值function- transformation function - 转换函数- Returns:
- transformed value if condition true, original otherwise
-
whenNonNull
Execute transformation if value is non-null 如果值非空则执行转换- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
value- value to transform - 要转换的值function- transformation function - 转换函数- Returns:
- transformed value or null
-
whenNonNullOrElse
public static <T,R> R whenNonNullOrElse(T value, Function<? super T, ? extends R> function, R defaultValue) Execute transformation if value is non-null, with default 如果值非空则执行转换,带默认值- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
value- value to transform - 要转换的值function- transformation function - 转换函数defaultValue- default if null - null 时的默认值- Returns:
- transformed value or default
-
whenMatches
Execute transformation if predicate matches 如果谓词匹配则执行转换- Type Parameters:
T- value type - 值类型- Parameters:
value- value to test and transform - 要测试和转换的值predicate- condition to test - 要测试的条件function- transformation function - 转换函数- Returns:
- transformed value if matches, original otherwise
-
tap
Execute side effect and return value 执行副作用并返回值Useful for logging or debugging in the middle of a chain.
在链中间进行日志记录或调试时很有用。
- Type Parameters:
T- value type - 值类型- Parameters:
value- the value - 值consumer- side effect - 副作用- Returns:
- the original value
-
tapIfPresent
Execute side effect if value is non-null 如果值非空则执行副作用- Type Parameters:
T- value type - 值类型- Parameters:
value- the value - 值consumer- side effect - 副作用- Returns:
- the original value
-
transform
public static <T,R> List<R> transform(Collection<T> collection, Function<? super T, ? extends R> mapper) Transform a collection 转换集合- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
collection- source collection - 源集合mapper- transformation - 转换- Returns:
- list of transformed elements
-
filterAndTransform
public static <T,R> List<R> filterAndTransform(Collection<T> collection, Predicate<? super T> filter, Function<? super T, ? extends R> mapper) Filter and transform a collection 过滤并转换集合- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
collection- source collection - 源集合filter- filter predicate - 过滤谓词mapper- transformation - 转换- Returns:
- list of filtered and transformed elements
-
filterNonNull
Filter a collection removing nulls 过滤集合移除 null- Type Parameters:
T- element type - 元素类型- Parameters:
collection- source collection - 源集合- Returns:
- list without nulls
-
chain
public static <T,U, Function<T,R> R> chain(Function<? super T, ? extends U> first, Function<? super U, ? extends R> second) Chain two functions 链接两个函数- Type Parameters:
T- input type - 输入类型U- intermediate type - 中间类型R- result type - 结果类型- Parameters:
first- first function - 第一个函数second- second function - 第二个函数- Returns:
- composed function
-
chain
public static <T,U, Function<T,V, R> R> chain(Function<? super T, ? extends U> first, Function<? super U, ? extends V> second, Function<? super V, ? extends R> third) Chain three functions 链接三个函数- Type Parameters:
T- input type - 输入类型U- first intermediate type - 第一个中间类型V- second intermediate type - 第二个中间类型R- result type - 结果类型- Parameters:
first- first function - 第一个函数second- second function - 第二个函数third- third function - 第三个函数- Returns:
- composed function
-
sequence
Create a transformation that applies multiple transformations in order 创建按顺序应用多个转换的转换- Type Parameters:
T- value type - 值类型- Parameters:
transformations- transformations to apply - 要应用的转换- Returns:
- composed transformation
-
apply
Apply a value to a function (reverse application) 将值应用于函数(反向应用)Allows writing value |> function style code.
允许编写 value |> function 风格的代码。
- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
value- the value - 值function- the function - 函数- Returns:
- function result
-