Skip to content

Commit 8f74877

Browse files
gawim0mus
authored andcommitted
Implemented hashCode() with caching for JsonArrayImpl, JsonObjectImpl and JsonNumber. (#125)
Signed-off-by: Jean-Philippe Gariépy <[email protected]>
1 parent c8daac3 commit 8f74877

File tree

7 files changed

+80
-8
lines changed

7 files changed

+80
-8
lines changed

impl/src/main/java/org/glassfish/json/JsonArrayBuilderImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -360,6 +360,7 @@ private void validateValue(Object value) {
360360
private static final class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray {
361361
private final List<JsonValue> valueList; // Unmodifiable
362362
private final BufferPool bufferPool;
363+
private int hashCode;
363364

364365
JsonArrayImpl(List<JsonValue> valueList, BufferPool bufferPool) {
365366
this.valueList = valueList;
@@ -461,6 +462,14 @@ public JsonValue get(int index) {
461462
return valueList.get(index);
462463
}
463464

465+
@Override
466+
public int hashCode() {
467+
if (hashCode == 0) {
468+
hashCode = super.hashCode();
469+
}
470+
return hashCode;
471+
}
472+
464473
@Override
465474
public String toString() {
466475
StringWriter sw = new StringWriter();

impl/src/main/java/org/glassfish/json/JsonNumberImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -28,6 +28,8 @@
2828
*/
2929
abstract class JsonNumberImpl implements JsonNumber {
3030

31+
private int hashCode;
32+
3133
static JsonNumber getJsonNumber(int num) {
3234
return new JsonIntNumber(num);
3335
}
@@ -240,7 +242,10 @@ public ValueType getValueType() {
240242

241243
@Override
242244
public int hashCode() {
243-
return bigDecimalValue().hashCode();
245+
if (hashCode == 0) {
246+
hashCode = bigDecimalValue().hashCode();
247+
}
248+
return hashCode;
244249
}
245250

246251
@Override

impl/src/main/java/org/glassfish/json/JsonObjectBuilderImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -202,6 +202,7 @@ private void validateValue(Object value) {
202202
private static final class JsonObjectImpl extends AbstractMap<String, JsonValue> implements JsonObject {
203203
private final Map<String, JsonValue> valueMap; // unmodifiable
204204
private final BufferPool bufferPool;
205+
private int hashCode;
205206

206207
JsonObjectImpl(Map<String, JsonValue> valueMap, BufferPool bufferPool) {
207208
this.valueMap = valueMap;
@@ -294,6 +295,14 @@ public Set<Entry<String, JsonValue>> entrySet() {
294295
return valueMap.entrySet();
295296
}
296297

298+
@Override
299+
public int hashCode() {
300+
if (hashCode == 0) {
301+
hashCode = super.hashCode();
302+
}
303+
return hashCode;
304+
}
305+
297306
@Override
298307
public String toString() {
299308
StringWriter sw = new StringWriter();

tests/src/test/java/org/glassfish/json/tests/JsonArrayTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -111,4 +111,18 @@ public void testArrayBuilderNpe() {
111111
}
112112
}
113113

114+
public void testHashCode() {
115+
JsonArray array1 = Json.createArrayBuilder().add(1).add(2).add(3).build();
116+
assertTrue(array1.hashCode() == array1.hashCode()); //1st call compute hashCode, 2nd call returns cached value
117+
118+
JsonArray array2 = Json.createArrayBuilder().add(1).add(2).add(3).build();
119+
assertTrue(array1.hashCode() == array2.hashCode());
120+
121+
JsonArray array3 = Json.createArrayBuilder().build(); //org.glassfish.json.JsonArrayBuilderImpl.JsonArrayImpl
122+
JsonArray array4 = JsonValue.EMPTY_JSON_ARRAY; //javax.json.EmptyArray
123+
124+
assertTrue(array3.equals(array4));
125+
assertTrue(array3.hashCode() == array4.hashCode()); //equal instances have same hashCode
126+
}
127+
114128
}

tests/src/test/java/org/glassfish/json/tests/JsonNumberTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -206,5 +206,14 @@ public void testBigIntegerExact() {
206206
}
207207
}
208208

209+
public void testHashCode() {
210+
JsonNumber jsonNumber1 = Json.createValue(1);
211+
assertTrue(jsonNumber1.hashCode() == jsonNumber1.bigDecimalValue().hashCode());
212+
213+
JsonNumber jsonNumber2 = Json.createValue(1);
214+
215+
assertTrue(jsonNumber1.equals(jsonNumber2));
216+
assertTrue(jsonNumber1.hashCode() == jsonNumber2.hashCode());
217+
}
209218

210219
}

tests/src/test/java/org/glassfish/json/tests/JsonObjectTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -122,4 +122,18 @@ public void testObjectBuilderNpe() {
122122
}
123123
}
124124

125+
public void testHashCode() {
126+
JsonObject object1 = Json.createObjectBuilder().add("a", 1).add("b", 2).add("c", 3).build();
127+
assertTrue(object1.hashCode() == object1.hashCode()); //1st call compute hashCode, 2nd call returns cached value
128+
129+
JsonObject object2 = Json.createObjectBuilder().add("a", 1).add("b", 2).add("c", 3).build();
130+
assertTrue(object1.hashCode() == object2.hashCode());
131+
132+
JsonObject object3 = Json.createObjectBuilder().build(); //org.glassfish.json.JsonArrayBuilderImpl.JsonArrayImpl
133+
JsonObject object4 = JsonValue.EMPTY_JSON_OBJECT; //javax.json.EmptyObject
134+
135+
assertTrue(object3.equals(object4));
136+
assertTrue(object3.hashCode() == object4.hashCode()); //equal instances have same hashCode
137+
}
138+
125139
}

tests/src/test/java/org/glassfish/json/tests/JsonStringTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -45,6 +45,18 @@ public void testToString() throws Exception {
4545
escapedString("abc\"\\/abc");
4646
}
4747

48+
public void testHashCode() {
49+
String string1 = new String("a");
50+
JsonString jsonString1 = Json.createValue(string1);
51+
assertTrue(jsonString1.hashCode() == jsonString1.getString().hashCode());
52+
53+
String string2 = new String("a");
54+
JsonString jsonString2 = Json.createValue(string2);
55+
56+
assertTrue(jsonString1.equals(jsonString2));
57+
assertTrue(jsonString1.hashCode() == jsonString2.hashCode());
58+
}
59+
4860
void escapedString(String str) throws Exception {
4961
JsonArray exp = Json.createArrayBuilder().add(str).build();
5062
String parseStr = "["+exp.get(0).toString()+"]";

0 commit comments

Comments
 (0)