Skip to content

Commit e4e9e34

Browse files
committed
RawStringValueImpl#equals and #hashcode use the same logics with StringValueImpl
1 parent 430b1e7 commit e4e9e34

2 files changed

Lines changed: 21 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.ValueType;
56
import org.msgpack.value.*;
67

@@ -54,12 +55,16 @@ public boolean equals(Object o) {
5455
if (!v.isString()) {
5556
return false;
5657
}
57-
StringValue sv = v.asString();
58-
return sv.toByteBuffer().equals(byteBuffer);
58+
try {
59+
return toString().equals(v.asString().toString());
60+
} catch (MessageStringCodingException ex) {
61+
return false;
62+
}
63+
5964
}
6065

6166
@Override
6267
public int hashCode() {
63-
return byteBuffer.hashCode();
68+
return toString().hashCode();
6469
}
6570
}

msgpack-core/src/test/scala/org/msgpack/value/ValueFactoryTest.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,17 @@ class ValueFactoryTest extends MessagePackSpec {
5050
}
5151

5252
}
53+
54+
"StringValue" should {
55+
"return the same hash code if they are equal" in {
56+
val str = "a"
57+
val a1 = ValueFactory.newRawString(str.getBytes("UTF-8"))
58+
val a2 = ValueFactory.newString(str)
59+
60+
a1.shouldEqual(a2)
61+
a1.hashCode.shouldEqual(a2.hashCode)
62+
a2.shouldEqual(a1)
63+
a2.hashCode.shouldEqual(a1.hashCode)
64+
}
65+
}
5366
}

0 commit comments

Comments
 (0)