Package org.apache.commons.io.file

# Class AccumulatorPathVisitor

java.lang.Object
java.nio.file.SimpleFileVisitor<Path>
org.apache.commons.io.file.SimplePathVisitor
org.apache.commons.io.file.CountingPathVisitor
org.apache.commons.io.file.AccumulatorPathVisitor

All Implemented Interfaces:
  `FileVisitor<Path>`, `PathVisitor`

public class AccumulatorPathVisitor extends CountingPathVisitor
Accumulates normalized paths during visitation.

Use with care on large file trees as each visited Path element is remembered.

## Example

     Path dir = PathUtils.current();
     // We are interested in files older than one day
     Instant cutoff = Instant.now().minus(Duration.ofDays(1));
     AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new AgeFileFilter(cutoff));
     //
     // Walk one directory
     Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
     System.out.println(visitor.getPathCounters());
     System.out.println(visitor.getFileList());
     //
     visitor.getPathCounters().reset();
     //
     // Walk directory tree
     Files.walkFileTree(dir, visitor);
     System.out.println(visitor.getPathCounters());
     System.out.println(visitor.getDirList());
     System.out.println(visitor.getFileList());

Since:
  2.7

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `AccumulatorPathVisitor.Builder`
  Builds instances of `AccumulatorPathVisitor`.

  ## Nested classes/interfaces inherited from class org.apache.commons.io.file.CountingPathVisitor

  `CountingPathVisitor.AbstractBuilder<T,B extends CountingPathVisitor.AbstractBuilder<T,B>>`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AccumulatorPathVisitor()`
  Deprecated.
  Use `builder()`.
  `AccumulatorPathVisitor(Counters.PathCounters pathCounter)`
  Deprecated.
  Use `builder()`.
  `AccumulatorPathVisitor(Counters.PathCounters pathCounter, PathFilter fileFilter, PathFilter dirFilter)`
  Deprecated.
  Use `builder()`.
  `AccumulatorPathVisitor(Counters.PathCounters pathCounter, PathFilter fileFilter, PathFilter dirFilter, IOBiFunction<Path,IOException,FileVisitResult> visitFileFailed)`
  Deprecated.
  Use `builder()`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static AccumulatorPathVisitor.Builder`
  `builder()`
  Builds instances of `AccumulatorPathVisitor`.
  `boolean`
  `equals(Object obj)`
  `List<Path>`
  `getDirList()`
  Gets a copy of the list of visited directories.
  `List<Path>`
  `getFileList()`
  Gets a copy of the list of visited files.
  `int`
  `hashCode()`
  `List<Path>`
  `relativizeDirectories(Path parent, boolean sort, Comparator<? super Path> comparator)`
  Relativizes each directory path with `Path.relativize(Path)` against the given `parent`, optionally sorting the result.
  `List<Path>`
  `relativizeFiles(Path parent, boolean sort, Comparator<? super Path> comparator)`
  Relativizes each file path with `Path.relativize(Path)` against the given `parent`, optionally sorting the result.
  `protected void`
  `updateDirCounter(Path dir, IOException exc)`
  Updates the counter for visiting the given directory.
  `protected void`
  `updateFileCounters(Path file, BasicFileAttributes attributes)`
  Updates the counters for visiting the given file.
  `static AccumulatorPathVisitor`
  `withBigIntegerCounters()`
  Constructs a new instance configured with a BigInteger `Counters.PathCounters`.
  `static AccumulatorPathVisitor`
  `withBigIntegerCounters(PathFilter fileFilter, PathFilter dirFilter)`
  Constructs a new instance configured with a BigInteger `Counters.PathCounters`.
  `static AccumulatorPathVisitor`
  `withLongCounters()`
  Constructs a new instance configured with a long `Counters.PathCounters`.
  `static AccumulatorPathVisitor`
  `withLongCounters(PathFilter fileFilter, PathFilter dirFilter)`
  Constructs a new instance configured with a long `Counters.PathCounters`.

  ### Methods inherited from class org.apache.commons.io.file.CountingPathVisitor

  `accept, getPathCounters, postVisitDirectory, preVisitDirectory, toString, visitFile`

  ### Methods inherited from class org.apache.commons.io.file.SimplePathVisitor

  `visitFileFailed`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### AccumulatorPathVisitor

    @Deprecated public AccumulatorPathVisitor()
    Deprecated.
    Use `builder()`.
    Constructs a new instance with a noop path counter.
    Since:
      2.9.0

  - ### AccumulatorPathVisitor

    @Deprecated public AccumulatorPathVisitor(Counters.PathCounters pathCounter)
    Deprecated.
    Use `builder()`.
    Constructs a new instance that counts file system elements.
    Parameters:
      `pathCounter` - How to count path visits.

  - ### AccumulatorPathVisitor

    @Deprecated public AccumulatorPathVisitor(Counters.PathCounters pathCounter, PathFilter fileFilter, PathFilter dirFilter)
    Deprecated.
    Use `builder()`.
    Constructs a new instance.
    Parameters:
      `pathCounter` - How to count path visits.
      `fileFilter` - Filters which files to count.
      `dirFilter` - Filters which directories to count.
    Since:
      2.9.0

  - ### AccumulatorPathVisitor

    @Deprecated public AccumulatorPathVisitor(Counters.PathCounters pathCounter, PathFilter fileFilter, PathFilter dirFilter, IOBiFunction<Path,IOException,FileVisitResult> visitFileFailed)
    Deprecated.
    Use `builder()`.
    Constructs a new instance.
    Parameters:
      `pathCounter` - How to count path visits.
      `fileFilter` - Filters which files to count.
      `dirFilter` - Filters which directories to count.
      `visitFileFailed` - Called on `SimplePathVisitor.visitFileFailed(Path, IOException)`.
    Since:
      2.12.0

