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.math.BigInteger;
21 import java.util.Objects;
22
23
24
25
26
27
28 public class Counters {
29
30
31
32
33 private static class AbstractPathCounters implements PathCounters {
34
35 private final Counter byteCounter;
36 private final Counter directoryCounter;
37 private final Counter fileCounter;
38
39
40
41
42
43
44
45
46 protected AbstractPathCounters(final Counter byteCounter, final Counter directoryCounter, final Counter fileCounter) {
47 this.byteCounter = byteCounter;
48 this.directoryCounter = directoryCounter;
49 this.fileCounter = fileCounter;
50 }
51
52 @Override
53 public boolean equals(final Object obj) {
54 if (this == obj) {
55 return true;
56 }
57 if (!(obj instanceof AbstractPathCounters)) {
58 return false;
59 }
60 final AbstractPathCounters other = (AbstractPathCounters) obj;
61 return Objects.equals(byteCounter, other.byteCounter)
62 && Objects.equals(directoryCounter, other.directoryCounter)
63 && Objects.equals(fileCounter, other.fileCounter);
64 }
65
66 @Override
67 public Counter getByteCounter() {
68 return byteCounter;
69 }
70
71 @Override
72 public Counter getDirectoryCounter() {
73 return directoryCounter;
74 }
75
76
77
78
79
80
81 @Override
82 public Counter getFileCounter() {
83 return this.fileCounter;
84 }
85
86 @Override
87 public int hashCode() {
88 return Objects.hash(byteCounter, directoryCounter, fileCounter);
89 }
90
91 @Override
92 public void reset() {
93 byteCounter.reset();
94 directoryCounter.reset();
95 fileCounter.reset();
96 }
97
98 @Override
99 public String toString() {
100 return String.format("%,d files, %,d directories, %,d bytes", Long.valueOf(fileCounter.get()),
101 Long.valueOf(directoryCounter.get()), Long.valueOf(byteCounter.get()));
102 }
103
104 }
105
106
107
108
109 private static final class BigIntegerCounter implements Counter {
110
111 private BigInteger value = BigInteger.ZERO;
112
113 @Override
114 public void add(final long val) {
115 value = value.add(BigInteger.valueOf(val));
116
117 }
118
119 @Override
120 public boolean equals(final Object obj) {
121 if (this == obj) {
122 return true;
123 }
124 if (!(obj instanceof Counter)) {
125 return false;
126 }
127 final Counter other = (Counter) obj;
128 return Objects.equals(value, other.getBigInteger());
129 }
130
131 @Override
132 public long get() {
133 return value.longValueExact();
134 }
135
136 @Override
137 public BigInteger getBigInteger() {
138 return value;
139 }
140
141 @Override
142 public Long getLong() {
143 return Long.valueOf(value.longValueExact());
144 }
145
146 @Override
147 public int hashCode() {
148 return Objects.hash(value);
149 }
150
151 @Override
152 public void increment() {
153 value = value.add(BigInteger.ONE);
154 }
155
156 @Override
157 public void reset() {
158 value = BigInteger.ZERO;
159 }
160
161 @Override
162 public String toString() {
163 return value.toString();
164 }
165 }
166
167
168
169
170 private static final class BigIntegerPathCounters extends AbstractPathCounters {
171
172
173
174
175 protected BigIntegerPathCounters() {
176 super(bigIntegerCounter(), bigIntegerCounter(), bigIntegerCounter());
177 }
178
179 }
180
181
182
183
184 public interface Counter {
185
186
187
188
189
190
191 void add(long val);
192
193
194
195
196
197
198 long get();
199
200
201
202
203
204
205 BigInteger getBigInteger();
206
207
208
209
210
211
212 Long getLong();
213
214
215
216
217 void increment();
218
219
220
221
222 default void reset() {
223
224 }
225
226 }
227
228
229
230
231 private static final class LongCounter implements Counter {
232
233 private long value;
234
235 @Override
236 public void add(final long add) {
237 value += add;
238
239 }
240
241 @Override
242 public boolean equals(final Object obj) {
243 if (this == obj) {
244 return true;
245 }
246 if (!(obj instanceof Counter)) {
247 return false;
248 }
249 final Counter other = (Counter) obj;
250 return value == other.get();
251 }
252
253 @Override
254 public long get() {
255 return value;
256 }
257
258 @Override
259 public BigInteger getBigInteger() {
260 return BigInteger.valueOf(value);
261 }
262
263 @Override
264 public Long getLong() {
265 return Long.valueOf(value);
266 }
267
268 @Override
269 public int hashCode() {
270 return Objects.hash(value);
271 }
272
273 @Override
274 public void increment() {
275 value++;
276 }
277
278 @Override
279 public void reset() {
280 value = 0L;
281 }
282
283 @Override
284 public String toString() {
285 return Long.toString(value);
286 }
287 }
288
289
290
291
292 private static final class LongPathCounters extends AbstractPathCounters {
293
294
295
296
297 protected LongPathCounters() {
298 super(longCounter(), longCounter(), longCounter());
299 }
300
301 }
302
303
304
305
306 private static final class NoopCounter implements Counter {
307
308 static final NoopCounter INSTANCE = new NoopCounter();
309
310 @Override
311 public void add(final long add) {
312
313 }
314
315 @Override
316 public long get() {
317 return 0;
318 }
319
320 @Override
321 public BigInteger getBigInteger() {
322 return BigInteger.ZERO;
323 }
324
325 @Override
326 public Long getLong() {
327 return 0L;
328 }
329
330 @Override
331 public void increment() {
332
333 }
334
335
336
337
338
339
340
341 @Override
342 public String toString() {
343 return "0";
344 }
345
346 }
347
348
349
350
351 private static final class NoopPathCounters extends AbstractPathCounters {
352
353 static final NoopPathCounters INSTANCE = new NoopPathCounters();
354
355
356
357
358 private NoopPathCounters() {
359 super(noopCounter(), noopCounter(), noopCounter());
360 }
361
362 }
363
364
365
366
367 public interface PathCounters {
368
369
370
371
372
373
374 Counter getByteCounter();
375
376
377
378
379
380
381 Counter getDirectoryCounter();
382
383
384
385
386
387
388 Counter getFileCounter();
389
390
391
392
393 default void reset() {
394
395 }
396
397 }
398
399
400
401
402
403
404 public static Counter bigIntegerCounter() {
405 return new BigIntegerCounter();
406 }
407
408
409
410
411
412
413 public static PathCounters bigIntegerPathCounters() {
414 return new BigIntegerPathCounters();
415 }
416
417
418
419
420
421
422 public static Counter longCounter() {
423 return new LongCounter();
424 }
425
426
427
428
429
430
431 public static PathCounters longPathCounters() {
432 return new LongPathCounters();
433 }
434
435
436
437
438
439
440
441 public static Counter noopCounter() {
442 return NoopCounter.INSTANCE;
443 }
444
445
446
447
448
449
450
451 public static PathCounters noopPathCounters() {
452 return NoopPathCounters.INSTANCE;
453 }
454
455
456
457
458
459
460 @Deprecated
461 public Counters() {
462
463 }
464 }