Skip to content

Commit c1d7975

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

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ public boolean equals(Object obj) {
116116
if (this == obj) {
117117
return true;
118118
}
119-
if (!(obj instanceof BigIntegerCounter)) {
119+
if (!(obj instanceof Counter)) {
120120
return false;
121121
}
122-
BigIntegerCounter other = (BigIntegerCounter) obj;
123-
return Objects.equals(value, other.value);
122+
Counter other = (Counter) obj;
123+
return Objects.equals(value, other.getBigInteger());
124124
}
125125

126126
@Override
@@ -226,11 +226,11 @@ public boolean equals(Object obj) {
226226
if (this == obj) {
227227
return true;
228228
}
229-
if (!(obj instanceof LongCounter)) {
229+
if (!(obj instanceof Counter)) {
230230
return false;
231231
}
232-
LongCounter other = (LongCounter) obj;
233-
return value == other.value;
232+
Counter other = (Counter) obj;
233+
return value == other.get();
234234
}
235235

236236
@Override

src/test/java/org/apache/commons/io/file/CountersEqualsAndHashCodeTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ public void testLongCounterEquals() {
8787
testEquals(Counters.longCounter(), Counters.longCounter());
8888
}
8989

90+
@Test
91+
public void testLongCounterMixEquals() {
92+
testEquals(Counters.longCounter(), Counters.bigIntegerCounter());
93+
testEquals(Counters.bigIntegerCounter(), Counters.longCounter());
94+
}
95+
9096
@Test
9197
public void testLongCounterHashCodes() {
9298
testHashCodes(Counters.longCounter(), Counters.longCounter());
@@ -111,4 +117,9 @@ public void testLongPathCountersEqualsFileCounters() {
111117
public void testLongPathCountersHashCodeFileCounters() {
112118
testHashCodeFileCounters(Counters.longPathCounters(), Counters.longPathCounters());
113119
}
120+
121+
@Test
122+
public void testMix() {
123+
testHashCodeFileCounters(Counters.longPathCounters(), Counters.bigIntegerPathCounters());
124+
}
114125
}

0 commit comments

Comments
 (0)