1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.io.file;
19
20 import java.io.IOException;
21 import java.nio.file.FileVisitResult;
22 import java.nio.file.Path;
23 import java.nio.file.attribute.BasicFileAttributes;
24 import java.util.ArrayList;
25 import java.util.Comparator;
26 import java.util.List;
27 import java.util.Objects;
28
29 import org.apache.commons.io.file.Counters.PathCounters;
30 import org.apache.commons.io.function.IOBiFunction;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public class AccumulatorPathVisitor extends CountingPathVisitor {
62
63
64
65
66
67
68 public static class Builder extends AbstractBuilder<AccumulatorPathVisitor, Builder> {
69
70
71
72
73 public Builder() {
74
75 }
76
77 @Override
78 public AccumulatorPathVisitor get() {
79 return new AccumulatorPathVisitor(this);
80 }
81
82 }
83
84
85
86
87
88
89
90 public static Builder builder() {
91 return new Builder();
92 }
93
94
95
96
97
98
99
100
101 public static AccumulatorPathVisitor withBigIntegerCounters() {
102 return builder().setPathCounters(Counters.bigIntegerPathCounters()).get();
103 }
104
105
106
107
108
109
110
111
112
113
114
115 public static AccumulatorPathVisitor withBigIntegerCounters(final PathFilter fileFilter, final PathFilter dirFilter) {
116 return builder().setPathCounters(Counters.bigIntegerPathCounters()).setFileFilter(fileFilter).setDirectoryFilter(dirFilter).get();
117 }
118
119
120
121
122
123
124
125
126 public static AccumulatorPathVisitor withLongCounters() {
127 return builder().setPathCounters(Counters.longPathCounters()).get();
128 }
129
130
131
132
133
134
135
136
137
138
139
140 public static AccumulatorPathVisitor withLongCounters(final PathFilter fileFilter, final PathFilter dirFilter) {
141 return builder().setPathCounters(Counters.longPathCounters()).setFileFilter(fileFilter).setDirectoryFilter(dirFilter).get();
142 }
143
144 private final List<Path> dirList = new ArrayList<>();
145
146 private final List<Path> fileList = new ArrayList<>();
147
148
149
150
151
152
153
154 @Deprecated
155 public AccumulatorPathVisitor() {
156 super(Counters.noopPathCounters());
157 }
158
159 private AccumulatorPathVisitor(final Builder builder) {
160 super(builder);
161 }
162
163
164
165
166
167
168
169 @Deprecated
170 public AccumulatorPathVisitor(final PathCounters pathCounter) {
171 super(pathCounter);
172 }
173
174
175
176
177
178
179
180
181
182
183 @Deprecated
184 public AccumulatorPathVisitor(final PathCounters pathCounter, final PathFilter fileFilter, final PathFilter dirFilter) {
185 super(pathCounter, fileFilter, dirFilter);
186 }
187
188
189
190
191
192
193
194
195
196
197
198 @Deprecated
199 public AccumulatorPathVisitor(final PathCounters pathCounter, final PathFilter fileFilter, final PathFilter dirFilter,
200 final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailed) {
201 super(pathCounter, fileFilter, dirFilter, visitFileFailed);
202 }
203
204 private void add(final List<Path> list, final Path dir) {
205 list.add(dir.normalize());
206 }
207
208 @Override
209 public boolean equals(final Object obj) {
210 if (this == obj) {
211 return true;
212 }
213 if (!super.equals(obj)) {
214 return false;
215 }
216 if (!(obj instanceof AccumulatorPathVisitor)) {
217 return false;
218 }
219 final AccumulatorPathVisitor other = (AccumulatorPathVisitor) obj;
220 return Objects.equals(dirList, other.dirList) && Objects.equals(fileList, other.fileList);
221 }
222
223
224
225
226
227
228 public List<Path> getDirList() {
229 return new ArrayList<>(dirList);
230 }
231
232
233
234
235
236
237 public List<Path> getFileList() {
238 return new ArrayList<>(fileList);
239 }
240
241 @Override
242 public int hashCode() {
243 final int prime = 31;
244 int result = super.hashCode();
245 result = prime * result + Objects.hash(dirList, fileList);
246 return result;
247 }
248
249
250
251
252
253
254
255
256
257
258 public List<Path> relativizeDirectories(final Path parent, final boolean sort,
259 final Comparator<? super Path> comparator) {
260 return PathUtils.relativize(getDirList(), parent, sort, comparator);
261 }
262
263
264
265
266
267
268
269
270
271
272 public List<Path> relativizeFiles(final Path parent, final boolean sort,
273 final Comparator<? super Path> comparator) {
274 return PathUtils.relativize(getFileList(), parent, sort, comparator);
275 }
276
277 @Override
278 protected void updateDirCounter(final Path dir, final IOException exc) {
279 super.updateDirCounter(dir, exc);
280 add(dirList, dir);
281 }
282
283 @Override
284 protected void updateFileCounters(final Path file, final BasicFileAttributes attributes) {
285 super.updateFileCounters(file, attributes);
286 add(fileList, file);
287 }
288
289 }