1919
2020import java .io .IOException ;
2121import java .nio .file .FileVisitResult ;
22- import java .nio .file .Files ;
2322import java .nio .file .Path ;
2423import java .nio .file .attribute .BasicFileAttributes ;
2524import java .util .ArrayList ;
2827import java .util .Objects ;
2928
3029import org .apache .commons .io .file .Counters .PathCounters ;
30+ import org .apache .commons .io .filefilter .PathFilter ;
3131
3232/**
3333 * Accumulates normalized paths during visitation.
3434 * <p>
3535 * Use with care on large file trees as each visited Path element is remembered.
3636 * </p>
37+ * <h2>Example</h2>
38+ *
39+ * <pre>
40+ * Path dir = Paths.get(".");
41+ * // We are interested in files older than one day
42+ * long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
43+ * AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new AgeFileFilter(cutoff));
44+ * //
45+ * // Walk one dir
46+ * Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
47+ * System.out.println(visitor.getPathCounters());
48+ * System.out.println(visitor.getFileList());
49+ * //
50+ * visitor.getPathCounters().reset();
51+ * //
52+ * // Walk dir tree
53+ * Files.walkFileTree(dir, visitor);
54+ * System.out.println(visitor.getPathCounters());
55+ * System.out.println(visitor.getDirList());
56+ * System.out.println(visitor.getFileList());
57+ * </pre>
3758 *
3859 * @since 2.7
3960 */
@@ -57,19 +78,65 @@ public static AccumulatorPathVisitor withLongCounters() {
5778 return new AccumulatorPathVisitor (Counters .longPathCounters ());
5879 }
5980
81+ /**
82+ * Creates a new instance configured with a long {@link PathCounters}.
83+ *
84+ * @param pathFilter Filters files to accumulate and count.
85+ * @return a new instance configured with a long {@link PathCounters}.
86+ * @since 2.9.0
87+ */
88+ public static AccumulatorPathVisitor withLongCounters (final PathFilter pathFilter ) {
89+ return new AccumulatorPathVisitor (Counters .longPathCounters (), pathFilter );
90+ }
91+
92+ /**
93+ * Creates a new instance configured with a BigInteger {@link PathCounters}.
94+ *
95+ * @param pathFilter Filters files to accumulate and count.
96+ * @return a new instance configured with a long {@link PathCounters}.
97+ * @since 2.9.0
98+ */
99+ public static AccumulatorPathVisitor withBigIntegerCounters (final PathFilter pathFilter ) {
100+ return new AccumulatorPathVisitor (Counters .bigIntegerPathCounters (), pathFilter );
101+ }
102+
60103 private final List <Path > dirList = new ArrayList <>();
61104
62105 private final List <Path > fileList = new ArrayList <>();
63106
64107 /**
65108 * Constructs a new instance.
66109 *
110+ * @since 2.9.0
111+ */
112+ public AccumulatorPathVisitor () {
113+ super (Counters .noopPathCounters ());
114+ }
115+
116+ /**
117+ * Constructs a new instance that counts file system elements.
118+ *
67119 * @param pathCounter How to count path visits.
68120 */
69121 public AccumulatorPathVisitor (final PathCounters pathCounter ) {
70122 super (pathCounter );
71123 }
72124
125+ /**
126+ * Constructs a new instance.
127+ *
128+ * @param pathCounter How to count path visits.
129+ * @param pathFilter Filters which paths to count.
130+ * @since 2.9.0
131+ */
132+ public AccumulatorPathVisitor (final PathCounters pathCounter , final PathFilter pathFilter ) {
133+ super (pathCounter , pathFilter );
134+ }
135+
136+ private void add (final List <Path > list , final Path dir ) {
137+ list .add (dir .normalize ());
138+ }
139+
73140 @ Override
74141 public boolean equals (final Object obj ) {
75142 if (this == obj ) {
@@ -111,6 +178,12 @@ public int hashCode() {
111178 return result ;
112179 }
113180
181+ @ Override
182+ public FileVisitResult postVisitDirectory (final Path dir , final IOException exc ) throws IOException {
183+ add (dirList , dir );
184+ return super .postVisitDirectory (dir , exc );
185+ }
186+
114187 /**
115188 * Relativizes each directory path with {@link Path#relativize(Path)} against the given {@code parent}, optionally
116189 * sorting the result.
@@ -120,7 +193,8 @@ public int hashCode() {
120193 * @param comparator How to sort, null uses default sorting.
121194 * @return A new list
122195 */
123- public List <Path > relativizeDirectories (final Path parent , final boolean sort , final Comparator <? super Path > comparator ) {
196+ public List <Path > relativizeDirectories (final Path parent , final boolean sort ,
197+ final Comparator <? super Path > comparator ) {
124198 return PathUtils .relativize (getDirList (), parent , sort , comparator );
125199 }
126200
@@ -133,13 +207,14 @@ public List<Path> relativizeDirectories(final Path parent, final boolean sort, f
133207 * @param comparator How to sort, null uses default sorting.
134208 * @return A new list
135209 */
136- public List <Path > relativizeFiles (final Path parent , final boolean sort , final Comparator <? super Path > comparator ) {
210+ public List <Path > relativizeFiles (final Path parent , final boolean sort ,
211+ final Comparator <? super Path > comparator ) {
137212 return PathUtils .relativize (getFileList (), parent , sort , comparator );
138213 }
139214
140215 @ Override
141216 public FileVisitResult visitFile (final Path file , final BasicFileAttributes attributes ) throws IOException {
142- (( Files . isDirectory ( file )) ? dirList : fileList ). add (file . normalize () );
217+ add (fileList , file );
143218 return super .visitFile (file , attributes );
144219 }
145220
0 commit comments