Class PipeUtil

java.lang.Object
cloud.opencode.base.functional.pipeline.PipeUtil

public final class PipeUtil extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static final class 
    Pipe - Value holder for pipe operations Pipe - 管道操作的值持有者
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T,R> R
    apply(T value, Function<? super T, ? extends R> function)
    Apply a value to a function (reverse application) 将值应用于函数(反向应用)
    static <T,U,R> Function<T,R>
    chain(Function<? super T, ? extends U> first, Function<? super U, ? extends R> second)
    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 过滤集合移除 null
    static <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> T
    tap(T value, Consumer<? super T> consumer)
    Execute side effect and return value 执行副作用并返回值
    static <T> T
    tapIfPresent(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> T
    when(boolean condition, T value, UnaryOperator<T> function)
    Execute transformation conditionally 条件执行转换
    static <T> T
    whenMatches(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 如果值非空则执行转换,带默认值

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • pipe

      public static <T> PipeUtil.Pipe<T> pipe(T value)
      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

      public static <T> T when(boolean condition, T value, UnaryOperator<T> function)
      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

      public static <T,R> R whenNonNull(T value, Function<? super T, ? extends R> function)
      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

      public static <T> T whenMatches(T value, Predicate<? super T> predicate, UnaryOperator<T> function)
      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

      public static <T> T tap(T value, Consumer<? super T> consumer)
      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

      public static <T> T tapIfPresent(T value, Consumer<? super T> consumer)
      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

      public static <T> List<T> filterNonNull(Collection<T> collection)
      Filter a collection removing nulls 过滤集合移除 null
      Type Parameters:
      T - element type - 元素类型
      Parameters:
      collection - source collection - 源集合
      Returns:
      list without nulls
    • chain

      public static <T,U,R> Function<T,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,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 链接三个函数
      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

      @SafeVarargs public static <T> UnaryOperator<T> sequence(UnaryOperator<T>... transformations)
      Create a transformation that applies multiple transformations in order 创建按顺序应用多个转换的转换
      Type Parameters:
      T - value type - 值类型
      Parameters:
      transformations - transformations to apply - 要应用的转换
      Returns:
      composed transformation
    • apply

      public static <T,R> R apply(T value, Function<? super T, ? extends R> function)
      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