Skip to content

Commit 6803ac1

Browse files
author
Gary Gregory
committed
Only accumulate files and dirs accepted by the filter.
1 parent e5f8b24 commit 6803ac1

4 files changed

Lines changed: 148 additions & 24 deletions

File tree

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ file comparators, endian transformation classes, and much more.
5252
<connection>scm:git:https://gitbox.apache.org/repos/asf/commons-io.git</connection>
5353
<developerConnection>scm:git:https://gitbox.apache.org/repos/asf/commons-io.git</developerConnection>
5454
<url>https://gitbox.apache.org/repos/asf?p=commons-io.git</url>
55-
<tag>commons-io-2.8.0</tag>
55+
<tag>rel/commons-io-2.9.0</tag>
5656
</scm>
5757

5858
<developers>
@@ -278,9 +278,9 @@ file comparators, endian transformation classes, and much more.
278278
<maven.compiler.target>1.8</maven.compiler.target>
279279
<commons.componentid>io</commons.componentid>
280280
<commons.module.name>org.apache.commons.io</commons.module.name>
281-
<commons.rc.version>RC2</commons.rc.version>
282-
<commons.bc.version>2.7</commons.bc.version>
283-
<commons.release.version>2.8.0</commons.release.version>
281+
<commons.rc.version>RC1</commons.rc.version>
282+
<commons.bc.version>2.8.0</commons.bc.version>
283+
<commons.release.version>2.9.0</commons.release.version>
284284
<commons.release.desc>(requires Java 8)</commons.release.desc>
285285
<commons.jira.id>IO</commons.jira.id>
286286
<commons.jira.pid>12310477</commons.jira.pid>

src/main/java/org/apache/commons/io/file/AccumulatorPathVisitor.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.commons.io.file;
1919

2020
import java.io.IOException;
21-
import java.nio.file.FileVisitResult;
2221
import java.nio.file.Path;
2322
import java.nio.file.attribute.BasicFileAttributes;
2423
import java.util.ArrayList;
@@ -70,34 +69,34 @@ public static AccumulatorPathVisitor withBigIntegerCounters() {
7069
}
7170

7271
/**
73-
* Creates a new instance configured with a long {@link PathCounters}.
72+
* Creates a new instance configured with a BigInteger {@link PathCounters}.
7473
*
74+
* @param pathFilter Filters files to accumulate and count.
7575
* @return a new instance configured with a long {@link PathCounters}.
76+
* @since 2.9.0
7677
*/
77-
public static AccumulatorPathVisitor withLongCounters() {
78-
return new AccumulatorPathVisitor(Counters.longPathCounters());
78+
public static AccumulatorPathVisitor withBigIntegerCounters(final PathFilter pathFilter) {
79+
return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters(), pathFilter);
7980
}
8081

