Package org.apache.commons.io.file
Class AccumulatorPathVisitor
- java.lang.Object
-
- java.nio.file.SimpleFileVisitor<java.nio.file.Path>
-
- org.apache.commons.io.file.SimplePathVisitor
-
- org.apache.commons.io.file.CountingPathVisitor
-
- org.apache.commons.io.file.AccumulatorPathVisitor
-
- All Implemented Interfaces:
java.nio.file.FileVisitor<java.nio.file.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 = Paths.get(""); // We are interested in files older than one day long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000); AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new AgeFileFilter(cutoff)); // // Walk one dir Files.walkFileTree(dir, Collections.emptySet(), 1, visitor); System.out.println(visitor.getPathCounters()); System.out.println(visitor.getFileList()); // visitor.getPathCounters().reset(); // // Walk dir tree Files.walkFileTree(dir, visitor); System.out.println(visitor.getPathCounters()); System.out.println(visitor.getDirList()); System.out.println(visitor.getFileList());
- Since:
- 2.7
-
-
Constructor Summary
Constructors Constructor Description AccumulatorPathVisitor()
Constructs a new instance.AccumulatorPathVisitor(Counters.PathCounters pathCounter)
Constructs a new instance that counts file system elements.AccumulatorPathVisitor(Counters.PathCounters pathCounter, PathFilter fileFilter, PathFilter dirFilter)
Constructs a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(java.lang.Object obj)
java.util.List<java.nio.file.Path>
getDirList()
Gets the list of visited directories.java.util.List<java.nio.file.Path>
getFileList()
Gets the list of visited files.int
hashCode()
java.util.List<java.nio.file.Path>
relativizeDirectories(java.nio.file.Path parent, boolean sort, java.util.Comparator<? super java.nio.file.Path> comparator)
Relativizes each directory path withPath.relativize(Path)
against the givenparent
, optionally sorting the result.java.util.List<java.nio.file.Path>
relativizeFiles(java.nio.file.Path parent, boolean sort, java.util.Comparator<? super java.nio.file.Path> comparator)
Relativizes each file path withPath.relativize(Path)
against the givenparent
, optionally sorting the result.static AccumulatorPathVisitor
withBigIntegerCounters()
Creates a new instance configured with a BigIntegerCounters.PathCounters
.static AccumulatorPathVisitor
withBigIntegerCounters(PathFilter fileFilter, PathFilter dirFilter)
Creates a new instance configured with a BigIntegerCounters.PathCounters
.static AccumulatorPathVisitor
withLongCounters()
Creates a new instance configured with a longCounters.PathCounters
.static AccumulatorPathVisitor
withLongCounters(PathFilter fileFilter, PathFilter dirFilter)
Creates a new instance configured with a longCounters.PathCounters
.-
Methods inherited from class org.apache.commons.io.file.CountingPathVisitor
getPathCounters, postVisitDirectory, preVisitDirectory, toString, visitFile
-
-
-
-
Constructor Detail
-
AccumulatorPathVisitor
public AccumulatorPathVisitor()
Constructs a new instance.- Since:
- 2.9.0
-
AccumulatorPathVisitor
public AccumulatorPathVisitor(Counters.PathCounters pathCounter)
Constructs a new instance that counts file system elements.- Parameters:
pathCounter
- How to count path visits.
-
AccumulatorPathVisitor
public AccumulatorPathVisitor(Counters.PathCounters pathCounter, PathFilter fileFilter, PathFilter dirFilter)
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
-
-
Method Detail
-
withBigIntegerCounters
public static AccumulatorPathVisitor withBigIntegerCounters()
Creates a new instance configured with a BigIntegerCounters.PathCounters
.- Returns:
- a new instance configured with a BigInteger
Counters.PathCounters
.
-
withBigIntegerCounters
public static AccumulatorPathVisitor withBigIntegerCounters(PathFilter fileFilter, PathFilter dirFilter)
Creates a new instance configured with a BigIntegerCounters.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
-
withLongCounters
public static AccumulatorPathVisitor withLongCounters()
Creates a new instance configured with a longCounters.PathCounters
.- Returns:
- a new instance configured with a long
Counters.PathCounters
.
-
withLongCounters
public static AccumulatorPathVisitor withLongCounters(PathFilter fileFilter, PathFilter dirFilter)
Creates a new instance configured with a longCounters.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
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classCountingPathVisitor
-
getDirList
public java.util.List<java.nio.file.Path> getDirList()
Gets the list of visited directories.- Returns:
- the list of visited directories.
-
getFileList
public java.util.List<java.nio.file.Path> getFileList()
Gets the list of visited files.- Returns:
- the list of visited files.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classCountingPathVisitor
-
relativizeDirectories
public java.util.List<java.nio.file.Path> relativizeDirectories(java.nio.file.Path parent, boolean sort, java.util.Comparator<? super java.nio.file.Path> comparator)
Relativizes each directory path withPath.relativize(Path)
against the givenparent
, optionally sorting the result.- Parameters:
parent
- A parent pathsort
- Whether to sortcomparator
- How to sort, null uses default sorting.- Returns:
- A new list
-
relativizeFiles
public java.util.List<java.nio.file.Path> relativizeFiles(java.nio.file.Path parent, boolean sort, java.util.Comparator<? super java.nio.file.Path> comparator)
Relativizes each file path withPath.relativize(Path)
against the givenparent
, optionally sorting the result.- Parameters:
parent
- A parent pathsort
- Whether to sortcomparator
- How to sort, null uses default sorting.- Returns:
- A new list
-
-