Skip to content

Commit 669b000

Browse files
committed
Merge pull request msgpack#168 from msgpack/issue_164
RawStringValueImpl#equals and #hashcode use the same logics with StringV...
2 parents 3291956 + 2cc6605 commit 669b000

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

msgpack-core/src/main/java/org/msgpack/value/impl/RawStringValueImpl.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.msgpack.value.impl;
22

33
import org.msgpack.core.MessagePacker;
4+
import org.msgpack.core.MessageStringCodingException;
45
import org.msgpack.value.*;
56

67
import java.io.IOException;
@@ -53,12 +54,16 @@ public boolean equals(Object o) {
5354
if (!v.isString()) {
5455
return false;
5556
}
56-
StringValue sv = v.asString();
57-
return sv.toByteBuffer().equals(byteBuffer);
57+
try {
58+
return toString().equals(v.asString().toString());
59+
} catch (MessageStringCodingException ex) {
60+
return false;
61+
}
62+
5863
}
5964

6065
@Override
6166
public int hashCode() {
62-
return byteBuffer.hashCode();
67+
return toString().hashCode();
6368
}
6469
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.msgpack.value
2+
3+
import org.msgpack.core.MessagePackSpec
4+
5+
class RawStringValueImplTest extends MessagePackSpec {
6+
7+
"StringValue" should {
8+
"return the same hash code if they are equal" in {
9+
val str = "a"
10+
val a1 = ValueFactory.newRawString(str.getBytes("UTF-8"))
11+
val a2 = ValueFactory.newString(str)
12+
13+
a1.shouldEqual(a2)
14+
a1.hashCode.shouldEqual(a2.hashCode)
15+
a2.shouldEqual(a1)
16+
a2.hashCode.shouldEqual(a1.hashCode)
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)