8182
/**
8283
* Creates a new instance configured with a long {@link PathCounters}.
8384
*
84-
* @param pathFilter Filters files to accumulate and count.
8585
* @return a new instance configured with a long {@link PathCounters}.
86-
* @since 2.9.0
8786
*/
88-
public static AccumulatorPathVisitor withLongCounters(final PathFilter pathFilter) {
89-
return new AccumulatorPathVisitor(Counters.longPathCounters(), pathFilter);
87+
public static AccumulatorPathVisitor withLongCounters() {
88+
return new AccumulatorPathVisitor(Counters.longPathCounters());
9089
}
9190

9291
/**
93-
* Creates a new instance configured with a BigInteger {@link PathCounters}.
92+
* Creates a new instance configured with a long {@link PathCounters}.
9493
*
9594
* @param pathFilter Filters files to accumulate and count.
9695
* @return a new instance configured with a long {@link PathCounters}.
9796
* @since 2.9.0
9897
*/
99-
public static AccumulatorPathVisitor withBigIntegerCounters(final PathFilter pathFilter) {
100-
return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters(), pathFilter);
98+
public static AccumulatorPathVisitor withLongCounters(final PathFilter pathFilter) {
99+
return new AccumulatorPathVisitor(Counters.longPathCounters(), pathFilter);
101100
}
102101

103102
private final List<Path> dirList = new ArrayList<>();
@@ -178,12 +177,6 @@ public int hashCode() {
178177
return result;
179178
}
180179

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-
187180
/**
188181
* Relativizes each directory path with {@link Path#relativize(Path)} against the given {@code parent}, optionally
189182
* sorting the result.
@@ -213,9 +206,15 @@ public List<Path> relativizeFiles(final Path parent, final boolean sort,
213206
}
214207

215208
@Override
216-
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
209+
protected void updateDirCounter(final Path dir, final IOException exc) {
210+
super.updateDirCounter(dir, exc);
211+
add(dirList, dir);
212+
}
213+
214+
@Override
215+
protected void updateFileCounters(final Path file, final BasicFileAttributes attributes) {
216+
super.updateFileCounters(file, attributes);
217217
add(fileList, file);
218-
return super.visitFile(file, attributes);
219218
}
220219

221220
}

src/main/java/org/apache/commons/io/file/CountingPathVisitor.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public int hashCode() {
108108

109109
@Override
110110
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
111-
pathCounters.getDirectoryCounter().increment();
111+
updateDirCounter(dir, exc);
112112
return FileVisitResult.CONTINUE;
113113
}
114114

@@ -117,6 +117,17 @@ public String toString() {
117117
return pathCounters.toString();
118118
}
119119

120+
/**
121+
* Updates the counter for visiting the given directory.
122+
*
123+
* @param dir the visited directory.
124+
* @param exc Encountered exception.
125+
* @since 2.9.0
126+
*/
127+
protected void updateDirCounter(Path dir, IOException exc) {
128+
pathCounters.getDirectoryCounter().increment();
129+
}
130+
120131
/**
121132
* Updates the counters for visiting the given file.
122133
*
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.filefilter;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
import java.util.Collections;
30+
31+
import org.apache.commons.io.file.AccumulatorPathVisitor;
32+
import org.apache.commons.io.file.CounterAssertions;
33+
import org.apache.commons.io.file.Counters;
34+
import org.junit.jupiter.api.Test;
35+
36+
/**
37+
* Tests {@link NameFileFilter}.
38+
*/
39+
public class NameFileFilterTest {
40+
41+
/**
42+
* Javadoc example.
43+
*
44+
* System.out calls are commented out here but not in the Javadoc.
45+
*/
46+
@Test
47+
public void testJavadocExampleUsingIo() {
48+
File dir = new File(".");
49+
String[] files = dir.list(new NameFileFilter("NOTICE.txt"));
50+
for (String file : files) {
51+
// System.out.println(file);
52+
}
53+
// End of Javadoc example
54+
assertEquals(1, files.length);
55+
}
56+
57+
/**
58+
* Javadoc example.
59+
*
60+
* System.out calls are commented out here but not in the Javadoc.
61+
*/
62+
@Test
63+
public void testJavadocExampleUsingNio() throws IOException {
64+
final Path dir = Paths.get(".");
65+
// We are interested in files older than one day
66+
final AccumulatorPathVisitor visitor = AccumulatorPathVisitor
67+
.withLongCounters(new NameFileFilter("NOTICE.txt"));
68+
//
69+
// Walk one dir
70+
Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
71+
// System.out.println(visitor.getPathCounters());
72+
// System.out.println(visitor.getFileList());
73+
// System.out.println(visitor.getDirList());
74+
assertEquals(1, visitor.getPathCounters().getFileCounter().get());
75+
assertEquals(1, visitor.getPathCounters().getDirectoryCounter().get());
76+
assertTrue(visitor.getPathCounters().getByteCounter().get() > 0);
77+
assertFalse(visitor.getDirList().isEmpty());
78+
assertFalse(visitor.getFileList().isEmpty());
79+
assertEquals(1, visitor.getFileList().size());
80+
//
81+
visitor.getPathCounters().reset();
82+
//
83+
// Walk dir tree
84+
Files.walkFileTree(dir, visitor);
85+
// System.out.println(visitor.getPathCounters());
86+
// System.out.println(visitor.getDirList());
87+
// System.out.println(visitor.getFileList());
88+
//
89+
// End of Javadoc example
90+
assertTrue(visitor.getPathCounters().getFileCounter().get() > 0);
91+
assertTrue(visitor.getPathCounters().getDirectoryCounter().get() > 0);
92+
assertTrue(visitor.getPathCounters().getByteCounter().get() > 0);
93+
// We counted and accumulated
94+
assertFalse(visitor.getDirList().isEmpty());
95+
assertFalse(visitor.getFileList().isEmpty());
96+
//
97+
assertNotEquals(Counters.noopPathCounters(), visitor.getPathCounters());
98+
visitor.getPathCounters().reset();
99+
CounterAssertions.assertZeroCounters(visitor.getPathCounters());
100+
}
101+
102+
@Test
103+
public void testNoCounting() throws IOException {
104+
final Path dir = Paths.get(".");
105+
final AccumulatorPathVisitor visitor = new AccumulatorPathVisitor(Counters.noopPathCounters(),
106+
new NameFileFilter("NOTICE.txt"));
107+
Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
108+
//
109+
CounterAssertions.assertZeroCounters(visitor.getPathCounters());
110+
// We did not count, but we still accumulated
111+
assertFalse(visitor.getDirList().isEmpty());
112+
assertFalse(visitor.getFileList().isEmpty());
113+
}
114+
}

0 commit comments

Comments
 (0)