Skip to content

Commit b179713

Browse files
authored
support nulls in TextNode equals (#4379)
1 parent f57efed commit b179713

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/main/java/com/fasterxml/jackson/databind/node/TextNode.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.databind.node;
22

33
import java.io.IOException;
4+
import java.util.Objects;
45

56
import com.fasterxml.jackson.core.*;
67
import com.fasterxml.jackson.core.io.CharTypes;
@@ -164,13 +165,16 @@ public boolean equals(Object o)
164165
if (o == this) return true;
165166
if (o == null) return false;
166167
if (o instanceof TextNode) {
167-
return ((TextNode) o)._value.equals(_value);
168+
TextNode otherNode = (TextNode) o;
169+
return Objects.equals(otherNode._value, _value);
168170
}
169171
return false;
170172
}
171173

172174
@Override
173-
public int hashCode() { return _value.hashCode(); }
175+
public int hashCode() {
176+
return Objects.hashCode(_value);
177+
}
174178

175179
@Deprecated // since 2.10
176180
protected static void appendQuoted(StringBuilder sb, String content)

src/test/java/com/fasterxml/jackson/databind/node/TextNodeTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.databind.node;
22

3+
import static org.junit.Assert.assertNotEquals;
4+
35
public class TextNodeTest extends NodeTestBase
46
{
57
public void testText()
@@ -36,4 +38,19 @@ public void testText()
3638
assertFalse(TextNode.valueOf("false").asBoolean(true));
3739
assertFalse(TextNode.valueOf("false").asBoolean(false));
3840
}
41+
42+
public void testEquals()
43+
{
44+
assertEquals(new TextNode(null), new TextNode(null));
45+
assertEquals(new TextNode("abc"), new TextNode("abc"));
46+
assertNotEquals(new TextNode(null), new TextNode("def"));
47+
assertNotEquals(new TextNode("abc"), new TextNode("def"));
48+
assertNotEquals(new TextNode("abc"), new TextNode(null));
49+
}
50+
51+
public void testHashCode()
52+
{
53+
assertEquals(0, new TextNode(null).hashCode());
54+
assertEquals("abc".hashCode(), new TextNode("abc").hashCode());
55+
}
3956
}

0 commit comments

Comments
 (0)