- ## Method Details

  - ### builder

    public static AccumulatorPathVisitor.Builder builder()
    Builds instances of `AccumulatorPathVisitor`.
    Returns:
      a new builder.
    Since:
      2.19.0

  - ### withBigIntegerCounters

    public static AccumulatorPathVisitor withBigIntegerCounters()
    Constructs a new instance configured with a BigInteger `Counters.PathCounters`.
    Returns:
      a new instance configured with a BigInteger `Counters.PathCounters`.
    See Also:
      - `builder()`
      - `AccumulatorPathVisitor.Builder`

  - ### withBigIntegerCounters

    public static AccumulatorPathVisitor withBigIntegerCounters(PathFilter fileFilter, PathFilter dirFilter)
    Constructs a new instance configured with a BigInteger `Counters.PathCounters`.
    Parameters:
      `fileFilter` - Filters files to accumulate and count.
      `dirFilter` - Filters directories to accumulate and count.
    Returns:
      a new instance configured with a long `Counters.PathCounters`.
    Since:
      2.9.0
    See Also:
      - `builder()`
      - `AccumulatorPathVisitor.Builder`

  - ### withLongCounters

    public static AccumulatorPathVisitor withLongCounters()
    Constructs a new instance configured with a long `Counters.PathCounters`.
    Returns:
      a new instance configured with a long `Counters.PathCounters`.
    See Also:
      - `builder()`
      - `AccumulatorPathVisitor.Builder`

  - ### withLongCounters

    public static AccumulatorPathVisitor withLongCounters(PathFilter fileFilter, PathFilter dirFilter)
    Constructs a new instance configured with a long `Counters.PathCounters`.
    Parameters:
      `fileFilter` - Filters files to accumulate and count.
      `dirFilter` - Filters directories to accumulate and count.
    Returns:
      a new instance configured with a long `Counters.PathCounters`.
    Since:
      2.9.0
    See Also:
      - `builder()`
      - `AccumulatorPathVisitor.Builder`

  - ### equals

    public boolean equals(Object obj)
    Overrides:
      `equals` in class `CountingPathVisitor`

  - ### getDirList

    public List<Path> getDirList()
    Gets a copy of the list of visited directories.
    Returns:
      a copy of the list of visited directories.

  - ### getFileList

    public List<Path> getFileList()
    Gets a copy of the list of visited files.
    Returns:
      a copy of the list of visited files.

  - ### hashCode

    public int hashCode()
    Overrides:
      `hashCode` in class `CountingPathVisitor`

  - ### relativizeDirectories

    public List<Path> relativizeDirectories(Path parent, boolean sort, Comparator<? super Path> comparator)
    Relativizes each directory path with `Path.relativize(Path)` against the given `parent`, optionally sorting the result.
    Parameters:
      `parent` - A parent path.
      `sort` - Whether to sort.
      `comparator` - How to sort, null uses default sorting.
    Returns:
      A new list.

  - ### relativizeFiles

    public List<Path> relativizeFiles(Path parent, boolean sort, Comparator<? super Path> comparator)
    Relativizes each file path with `Path.relativize(Path)` against the given `parent`, optionally sorting the result.
    Parameters:
      `parent` - A parent path.
      `sort` - Whether to sort.
      `comparator` - How to sort, null uses default sorting.
    Returns:
      A new list.

  - ### updateDirCounter

    protected void updateDirCounter(Path dir, IOException exc)
    Description copied from class: `CountingPathVisitor`
    Updates the counter for visiting the given directory.
    Overrides:
      `updateDirCounter` in class `CountingPathVisitor`
    Parameters:
      `dir` - the visited directory.
      `exc` - Encountered exception.

  - ### updateFileCounters

    protected void updateFileCounters(Path file, BasicFileAttributes attributes)
    Description copied from class: `CountingPathVisitor`
    Updates the counters for visiting the given file.
    Overrides:
      `updateFileCounters` in class `CountingPathVisitor`
    Parameters:
      `file` - the visited file.
      `attributes` - the visited file attributes.