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 AccumulatorPathVisitor withBigIntegerCounters() {
69 return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters());
70 }
71
72
73
74
75
76
77
78
79
80 public static AccumulatorPathVisitor withBigIntegerCounters(final PathFilter fileFilter,
81 final PathFilter dirFilter) {
82 return new AccumulatorPathVisitor(Counters.bigIntegerPathCounters(), fileFilter, dirFilter);
83 }
84
85
86
87
88
89
90 public static AccumulatorPathVisitor withLongCounters() {
91 return new AccumulatorPathVisitor(Counters.longPathCounters());
92 }
93
94
95
96
97
98
99
100
101
102 public static AccumulatorPathVisitor withLongCounters(final PathFilter fileFilter, final PathFilter dirFilter) {
103 return new AccumulatorPathVisitor(Counters.longPathCounters(), fileFilter, dirFilter);
104 }
105
106 private final List<Path> dirList = new ArrayList<>();
107
108 private final List<Path> fileList = new ArrayList<>();
109
110
111
112
113
114
115 public AccumulatorPathVisitor() {
116 super(Counters.noopPathCounters());
117 }
118
119
120
121
122
123
124 public AccumulatorPathVisitor(final PathCounters pathCounter) {
125 super(pathCounter);
126 }
127
128
129
130
131
132
133
134
135
136 public AccumulatorPathVisitor(final PathCounters pathCounter, final PathFilter fileFilter, final PathFilter dirFilter) {
137 super(pathCounter, fileFilter, dirFilter);
138 }
139
140
141
142
143
144
145
146
147
148
149 public AccumulatorPathVisitor(final PathCounters pathCounter, final PathFilter fileFilter, final PathFilter dirFilter,
150 final IOBiFunction<Path, IOException, FileVisitResult> visitFileFailed) {
151 super(pathCounter, fileFilter, dirFilter, visitFileFailed);
152 }
153
154 private void add(final List<Path> list, final Path dir) {
155 list.add(dir.normalize());
156 }
157
158 @Override
159 public boolean equals(final Object obj) {
160 if (this == obj) {
161 return true;
162 }
163 if (!super.equals(obj)) {
164 return false;
165 }
166 if (!(obj instanceof AccumulatorPathVisitor)) {
167 return false;
168 }
169 final AccumulatorPathVisitor other = (AccumulatorPathVisitor) obj;
170 return Objects.equals(dirList, other.dirList) && Objects.equals(fileList, other.fileList);
171 }
172
173
174
175
176
177
178 public List<Path> getDirList() {
179 return new ArrayList<>(dirList);
180 }
181
182
183
184
185
186
187 public List<Path> getFileList() {
188 return new ArrayList<>(fileList);
189 }
190
191 @Override
192 public int hashCode() {
193 final int prime = 31;
194 int result = super.hashCode();
195 result = prime * result + Objects.hash(dirList, fileList);
196 return result;
197 }
198
199
200
201
202
203
204
205
206
207
208 public List<Path> relativizeDirectories(final Path parent, final boolean sort,
209 final Comparator<? super Path> comparator) {
210 return PathUtils.relativize(getDirList(), parent, sort, comparator);
211 }
212
213
214
215
216
217
218
219
220
221
222 public List<Path> relativizeFiles(final Path parent, final boolean sort,
223 final Comparator<? super Path> comparator) {
224 return PathUtils.relativize(getFileList(), parent, sort, comparator);
225 }
226
227 @Override
228 protected void updateDirCounter(final Path dir, final IOException exc) {
229 super.updateDirCounter(dir, exc);
230 add(dirList, dir);
231 }
232
233 @Override
234 protected void updateFileCounters(final Path file, final BasicFileAttributes attributes) {
235 super.updateFileCounters(file, attributes);
236 add(fileList, file);
237 }
238
239 }