Skip to content

Commit 0507438

Browse files
Andrei_PaikinAndrei_Paikin
authored andcommitted
change length comparison to isEmpty method
1 parent a490ebd commit 0507438

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

CDL.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public static JSONArray toJSONArray(JSONArray names, String string)
224224
*/
225225
public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
226226
throws JSONException {
227-
if (names == null || names.length() == 0) {
227+
if (names == null || names.isEmpty()) {
228228
return null;
229229
}
230230
JSONArray ja = new JSONArray();
@@ -235,7 +235,7 @@ public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
235235
}
236236
ja.put(jo);
237237
}
238-
if (ja.length() == 0) {
238+
if (ja.isEmpty()) {
239239
return null;
240240
}
241241
return ja;
@@ -272,7 +272,7 @@ public static String toString(JSONArray ja) throws JSONException {
272272
*/
273273
public static String toString(JSONArray names, JSONArray ja)
274274
throws JSONException {
275-
if (names == null || names.length() == 0) {
275+
if (names == null || names.isEmpty()) {
276276
return null;
277277
}
278278
StringBuffer sb = new StringBuffer();

JSONArray.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ public boolean similar(Object other) {
13521352
* If any of the names are null.
13531353
*/
13541354
public JSONObject toJSONObject(JSONArray names) throws JSONException {
1355-
if (names == null || names.length() == 0 || this.length() == 0) {
1355+
if (names == null || names.isEmpty() || this.isEmpty()) {
13561356
return null;
13571357
}
13581358
JSONObject jo = new JSONObject(names.length());
@@ -1528,4 +1528,14 @@ public List<Object> toList() {
15281528
}
15291529
return results;
15301530
}
1531+
1532+
/**
1533+
* Check if JSONArray is empty.
1534+
*
1535+
* @return true if JSONArray is empty, otherwise false.
1536+
*/
1537+
public boolean isEmpty() {
1538+
return myArrayList.isEmpty();
1539+
}
1540+
15311541
}

JSONML.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private static Object parse(
178178
newjo.accumulate(attribute, "");
179179
}
180180
}
181-
if (arrayForm && newjo.length() > 0) {
181+
if (arrayForm && !newjo.isEmpty()) {
182182
newja.put(newjo);
183183
}
184184

@@ -208,7 +208,7 @@ private static Object parse(
208208
"' and '" + closeTag + "'");
209209
}
210210
tagName = null;
211-
if (!arrayForm && newja.length() > 0) {
211+
if (!arrayForm && !newja.isEmpty()) {
212212
newjo.put("childNodes", newja);
213213
}
214214
if (ja == null) {

JSONObject.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,10 @@ public long getLong(String key) throws JSONException {
810810
* @return An array of field names, or null if there are no names.
811811
*/
812812
public static String[] getNames(JSONObject jo) {
813-
int length = jo.length();
814-
if (length == 0) {
813+
if (jo.isEmpty()) {
815814
return null;
816815
}
817-
return jo.keySet().toArray(new String[length]);
816+
return jo.keySet().toArray(new String[jo.length()]);
818817
}
819818

820819
/**
@@ -972,15 +971,6 @@ public boolean isEmpty() {
972971
return map.isEmpty();
973972
}
974973

975-
/**
976-
* Check if JSONObject is not empty.
977-
*
978-
* @return true if JSONObject is not empty, otherwise false.
979-
*/
980-
public boolean isNotEmpty() {
981-
return !map.isEmpty();
982-
}
983-
984974
/**
985975
* Produce a JSONArray containing the names of the elements of this
986976
* JSONObject.
@@ -1966,7 +1956,7 @@ public static String quote(String string) {
19661956
}
19671957

19681958
public static Writer quote(String string, Writer w) throws IOException {
1969-
if (string == null || string.length() == 0) {
1959+
if (string == null || string.isEmpty()) {
19701960
w.write("\"\"");
19711961
return w;
19721962
}
@@ -2245,7 +2235,7 @@ public static void testValidity(Object o) throws JSONException {
22452235
* If any of the values are non-finite numbers.
22462236
*/
22472237
public JSONArray toJSONArray(JSONArray names) throws JSONException {
2248-
if (names == null || names.length() == 0) {
2238+
if (names == null || names.isEmpty()) {
22492239
return null;
22502240
}
22512241
JSONArray ja = new JSONArray();

XML.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, bool
277277
if ("CDATA".equals(token)) {
278278
if (x.next() == '[') {
279279
string = x.nextCDATA();
280-
if (string.length() > 0) {
280+
if (!string.isEmpty()) {
281281
context.accumulate("content", string);
282282
}
283283
return false;
@@ -353,7 +353,7 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, bool
353353
if (x.nextToken() != GT) {
354354
throw x.syntaxError("Misshaped tag");
355355
}
356-
if (jsonobject.length() > 0) {
356+
if (!jsonobject.isEmpty()) {
357357
context.accumulate(tagName, jsonobject);
358358
} else {
359359
context.accumulate(tagName, "");
@@ -371,15 +371,15 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, bool
371371
return false;
372372
} else if (token instanceof String) {
373373
string = (String) token;
374-
if (string.length() > 0) {
374+
if (!string.isEmpty()) {
375375
jsonobject.accumulate("content",
376376
keepStrings ? string : stringToValue(string));
377377
}
378378

379379
} else if (token == LT) {
380380
// Nested element
381381
if (parse(x, jsonobject, tagName,keepStrings)) {
382-
if (jsonobject.length() == 0) {
382+
if (jsonobject.isEmpty()) {
383383
context.accumulate(tagName, "");
384384
} else if (jsonobject.length() == 1
385385
&& jsonobject.opt("content") != null) {
@@ -676,7 +676,7 @@ public static String toString(final Object object, final String tagName)
676676

677677
string = (object == null) ? "null" : escape(object.toString());
678678
return (tagName == null) ? "\"" + string + "\""
679-
: (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
679+
: (string.isEmpty()) ? "<" + tagName + "/>" : "<" + tagName
680680
+ ">" + string + "</" + tagName + ">";
681681

682682
}

0 commit comments

Comments
 (0)