Class OpenBatch

java.lang.Object
cloud.opencode.base.io.batch.OpenBatch

public final class OpenBatch extends Object
Batch and Parallel File Operations Utility Class 批量和并行文件操作工具类

Provides batch operations for copying, moving, and deleting multiple files with parallel execution support and progress tracking.

提供批量复制、移动和删除多个文件的操作,支持并行执行和进度跟踪。

Features | 主要功能:

  • Batch copy/move/delete operations - 批量复制/移动/删除
  • Parallel execution with configurable parallelism - 可配置并行度的并行执行
  • Progress callbacks - 进度回调
  • Failure handling and result collection - 失败处理和结果收集

Usage Examples | 使用示例:

// Batch copy files
List<Path> files = List.of(path1, path2, path3);
BatchResult result = OpenBatch.copyAll(files, targetDir);

// Parallel delete with progress
BatchResult result = OpenBatch.parallel()
    .parallelism(4)
    .onProgress((path, index, total) -> System.out.println("Processing: " + path))
    .deleteAll(files);

// Batch move with filter
BatchResult result = OpenBatch.moveAll(files, targetDir,
    path -> path.toString().endsWith(".txt"));

Security | 安全性:

  • Thread-safe: Yes, parallel operations use virtual threads - 线程安全: 是,并行操作使用虚拟线程
  • Null-safe: No, paths and arguments must not be null - 空值安全: 否,路径和参数不可为null
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • DEFAULT_PARALLELISM

      public static final int DEFAULT_PARALLELISM
      Default parallelism (number of processors) 默认并行度(处理器数量)
  • Method Details

    • copyAll

      public static BatchResult copyAll(Collection<Path> sources, Path targetDir, CopyOption... options)
      Copy all files to target directory 复制所有文件到目标目录
      Parameters:
      sources - source files | 源文件
      targetDir - target directory | 目标目录
      options - copy options | 复制选项
      Returns:
      batch result | 批量结果
    • copyAll

      public static BatchResult copyAll(Collection<Path> sources, Path targetDir, Predicate<Path> filter, CopyOption... options)
      Copy files matching filter to target directory 复制匹配过滤器的文件到目标目录
      Parameters:
      sources - source files | 源文件
      targetDir - target directory | 目标目录
      filter - file filter | 文件过滤器
      options - copy options | 复制选项
      Returns:
      batch result | 批量结果
    • moveAll

      public static BatchResult moveAll(Collection<Path> sources, Path targetDir, CopyOption... options)
      Move all files to target directory 移动所有文件到目标目录
      Parameters:
      sources - source files | 源文件
      targetDir - target directory | 目标目录
      options - move options | 移动选项
      Returns:
      batch result | 批量结果
    • moveAll

      public static BatchResult moveAll(Collection<Path> sources, Path targetDir, Predicate<Path> filter, CopyOption... options)
      Move files matching filter to target directory 移动匹配过滤器的文件到目标目录
      Parameters:
      sources - source files | 源文件
      targetDir - target directory | 目标目录
      filter - file filter | 文件过滤器
      options - move options | 移动选项
      Returns:
      batch result | 批量结果
    • deleteAll

      public static BatchResult deleteAll(Collection<Path> paths)
      Delete all files 删除所有文件
      Parameters:
      paths - files to delete | 要删除的文件
      Returns:
      batch result | 批量结果
    • deleteAll

      public static BatchResult deleteAll(Collection<Path> paths, Predicate<Path> filter)
      Delete files matching filter 删除匹配过滤器的文件
      Parameters:
      paths - files to delete | 要删除的文件
      filter - file filter | 文件过滤器
      Returns:
      batch result | 批量结果
    • copyGlob

      public static BatchResult copyGlob(Path sourceDir, String pattern, Path targetDir, CopyOption... options)
      Copy files matching glob pattern 复制匹配 glob 模式的文件
      Parameters:
      sourceDir - source directory | 源目录
      pattern - glob pattern | glob 模式
      targetDir - target directory | 目标目录
      options - copy options | 复制选项
      Returns:
      batch result | 批量结果
    • moveGlob

      public static BatchResult moveGlob(Path sourceDir, String pattern, Path targetDir, CopyOption... options)
      Move files matching glob pattern 移动匹配 glob 模式的文件
      Parameters:
      sourceDir - source directory | 源目录
      pattern - glob pattern | glob 模式
      targetDir - target directory | 目标目录
      options - move options | 移动选项
      Returns:
      batch result | 批量结果
    • deleteGlob

      public static BatchResult deleteGlob(Path dir, String pattern)
      Delete files matching glob pattern 删除匹配 glob 模式的文件
      Parameters:
      dir - directory | 目录
      pattern - glob pattern | glob 模式
      Returns:
      batch result | 批量结果
    • parallel

      public static OpenBatch.ParallelBuilder parallel()
      Create a parallel batch operation builder 创建并行批量操作构建器
      Returns:
      builder | 构建器
    • collectFiles

      public static List<Path> collectFiles(Path dir)
      Collect files from directory 从目录收集文件
      Parameters:
      dir - directory | 目录
      Returns:
      list of files | 文件列表
    • collectGlob

      public static List<Path> collectGlob(Path dir, String pattern)
      Collect files matching glob pattern 收集匹配 glob 模式的文件
      Parameters:
      dir - directory | 目录
      pattern - glob pattern | glob 模式
      Returns:
      list of files | 文件列表
    • collectByExtension

      public static List<Path> collectByExtension(Path dir, String... extensions)
      Collect files by extension 按扩展名收集文件
      Parameters:
      dir - directory | 目录
      extensions - extensions (without dot) | 扩展名(不含点)
      Returns:
      list of files | 文件列表