This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 416ce8d  Implement hashCode() and equals() on counters.
416ce8d is described below

commit 416ce8d6e572e60e1cfaecdc482c55afb7df96dd
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Mon Nov 25 20:28:40 2019 -0500

    Implement hashCode() and equals() on counters.
---
 .../java/org/apache/commons/io/file/Counters.java  |  54 ++++++++++
 .../io/file/CountersEqualsAndHashCodeTest.java     | 114 +++++++++++++++++++++
 2 files changed, 168 insertions(+)

diff --git a/src/main/java/org/apache/commons/io/file/Counters.java 
b/src/main/java/org/apache/commons/io/file/Counters.java
index 4d1c33a..e3fad94 100644
--- a/src/main/java/org/apache/commons/io/file/Counters.java
+++ b/src/main/java/org/apache/commons/io/file/Counters.java
@@ -18,6 +18,7 @@
 package org.apache.commons.io.file;
 
 import java.math.BigInteger;
+import java.util.Objects;
 
 /**
  * Provides counters for files, directories, and sizes, as a visit proceeds.
@@ -51,6 +52,20 @@ public class Counters {
         }
 
         @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (!(obj instanceof AbstractPathCounters)) {
+                return false;
+            }
+            AbstractPathCounters other = (AbstractPathCounters) obj;
+            return Objects.equals(byteCounter, other.byteCounter)
+                    && Objects.equals(directoryCounter, other.directoryCounter)
+                    && Objects.equals(fileCounter, other.fileCounter);
+        }
+
+        @Override
         public Counter getByteCounter() {
             return byteCounter;
         }
@@ -71,6 +86,11 @@ public class Counters {
         }
 
         @Override
+        public int hashCode() {
+            return Objects.hash(byteCounter, directoryCounter, fileCounter);
+        }
+
+        @Override
         public String toString() {
             return String.format("%,d files, %,d directories, %,d bytes", 
Long.valueOf(fileCounter.get()),
                     Long.valueOf(directoryCounter.get()), 
Long.valueOf(byteCounter.get()));
@@ -92,6 +112,18 @@ public class Counters {
         }
 
         @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (!(obj instanceof BigIntegerCounter)) {
+                return false;
+            }
+            BigIntegerCounter other = (BigIntegerCounter) obj;
+            return Objects.equals(value, other.value);
+        }
+
+        @Override
         public long get() {
             return value.longValueExact();
         }
@@ -107,6 +139,11 @@ public class Counters {
         }
 
         @Override
+        public int hashCode() {
+            return Objects.hash(value);
+        }
+
+        @Override
         public void increment() {
             value = value.add(BigInteger.ONE);
         }
@@ -185,6 +222,18 @@ public class Counters {
         }
 
         @Override
+        public boolean equals(Object obj) {
+            if (this == obj) {
+                return true;
+            }
+            if (!(obj instanceof LongCounter)) {
+                return false;
+            }
+            LongCounter other = (LongCounter) obj;
+            return value == other.value;
+        }
+
+        @Override
         public long get() {
             return value;
         }
@@ -200,6 +249,11 @@ public class Counters {
         }
 
         @Override
+        public int hashCode() {
+            return Objects.hash(value);
+        }
+
+        @Override
         public void increment() {
             value++;
         }
diff --git 
a/src/test/java/org/apache/commons/io/file/CountersEqualsAndHashCodeTest.java 
b/src/test/java/org/apache/commons/io/file/CountersEqualsAndHashCodeTest.java
new file mode 100644
index 0000000..43388bb
--- /dev/null
+++ 
b/src/test/java/org/apache/commons/io/file/CountersEqualsAndHashCodeTest.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.io.file;
+
+import org.apache.commons.io.file.Counters.Counter;
+import org.apache.commons.io.file.Counters.PathCounters;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class CountersEqualsAndHashCodeTest {
+
+    @Test
+    public void testBigIntegerCounterEquals() {
+        testEquals(Counters.bigIntegerCounter(), Counters.bigIntegerCounter());
+    }
+
+    @Test
+    public void testBigIntegerHashCode() {
+        testHashCodes(Counters.bigIntegerCounter(), 
Counters.bigIntegerCounter());
+    }
+
+    private void testEquals(final Counter counter1, final Counter counter2) {
+        Assertions.assertEquals(counter1, counter2);
+        counter1.increment();
+        Assertions.assertNotEquals(counter1, counter2);
+        counter2.increment();
+        Assertions.assertEquals(counter1, counter2);
+    }
+
+    private void testEqualsByteCounters(final PathCounters counter1, final 
PathCounters counter2) {
+        Assertions.assertEquals(counter1, counter2);
+        counter1.getByteCounter().increment();
+        Assertions.assertNotEquals(counter1, counter2);
+        counter2.getByteCounter().increment();
+        Assertions.assertEquals(counter1, counter2);
+    }
+
+    private void testEqualsDirectoryCounters(final PathCounters counter1, 
final PathCounters counter2) {
+        Assertions.assertEquals(counter1, counter2);
+        counter1.getDirectoryCounter().increment();
+        Assertions.assertNotEquals(counter1, counter2);
+        counter2.getDirectoryCounter().increment();
+        Assertions.assertEquals(counter1, counter2);
+    }
+
+    private void testEqualsFileCounters(final PathCounters counter1, final 
PathCounters counter2) {
+        Assertions.assertEquals(counter1, counter2);
+        counter1.getFileCounter().increment();
+        Assertions.assertNotEquals(counter1, counter2);
+        counter2.getFileCounter().increment();
+        Assertions.assertEquals(counter1, counter2);
+    }
+
+    private void testHashCodeFileCounters(final PathCounters counter1, final 
PathCounters counter2) {
+        Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
+        counter1.getFileCounter().increment();
+        Assertions.assertNotEquals(counter1.hashCode(), counter2.hashCode());
+        counter2.getFileCounter().increment();
+        Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
+    }
+
+    private void testHashCodes(final Counter counter1, final Counter counter2) 
{
+        Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
+        counter1.increment();
+        Assertions.assertNotEquals(counter1.hashCode(), counter2.hashCode());
+        counter2.increment();
+        Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
+    }
+
+    @Test
+    public void testLongCounterEquals() {
+        testEquals(Counters.longCounter(), Counters.longCounter());
+    }
+
+    @Test
+    public void testLongCounterHashCodes() {
+        testHashCodes(Counters.longCounter(), Counters.longCounter());
+    }
+
+    @Test
+    public void testLongPathCountersEqualsByteCounters() {
+        testEqualsByteCounters(Counters.longPathCounters(), 
Counters.longPathCounters());
+    }
+
+    @Test
+    public void testLongPathCountersEqualsDirectoryCounters() {
+        testEqualsDirectoryCounters(Counters.longPathCounters(), 
Counters.longPathCounters());
+    }
+
+    @Test
+    public void testLongPathCountersEqualsFileCounters() {
+        testEqualsFileCounters(Counters.longPathCounters(), 
Counters.longPathCounters());
+    }
+
+    @Test
+    public void testLongPathCountersHashCodeFileCounters() {
+        testHashCodeFileCounters(Counters.longPathCounters(), 
Counters.longPathCounters());
+    }
+}

Reply via email to