Implement hashCode() and equals() on path counters.
MODIFY
src/main/java/org/apache/commons/io/file/CountingPathVisitor.java
src/main/java/org/apache/commons/io/file/CountingPathVisitor.java
@@ -65,6 +65,18 @@ public CountingPathVisitor(final PathCounters pathCounter) {
this.pathCounters = Objects.requireNonNull(pathCounter, "pathCounter");
}
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof CountingPathVisitor)) {
+ return false;
+ }
+ CountingPathVisitor other = (CountingPathVisitor) obj;
+ return Objects.equals(pathCounters, other.pathCounters);
+ }
+
/**
* Gets the visitation counts.
*
@@ -74,6 +86,11 @@ public PathCounters getPathCounters() {
return pathCounters;
}
+ @Override
+ public int hashCode() {
+ return Objects.hash(pathCounters);
+ }
+
@Override
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
pathCounters.getDirectoryCounter().increment();

MODIFY
src/test/java/org/apache/commons/io/file/CountingPathVisitorTest.java
src/test/java/org/apache/commons/io/file/CountingPathVisitorTest.java
import java.nio.file.Files;
import java.nio.file.Path;
+import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@ParameterizedTest
@MethodSource("countingPathVisitors")
public void testCountEmptyFolder(final CountingPathVisitor visitor) throws IOException {
+ checkZeroCounts(visitor);
final Path tempDir = Files.createTempDirectory(getClass().getCanonicalName());
try {
assertCounts(1, 0, 0, PathUtils.visitFileTree(visitor, tempDir));
@@ -51,6 +53,7 @@ public void testCountEmptyFolder(final CountingPathVisitor visitor) throws IOExc
@ParameterizedTest
@MethodSource("countingPathVisitors")
public void testCountFolders1FileSize0(final CountingPathVisitor visitor) throws IOException {
+ checkZeroCounts(visitor);
assertCounts(1, 1, 0, PathUtils.visitFileTree(visitor,
"src/test/resources/org/apache/commons/io/dirs-1-file-size-0"));
}
@@ -61,6 +64,7 @@ public void testCountFolders1FileSize0(final CountingPathVisitor visitor) throws
@ParameterizedTest
@MethodSource("countingPathVisitors")
public void testCountFolders1FileSize1(final CountingPathVisitor visitor) throws IOException {
+ checkZeroCounts(visitor);
assertCounts(1, 1, 1, PathUtils.visitFileTree(visitor,
"src/test/resources/org/apache/commons/io/dirs-1-file-size-1"));
}
@@ -71,10 +75,16 @@ public void testCountFolders1FileSize1(final CountingPathVisitor visitor) throws
@ParameterizedTest
@MethodSource("countingPathVisitors")
public void testCountFolders2FileSize2(final CountingPathVisitor visitor) throws IOException {
+ checkZeroCounts(visitor);
assertCounts(3, 2, 2, PathUtils.visitFileTree(visitor,
"src/test/resources/org/apache/commons/io/dirs-2-file-size-2"));
}
+ private void checkZeroCounts(final CountingPathVisitor visitor) {
+ Assertions.assertEquals(CountingPathVisitor.withLongCounters(), visitor);
+ Assertions.assertEquals(CountingPathVisitor.withBigIntegerCounters(), visitor);
+ }
+
@ParameterizedTest
@MethodSource("countingPathVisitors")
void testToString(final CountingPathVisitor visitor) {

Implement hashCode() and equals() on path counters.