Class LineProcessor

java.lang.Object
cloud.opencode.base.io.file.LineProcessor

public final class LineProcessor extends Object
Fluent Line-by-Line File Processor 流式逐行文件处理器

A fluent, functional line-by-line file processor that accumulates operations and applies them lazily when a terminal operation is called. Built on top of Files.lines(Path, Charset) for lazy evaluation.

流式的、函数式的逐行文件处理器,累积操作并在调用终端操作时惰性地应用它们。 基于Files.lines(Path, Charset)实现惰性求值。

Features | 主要功能:

  • Fluent API with immutable pipeline - 不可变管道的流式API
  • Lazy evaluation - 惰性求值
  • Filter, map, skip, limit operations - 过滤、映射、跳过、限制操作
  • Convenience shortcuts (trim, nonEmpty, grep) - 便捷快捷方式
  • Multiple terminal operations - 多种终端操作

Usage Examples | 使用示例:

// Read non-empty trimmed lines
List<String> lines = LineProcessor.of(path)
    .trim()
    .nonEmpty()
    .collect();

// Grep and limit
List<String> errors = LineProcessor.of(path)
    .grep("ERROR")
    .limit(100)
    .collect();

// Process and write to output
LineProcessor.of(input)
    .filter(line -> !line.startsWith("#"))
    .map(String::toUpperCase)
    .toFile(output);

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
Since:
JDK 25, opencode-base-io V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Tests whether any processed line matches the predicate 测试是否有任何处理后的行匹配谓词
    Collects all processed lines into a list 将所有处理后的行收集到列表中
    long
    Counts the number of processed lines 统计处理后的行数
    filter(Predicate<String> predicate)
    Filters lines matching the predicate 过滤匹配谓词的行
    Finds the first processed line 查找第一个处理后的行
    void
    Applies the action to each processed line 对每个处理后的行应用操作
    grep(String regex)
    Filters lines matching the given regex pattern 过滤匹配给定正则表达式模式的行
    limit(long maxLines)
    Limits the number of lines to process 限制要处理的行数
    Maps each line using the mapper function 使用映射函数映射每一行
    Filters out empty lines (shortcut for filter(s -> !s.isEmpty())) 过滤掉空行(filter(s -> !s.isEmpty())的快捷方式)
    of(String path)
    Creates a LineProcessor for the given path string with UTF-8 charset 使用UTF-8字符集为给定路径字符串创建LineProcessor
    of(Path path)
    Creates a LineProcessor for the given path with UTF-8 charset 使用UTF-8字符集为给定路径创建LineProcessor
    of(Path path, Charset charset)
    Creates a LineProcessor for the given path with specified charset 使用指定字符集为给定路径创建LineProcessor
    reduce(String identity, BinaryOperator<String> accumulator)
    Reduces the processed lines using the given identity and accumulator 使用给定的初始值和累加器归约处理后的行
    skip(long n)
    Skips the first N lines 跳过前N行
    void
    toFile(Path output)
    Writes the processed lines to an output file using UTF-8 使用UTF-8将处理后的行写入输出文件
    void
    toFile(Path output, Charset charset)
    Writes the processed lines to an output file with specified charset 使用指定字符集将处理后的行写入输出文件
    Trims whitespace from each line (shortcut for map(String::trim)) 去除每行的空白字符(map(String::trim)的快捷方式)

    Methods inherited from class Object

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

    • of

      public static LineProcessor of(Path path)
      Creates a LineProcessor for the given path with UTF-8 charset 使用UTF-8字符集为给定路径创建LineProcessor
      Parameters:
      path - the file path | 文件路径
      Returns:
      new LineProcessor instance | 新的LineProcessor实例
      Throws:
      NullPointerException - if path is null | 当path为null时抛出
    • of

      public static LineProcessor of(Path path, Charset charset)
      Creates a LineProcessor for the given path with specified charset 使用指定字符集为给定路径创建LineProcessor
      Parameters:
      path - the file path | 文件路径
      charset - the charset for reading | 读取使用的字符集
      Returns:
      new LineProcessor instance | 新的LineProcessor实例
      Throws:
      NullPointerException - if path or charset is null | 当path或charset为null时抛出
    • of

      public static LineProcessor of(String path)
      Creates a LineProcessor for the given path string with UTF-8 charset 使用UTF-8字符集为给定路径字符串创建LineProcessor
      Parameters:
      path - the file path string | 文件路径字符串
      Returns:
      new LineProcessor instance | 新的LineProcessor实例
      Throws:
      NullPointerException - if path is null | 当path为null时抛出
    • filter

      public LineProcessor filter(Predicate<String> predicate)
      Filters lines matching the predicate 过滤匹配谓词的行
      Parameters:
      predicate - the filter predicate | 过滤谓词
      Returns:
      new LineProcessor with filter applied | 应用过滤器的新LineProcessor
      Throws:
      NullPointerException - if predicate is null | 当predicate为null时抛出
    • map

      public LineProcessor map(UnaryOperator<String> mapper)
      Maps each line using the mapper function 使用映射函数映射每一行
      Parameters:
      mapper - the mapping function | 映射函数
      Returns:
      new LineProcessor with map applied | 应用映射的新LineProcessor
      Throws:
      NullPointerException - if mapper is null | 当mapper为null时抛出
    • skip

      public LineProcessor skip(long n)
      Skips the first N lines 跳过前N行
      Parameters:
      n - the number of lines to skip | 要跳过的行数
      Returns:
      new LineProcessor with skip applied | 应用跳过的新LineProcessor
      Throws:
      IllegalArgumentException - if n is negative | 当n为负数时抛出
    • limit

      public LineProcessor limit(long maxLines)
      Limits the number of lines to process 限制要处理的行数
      Parameters:
      maxLines - the maximum number of lines | 最大行数
      Returns:
      new LineProcessor with limit applied | 应用限制的新LineProcessor
      Throws:
      IllegalArgumentException - if maxLines is negative | 当maxLines为负数时抛出
    • trim

      public LineProcessor trim()
      Trims whitespace from each line (shortcut for map(String::trim)) 去除每行的空白字符(map(String::trim)的快捷方式)
      Returns:
      new LineProcessor with trim applied | 应用去空白的新LineProcessor
    • nonEmpty

      public LineProcessor nonEmpty()
      Filters out empty lines (shortcut for filter(s -> !s.isEmpty())) 过滤掉空行(filter(s -> !s.isEmpty())的快捷方式)
      Returns:
      new LineProcessor with non-empty filter applied | 应用非空过滤的新LineProcessor
    • grep

      public LineProcessor grep(String regex)
      Filters lines matching the given regex pattern 过滤匹配给定正则表达式模式的行
      Parameters:
      regex - the regex pattern to match | 要匹配的正则表达式模式
      Returns:
      new LineProcessor with grep filter applied | 应用grep过滤的新LineProcessor
      Throws:
      NullPointerException - if regex is null | 当regex为null时抛出
    • forEach

      public void forEach(Consumer<String> action)
      Applies the action to each processed line 对每个处理后的行应用操作
      Parameters:
      action - the action to apply | 要应用的操作
      Throws:
      NullPointerException - if action is null | 当action为null时抛出
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • collect

      public List<String> collect()
      Collects all processed lines into a list 将所有处理后的行收集到列表中
      Returns:
      list of processed lines | 处理后的行列表
      Throws:
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • count

      public long count()
      Counts the number of processed lines 统计处理后的行数
      Returns:
      the line count | 行数
      Throws:
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • reduce

      public String reduce(String identity, BinaryOperator<String> accumulator)
      Reduces the processed lines using the given identity and accumulator 使用给定的初始值和累加器归约处理后的行
      Parameters:
      identity - the identity value | 初始值
      accumulator - the accumulator function | 累加器函数
      Returns:
      the reduced result | 归约结果
      Throws:
      NullPointerException - if accumulator is null | 当accumulator为null时抛出
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • toFile

      public void toFile(Path output)
      Writes the processed lines to an output file using UTF-8 使用UTF-8将处理后的行写入输出文件
      Parameters:
      output - the output file path | 输出文件路径
      Throws:
      NullPointerException - if output is null | 当output为null时抛出
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • toFile

      public void toFile(Path output, Charset charset)
      Writes the processed lines to an output file with specified charset 使用指定字符集将处理后的行写入输出文件
      Parameters:
      output - the output file path | 输出文件路径
      charset - the charset for writing | 写入使用的字符集
      Throws:
      NullPointerException - if output or charset is null | 当output或charset为null时抛出
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • findFirst

      public Optional<String> findFirst()
      Finds the first processed line 查找第一个处理后的行
      Returns:
      the first line, or empty if none | 第一行,如果没有则为空
      Throws:
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • anyMatch

      public boolean anyMatch(Predicate<String> predicate)
      Tests whether any processed line matches the predicate 测试是否有任何处理后的行匹配谓词
      Parameters:
      predicate - the predicate to test | 要测试的谓词
      Returns:
      true if any line matches | 如果有行匹配则返回true
      Throws:
      NullPointerException - if predicate is null | 当predicate为null时抛出
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出