Skip to content

Commit f4b721c

Browse files
committed
[IO-872] PathUtils.directoryAndFileContentEquals doesn't work across
FileSystems - Add SimplePathVisitor.AbstractBuilder - Add CountingPathVisitor.AbstractBuilder and CountingPathVisitor.Builder - Add AccumulatorPathVisitor.Builder and builder( - For now, keep as much of the new code private or package-private as possible
1 parent e5c4d14 commit f4b721c

File tree

8 files changed

+310
-46
lines changed

8 files changed

+310
-46
lines changed

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ The <action> type attribute can be add,update,fix,remove.
9090
<action dev="ggregory" type="add" due-to="Gary Gregory">Add Uncheck.getAsBoolean(IOBooleanSupplier).</action>
9191
<action dev="ggregory" type="add" due-to="Gary Gregory">Add FileChannels.contentEquals(SeekableByteChannel, SeekableByteChannel, int).</action>
9292
<action dev="ggregory" type="add" due-to="Gary Gregory">Add FileChannels.contentEquals(ReadableByteChannel, ReadableByteChannel, int).</action>
93+
<action dev="ggregory" type="add" issue="IO-872" due-to="Gary Gregory">Add SimplePathVisitor.AbstractBuilder.</action>
94+
<action dev="ggregory" type="add" issue="IO-872" due-to="Gary Gregory">Add CountingPathVisitor.AbstractBuilder and CountingPathVisitor.Builder.</action>
95+
<action dev="ggregory" type="add" issue="IO-872" due-to="Gary Gregory">Add AccumulatorPathVisitor.Builder and builder().</action>
9396
<!-- UPDATE -->
9497
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump commons.bytebuddy.version from 1.15.10 to 1.17.5 #710, #715, #720, #734, #735.</action>
9598
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump commons-codec:commons-codec from 1.17.1 to 1.18.0. #717.</action>

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,8 +2282,14 @@ private static AccumulatorPathVisitor listAccumulate(final File directory, final
22822282
final boolean isDirFilterSet = dirFilter != null;
22832283
final FileEqualsFileFilter rootDirFilter = new FileEqualsFileFilter(directory);
22842284
final PathFilter dirPathFilter = isDirFilterSet ? rootDirFilter.or(dirFilter) : rootDirFilter;
2285-
final AccumulatorPathVisitor visitor = new AccumulatorPathVisitor(Counters.noopPathCounters(), fileFilter, dirPathFilter,
2286-
(p, e) -> FileVisitResult.CONTINUE);
2285+
// @formatter:off
2286+
final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.builder()
2287+
.setPathCounters(Counters.noopPathCounters())
2288+
.setFileFilter(fileFilter)
2289+
.setDirectoryFilter(dirPathFilter)
2290+
.setVisitFileFailedFunction((p, e) -> FileVisitResult.CONTINUE)
2291+
.get();
2292+
// @formatter:on
22872293
final Set<FileVisitOption> optionSet = new HashSet<>();
22882294
if (options != null) {
22892295
Collections.addAll(optionSet, options);

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

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,38 @@
6060
*/
6161
public class AccumulatorPathVisitor extends CountingPathVisitor {
6262

63+
/**
64+
* Builds instances of {@link AccumulatorPathVisitor}.
65+
*
66+
* @since 2.18.0
67+
*/
68+
public static class Builder extends AbstractBuilder<AccumulatorPathVisitor, Builder> {
69+
@Override
70+
public AccumulatorPathVisitor get() {
71+
return new AccumulatorPathVisitor(this);
72+
}
73+
74+
}
75+
76+
/**
77+
* Builds instances of {@link AccumulatorPathVisitor}.
78+
*
79+
* @return a new builder.
80+
* @since 2.18.0
81+
*/
82+
public static Builder builder() {
83+
return new Builder();
84+
}
85+
6386
/**
6487
* Constructs a new instance configured with a BigInteger {@link PathCounters}.
6588
*
6689
* @return a new instance configured with a BigInteger {@link PathCounters}.
90+
* @see #builder()
91+
* @see Builder
6792
*/
6893
public static AccumulatorPathVisitor withBigIntegerCounters() {
69-
return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters());
94+
return builder().setPathCounters(Counters.bigIntegerPathCounters()).get();
7095
}
7196

7297
/**
@@ -75,20 +100,23 @@ public static AccumulatorPathVisitor withBigIntegerCounters() {
75100
* @param fileFilter Filters files to accumulate and count.
76101
* @param dirFilter Filters directories to accumulate and count.
77102
* @return a new instance configured with a long {@link PathCounters}.
103+
* @see #builder()
104+
* @see Builder
78105
* @since 2.9.0
79106
*/
80-
public static AccumulatorPathVisitor withBigIntegerCounters(final PathFilter fileFilter,
81-
final PathFilter dirFilter) {
82-
return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters(), fileFilter, dirFilter);
107+
public static AccumulatorPathVisitor withBigIntegerCounters(final PathFilter fileFilter, final PathFilter dirFilter) {
108+
return builder().setPathCounters(Counters.bigIntegerPathCounters()).setFileFilter(fileFilter).setDirectoryFilter(dirFilter).get();
83109
}
84110

85111
/**
86112
* Constructs a new instance configured with a long {@link PathCounters}.
87113
*
88114
* @return a new instance configured with a long {@link PathCounters}.
115+
* @see #builder()
116+
* @see Builder
89117
*/
90118
public static AccumulatorPathVisitor withLongCounters() {
91-
return new AccumulatorPathVisitor(Counters.longPathCounters());
119+
return builder().setPathCounters(Counters.longPathCounters()).get();
92120
}
93121

94122
/**
@@ -97,30 +125,40 @@ public static AccumulatorPathVisitor withLongCounters() {
97125
* @param fileFilter Filters files to accumulate and count.
98126
* @param dirFilter Filters directories to accumulate and count.
99127
* @return a new instance configured with a long {@link PathCounters}.
128+
* @see #builder()
129+
* @see Builder
100130
* @since 2.9.0
101131
*/
102132
public static AccumulatorPathVisitor withLongCounters(final PathFilter fileFilter, final PathFilter dirFilter) {
103-
return new AccumulatorPathVisitor(Counters.longPathCounters(), fileFilter, dirFilter);
133+
return builder().setPathCounters(Counters.longPathCounters()).setFileFilter(fileFilter).setDirectoryFilter(dirFilter).get();
104134
}
105135

106136
private final List<Path> dirList = new ArrayList<>();
107137

108138
private final List<Path> fileList = new ArrayList<>();
109139

110140
/**
111-
* Constructs a new instance.
141+
* Constructs a new instance with a noop path counter.
112142
*
113143
* @since 2.9.0
144+
* @deprecated Use {@link #builder()}.
114145
*/
146+
@Deprecated
115147
public AccumulatorPathVisitor() {
116148
super(Counters.noopPathCounters());
117149
}
118150

151+
private AccumulatorPathVisitor(final Builder builder) {
152+
super(builder);
153+
}
154+
119155
/**
120156
* Constructs a new instance that counts file system elements.
121157
*
122158
* @param pathCounter How to count path visits.
159+
* @deprecated Use {@link #builder()}.
123160
*/
161+
@Deprecated
124162
public AccumulatorPathVisitor(final PathCounters pathCounter) {
125163
super(pathCounter);
126164
}
@@ -132,7 +170,9 @@ public AccumulatorPathVisitor(final PathCounters pathCounter) {
132170
* @param fileFilter Filters which files to count.
133171
* @param dirFilter Filters which directories to count.
134172
* @since 2.9.0
173+
* @deprecated Use {@link #builder()}.
135174
*/
175+
@Deprecated
136176
public AccumulatorPathVisitor(final PathCounters pathCounter, final PathFilter fileFilter, final PathFilter dirFilter) {
137177
super(pathCounter, fileFilter, dirFilter);
138178
}
@@ -145,7 +185,9 @@ public AccumulatorPathVisitor(final PathCounters pathCounter, final PathFilter f
145185
* @param dirFilter Filters which directories to count.
146186
* @param visitFileFailed Called on {@link #visitFileFailed(Path, IOException)}.
147187
* @since 2.12.0
188+
* @deprecated Use {@link #builder()}.
148189
*/
190+
@Deprecated
149191
public AccumulatorPathVisitor(final PathCounters pathCounter, final PathFilter fileFilter, final PathFilter dirFilter,
150192
final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailed) {
151193
super(pathCounter, fileFilter, dirFilter, visitFileFailed);

0 commit comments

Comments
 (0)