Class 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 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

      • 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.
      • 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 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 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 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​(java.nio.file.Path dir,
                                        java.io.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​(java.nio.file.Path file,
                                          java.nio.file.attribute.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.