Skip to content

Commit 416ce8d

Browse files
author
Gary Gregory
committed
Implement hashCode() and equals() on counters.
1 parent ef5b7a3 commit 416ce8d

2 files changed

Lines changed: 168 additions & 0 deletions

File tree

src/main/java/org/apache/commons/io/file/Counters.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.commons.io.file;
1919

2020
import java.math.BigInteger;
21+
import java.util.Objects;
2122

2223
/**
2324
* Provides counters for files, directories, and sizes, as a visit proceeds.
@@ -50,6 +51,20 @@ protected AbstractPathCounters(final Counter byteCounter, final Counter director
5051
this.fileCounter = fileCounter;
5152
}
5253

54+
@Override
55+
public boolean equals(Object obj) {
56+
if (this == obj) {
57+
return true;
58+
}
59+
if (!(obj instanceof AbstractPathCounters)) {
60+
return false;
61+
}
62+
AbstractPathCounters other = (AbstractPathCounters) obj;
63+
return Objects.equals(byteCounter, other.byteCounter)
64+
&& Objects.equals(directoryCounter, other.directoryCounter)
65+
&& Objects.equals(fileCounter, other.fileCounter);
66+
}
67+
5368
@Override
5469
public Counter getByteCounter() {
5570
return byteCounter;
@@ -70,6 +85,11 @@ public Counter getFileCounter() {
7085
return this.fileCounter;
7186
}
7287

88+
@Override
89+
public int hashCode() {
90+
return Objects.hash(byteCounter, directoryCounter, fileCounter);
91+
}
92+
7393
@Override
7494
public String toString() {
7595
return String.format("%,d files, %,d directories, %,d bytes", Long.valueOf(fileCounter.get()),
@@ -91,6 +111,18 @@ public void add(final long val) {
91111

92112
}
93113

114+
@Override
115+
public boolean equals(Object obj) {
116+
if (this == obj) {
117+
return true;
118+
}
119+
if (!(obj instanceof BigIntegerCounter)) {
120+
return false;
121+
}
122+
BigIntegerCounter other = (BigIntegerCounter) obj;
123+
return Objects.equals(value, other.value);
124+
}
125+
94126
@Override
95127
public long get() {
96128
return value.longValueExact();
@@ -106,6 +138,11 @@ public Long getLong() {
106138
return Long.valueOf(value.longValueExact());
107139
}
108140

141+
@Override
142+
public int hashCode() {
143+
return Objects.hash(value);
144+
}
145+
109146
@Override
110147
public void increment() {
111148
value = value.add(BigInteger.ONE);
@@ -184,6 +221,18 @@ public void add(final long add) {
184221

185222
}
186223

224+
@Override
225+
public boolean equals(Object obj) {
226+
if (this == obj) {
227+
return true;
228+
}
229+
if (!(obj instanceof LongCounter)) {
230+
return false;
231+
}
232+
LongCounter other = (LongCounter) obj;
233+
return value == other.value;
234+
}
235+
187236
@Override
188237
public long get() {
189238
return value;
@@ -199,6 +248,11 @@ public Long getLong() {
199248
return Long.valueOf(value);
200249
}
201250

251+
@Override
252+
public int hashCode() {
253+
return Objects.hash(value);
254+
}
255+
202256
@Override
203257
public void increment() {
204258
value++;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.io.file;
19+
20+
import org.apache.commons.io.file.Counters.Counter;
21+
import org.apache.commons.io.file.Counters.PathCounters;
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
public class CountersEqualsAndHashCodeTest {
26+
27+
@Test
28+
public void testBigIntegerCounterEquals() {
29+
testEquals(Counters.bigIntegerCounter(), Counters.bigIntegerCounter());
30+
}
31+
32+
@Test
33+
public void testBigIntegerHashCode() {
34+
testHashCodes(Counters.bigIntegerCounter(), Counters.bigIntegerCounter());
35+
}
36+
37+
private void testEquals(final Counter counter1, final Counter counter2) {
38+
Assertions.assertEquals(counter1, counter2);
39+
counter1.increment();
40+
Assertions.assertNotEquals(counter1, counter2);
41+
counter2.increment();
42+
Assertions.assertEquals(counter1, counter2);
43+
}
44+
45+
private void testEqualsByteCounters(final PathCounters counter1, final PathCounters counter2) {
46+
Assertions.assertEquals(counter1, counter2);
47+
counter1.getByteCounter().increment();
48+
Assertions.assertNotEquals(counter1, counter2);
49+
counter2.getByteCounter().increment();
50+
Assertions.assertEquals(counter1, counter2);
51+
}
52+
53+
private void testEqualsDirectoryCounters(final PathCounters counter1, final PathCounters counter2) {
54+
Assertions.assertEquals(counter1, counter2);
55+
counter1.getDirectoryCounter().increment();
56+
Assertions.assertNotEquals(counter1, counter2);
57+
counter2.getDirectoryCounter().increment();
58+
Assertions.assertEquals(counter1, counter2);
59+
}
60+
61+
private void testEqualsFileCounters(final PathCounters counter1, final PathCounters counter2) {
62+
Assertions.assertEquals(counter1, counter2);
63+
counter1.getFileCounter().increment();
64+
Assertions.assertNotEquals(counter1, counter2);
65+
counter2.getFileCounter().increment();
66+
Assertions.assertEquals(counter1, counter2);
67+
}
68+
69+
private void testHashCodeFileCounters(final PathCounters counter1, final PathCounters counter2) {
70+
Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
71+
counter1.getFileCounter().increment();
72+
Assertions.assertNotEquals(counter1.hashCode(), counter2.hashCode());
73+
counter2.getFileCounter().increment();
74+
Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
75+
}
76+
77+
private void testHashCodes(final Counter counter1, final Counter counter2) {
78+
Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
79+
counter1.increment();
80+
Assertions.assertNotEquals(counter1.hashCode(), counter2.hashCode());
81+
counter2.increment();
82+
Assertions.assertEquals(counter1.hashCode(), counter2.hashCode());
83+
}
84+
85+
@Test
86+
public void testLongCounterEquals() {
87+
testEquals(Counters.longCounter(), Counters.longCounter());
88+
}
89+
90+
@Test
91+
public void testLongCounterHashCodes() {
92+
testHashCodes(Counters.longCounter(), Counters.longCounter());
93+
}
94+
95+
@Test
96+
public void testLongPathCountersEqualsByteCounters() {
97+
testEqualsByteCounters(Counters.longPathCounters(), Counters.longPathCounters());
98+
}
99+
100+
@Test
101+
public void testLongPathCountersEqualsDirectoryCounters() {
102+
testEqualsDirectoryCounters(Counters.longPathCounters(), Counters.longPathCounters());
103+
}
104+
105+
@Test
106+
public void testLongPathCountersEqualsFileCounters() {
107+
testEqualsFileCounters(Counters.longPathCounters(), Counters.longPathCounters());
108+
}
109+
110+
@Test
111+
public void testLongPathCountersHashCodeFileCounters() {
112+
testHashCodeFileCounters(Counters.longPathCounters(), Counters.longPathCounters());
113+
}
114+
}

0 commit comments

Comments
 (0)