Skip to content

Commit 6589f5e

Browse files
committed
fix hashCode & equals implementation for AtomicPositiveInteger
1 parent 63cd838 commit 6589f5e

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/AtomicPositiveInteger.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,16 @@ public String toString() {
167167
public int hashCode() {
168168
final int prime = 31;
169169
int result = 1;
170-
result = prime * result + ((i == null) ? 0 : i.hashCode());
170+
result = prime * result + i.hashCode();
171171
return result;
172172
}
173173

174174
@Override
175175
public boolean equals(Object obj) {
176176
if (this == obj) return true;
177-
if (obj == null) return false;
178-
if (getClass() != obj.getClass()) return false;
177+
if (!(obj instanceof AtomicPositiveInteger)) return false;
179178
AtomicPositiveInteger other = (AtomicPositiveInteger) obj;
180-
if (i == null) {
181-
if (other.i != null) return false;
182-
} else if (!i.equals(other.i)) return false;
183-
return true;
179+
return i.intValue() == other.i.intValue();
184180
}
185181

186182
}

dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/AtomicPositiveIntegerTest.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
/*
2-
* Copyright 1999-2011 Alibaba Group.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
1+
/*
2+
* Copyright 1999-2011 Alibaba Group.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
1616
package com.alibaba.dubbo.common.utils;
1717

1818
import static org.hamcrest.CoreMatchers.allOf;
@@ -158,4 +158,10 @@ public void test_addAndGet() throws Exception {
158158
assertEquals(2, get);
159159
assertEquals(2, i3.get());
160160
}
161+
162+
@Test
163+
public void test_equals() {
164+
assertEquals(new AtomicPositiveInteger(), new AtomicPositiveInteger());
165+
assertEquals(new AtomicPositiveInteger(1), new AtomicPositiveInteger(1));
166+
}
161167
}

0 commit comments

Comments
 (0)