Skip to content

Commit 0f01090

Browse files
committed
add JSONBuilder and BundleBuilder
1 parent 8f30865 commit 0f01090

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.isolpro.custom;
2+
3+
import android.os.Bundle;
4+
5+
public class BundleBuilder {
6+
7+
private final Bundle bundle;
8+
9+
public BundleBuilder(Bundle bundle) {
10+
this.bundle = bundle;
11+
}
12+
13+
public static BundleBuilder get(Bundle bundle) {
14+
return new BundleBuilder(
15+
bundle != null ? bundle : new Bundle()
16+
);
17+
}
18+
19+
public static BundleBuilder get() {
20+
return get(new Bundle());
21+
}
22+
23+
public BundleBuilder putString(String key, String value) {
24+
bundle.putString(key, value);
25+
return this;
26+
}
27+
28+
public BundleBuilder putInt(String key, int value) {
29+
bundle.putInt(key, value);
30+
return this;
31+
}
32+
33+
public BundleBuilder putBoolean(String key, boolean value) {
34+
bundle.putBoolean(key, value);
35+
return this;
36+
}
37+
38+
public String getString(String key, String defaultValue) {
39+
return bundle.containsKey(key) ? bundle.getString(key) : defaultValue;
40+
}
41+
42+
public String getString(String key) {
43+
return getString(key, "");
44+
}
45+
46+
public int getInt(String key, int defaultValue) {
47+
return bundle.getInt(key, defaultValue);
48+
}
49+
50+
public int getInt(String key) {
51+
return getInt(key, 0);
52+
}
53+
54+
public boolean getBoolean(String key, boolean defaultValue) {
55+
return bundle.getBoolean(key, defaultValue);
56+
}
57+
58+
public boolean getBoolean(String key) {
59+
return getBoolean(key, false);
60+
}
61+
62+
public Bundle build() {
63+
return bundle;
64+
}
65+
66+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.isolpro.custom;
2+
3+
import android.util.Log;
4+
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
8+
public class JSONBuilder {
9+
10+
private final JSONObject jsonObject;
11+
12+
private JSONBuilder(JSONObject jsonObject) {
13+
this.jsonObject = jsonObject;
14+
}
15+
16+
public static JSONBuilder get(JSONObject jsonObject) {
17+
return new JSONBuilder(
18+
jsonObject != null ? jsonObject : new JSONObject()
19+
);
20+
}
21+
22+
public static JSONBuilder get() {
23+
return get(new JSONObject());
24+
}
25+
26+
private JSONBuilder putHandled(String key, Object value, boolean toPut) {
27+
if (toPut) {
28+
try {
29+
jsonObject.put(key, value);
30+
} catch (JSONException e) {
31+
Log.e(JSONBuilder.class.toString(), e.getMessage());
32+
}
33+
}
34+
35+
return this;
36+
}
37+
38+
public JSONBuilder put(String key, String value, boolean toPut) {
39+
return putHandled(key, value, toPut);
40+
}
41+
42+
public JSONBuilder put(String key, String value) {
43+
return putHandled(key, value, true);
44+
}
45+
46+
public JSONBuilder put(String key, int value, boolean toPut) {
47+
return putHandled(key, value, toPut);
48+
}
49+
50+
public JSONBuilder put(String key, int value) {
51+
return putHandled(key, value, true);
52+
}
53+
54+
public JSONBuilder put(String key, float value, boolean toPut) {
55+
return putHandled(key, value, toPut);
56+
}
57+
58+
public JSONBuilder put(String key, float value) {
59+
return putHandled(key, value, true);
60+
}
61+
62+
public JSONBuilder put(String key, boolean value, boolean toPut) {
63+
return putHandled(key, value, toPut);
64+
}
65+
66+
public JSONBuilder put(String key, boolean value) {
67+
return putHandled(key, value, true);
68+
}
69+
70+
public JSONObject build() {
71+
return jsonObject;
72+
}
73+
74+
}

0 commit comments

Comments
 (0)