Skip to content

Commit 3491e22

Browse files
authored
Make UserState JSONObjects immutable (#1203)
* Fix ConcurrentModificationException * Have mutable synchronization centralized
1 parent 82d8f63 commit 3491e22

File tree

7 files changed

+369
-144
lines changed

7 files changed

+369
-144
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Modified MIT License
3+
* <p>
4+
* Copyright 2020 OneSignal
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
* <p>
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
* <p>
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
package com.onesignal;
29+
30+
import org.json.JSONException;
31+
import org.json.JSONObject;
32+
33+
class ImmutableJSONObject {
34+
35+
private final JSONObject jsonObject;
36+
37+
public ImmutableJSONObject() {
38+
this.jsonObject = new JSONObject();
39+
}
40+
41+
public ImmutableJSONObject(JSONObject jsonObject) {
42+
this.jsonObject = jsonObject;
43+
}
44+
45+
public long getLong(String name) throws JSONException {
46+
return jsonObject.getLong(name);
47+
}
48+
49+
public boolean has(String name) {
50+
return jsonObject.has(name);
51+
}
52+
53+
public Object opt(String name) {
54+
return jsonObject.opt(name);
55+
}
56+
57+
public String optString(String name) {
58+
return jsonObject.optString(name);
59+
}
60+
61+
public String optString(String name, String fallback) {
62+
return jsonObject.optString(name, fallback);
63+
}
64+
65+
public boolean optBoolean(String name) {
66+
return jsonObject.optBoolean(name);
67+
}
68+
69+
public boolean optBoolean(String name, boolean fallback) {
70+
return jsonObject.optBoolean(name, fallback);
71+
}
72+
73+
public long optLong(String name) {
74+
return jsonObject.optLong(name);
75+
}
76+
77+
public int optInt(String name) {
78+
return jsonObject.optInt(name);
79+
}
80+
81+
public int optInt(String name, int fallback) {
82+
return jsonObject.optInt(name, fallback);
83+
}
84+
85+
public JSONObject optJSONObject(String name) {
86+
return jsonObject.optJSONObject(name);
87+
}
88+
89+
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/JSONUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static String toStringNE(JSONArray jsonArray) {
130130
return strArray + "]";
131131
}
132132

133-
static JSONObject getJSONObjectWithoutBlankValues(JSONObject jsonObject, String getKey) {
133+
static JSONObject getJSONObjectWithoutBlankValues(ImmutableJSONObject jsonObject, String getKey) {
134134
if (!jsonObject.has(getKey))
135135
return null;
136136

0 commit comments

Comments
 (0)