Class Pipeline<T,R>

java.lang.Object
cloud.opencode.base.functional.pipeline.Pipeline<T,R>
Type Parameters:
T - input type - 输入类型
R - output type - 输出类型

public final class Pipeline<T,R> extends Object
Pipeline - Composable data transformation pipeline Pipeline - 可组合的数据转换管道

A fluent API for building transformation pipelines that process data through a series of stages. Supports filtering, mapping, flat-mapping, and error handling.

用于构建转换管道的流式 API,通过一系列阶段处理数据。支持过滤、映射、 扁平映射和错误处理。

Features | 主要功能:

  • Fluent chainable operations - 流式可链接操作
  • Type-safe transformations - 类型安全转换
  • Error handling integration - 错误处理集成
  • Lazy evaluation option - 惰性求值选项
  • Collection processing - 集合处理

Usage Examples | 使用示例:

// Simple transformation pipeline
String result = Pipeline.of("  hello world  ")
    .map(String::trim)
    .map(String::toUpperCase)
    .filter(s -> s.length() > 0)
    .execute();

// Pipeline with error handling
Try<Integer> parsed = Pipeline.of(userInput)
    .map(String::trim)
    .filter(s -> !s.isEmpty())
    .mapTry(Integer::parseInt)
    .executeTry();

// Collection pipeline
List<String> names = Pipeline.ofCollection(users)
    .filter(User::isActive)
    .map(User::getName)
    .map(String::toUpperCase)
    .toList();

// Pipeline composition
Pipeline<String, Integer> parseAndValidate = Pipeline.<String>identity()
    .map(String::trim)
    .mapTry(Integer::parseInt);

Performance | 性能特性:

  • Each stage is O(1) to add - 每个阶段添加为 O(1)
  • Execution is O(n) for n stages - 执行为 O(n) n 个阶段
  • Memory: proportional to pipeline length - 内存: 与管道长度成正比

Security | 安全性:

  • Thread-safe: No (not designed for concurrent modification) - 线程安全: 否
  • Null-safe: Configurable null handling - 空值安全: 可配置空值处理
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

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

      public static <T> Pipeline<T,T> identity()
      Create an identity pipeline 创建恒等管道
      Type Parameters:
      T - type - 类型
      Returns:
      identity pipeline
    • from

      public static <T,R> Pipeline<T,R> from(Function<T,R> function)
      Create a pipeline from a function 从函数创建管道
      Type Parameters:
      T - input type - 输入类型
      R - output type - 输出类型
      Parameters:
      function - the function - 函数
      Returns:
      pipeline wrapping the function
    • ofCollection

      public static <T> Pipeline.CollectionPipeline<T,T> ofCollection(Collection<T> collection)
      Create a collection pipeline 创建集合管道
      Type Parameters:
      T - element type - 元素类型
      Parameters:
      collection - the collection - 集合
      Returns:
      collection pipeline builder
    • apply

      public R apply(T input)
      Apply the pipeline to a value 将管道应用于值
      Parameters:
      input - the input value - 输入值
      Returns:
      transformed value
    • applyTry

      public Try<R> applyTry(T input)
      Apply the pipeline safely, returning Try 安全地应用管道,返回 Try
      Parameters:
      input - the input value - 输入值
      Returns:
      Try containing result or exception
    • andThen

      public <U> Pipeline<T,U> andThen(Function<? super R, ? extends U> mapper)
      Chain another transformation 链接另一个转换
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function - 转换函数
      Returns:
      extended pipeline
    • compose

      public <V> Pipeline<V,R> compose(Pipeline<V,T> before)
      Compose with another pipeline 与另一个管道组合
      Type Parameters:
      V - input type - 输入类型
      Parameters:
      before - pipeline to apply first - 先应用的管道
      Returns:
      composed pipeline
    • toFunction

      public Function<T,R> toFunction()
      Get the underlying function 获取底层函数
      Returns:
      the function - 函数