Skip to content

Commit a835bf3

Browse files
author
alwinsanil
committed
Complex conditional: JSONArray.getBoolean code smell fix
1 parent b71146b commit a835bf3

1 file changed

Lines changed: 30 additions & 11 deletions

File tree

src/main/java/org/json/JSONArray.java

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,27 +325,46 @@ public Object get(int index) throws JSONException {
325325
* Get the boolean value associated with an index. The string values "true"
326326
* and "false" are converted to boolean.
327327
*
328-
* @param index
329-
* The index must be between 0 and length() - 1.
328+
* @param index The index must be between 0 and length() - 1.
330329
* @return The truth.
331-
* @throws JSONException
332-
* If there is no value for the index or if the value is not
333-
* convertible to boolean.
330+
* @throws JSONException If there is no value for the index or if the value is not
331+
* convertible to boolean.
334332
*/
335333
public boolean getBoolean(int index) throws JSONException {
336334
Object object = this.get(index);
337-
if (object.equals(Boolean.FALSE)
338-
|| (object instanceof String && ((String) object)
339-
.equalsIgnoreCase("false"))) {
335+
336+
if (isFalse(object)) {
340337
return false;
341-
} else if (object.equals(Boolean.TRUE)
342-
|| (object instanceof String && ((String) object)
343-
.equalsIgnoreCase("true"))) {
338+
}
339+
if (isTrue(object)) {
344340
return true;
345341
}
342+
346343
throw wrongValueFormatException(index, "boolean", object, null);
347344
}
348345

346+
/**
347+
* Checks if the object represents a false value
348+
* @param object The object to check
349+
* @return true if the object represents false
350+
*/
351+
private boolean isFalse(Object object) {
352+
if (object.equals(Boolean.FALSE)) return true;
353+
if (!(object instanceof String)) return false;
354+
return ((String) object).equalsIgnoreCase("false");
355+
}
356+
357+
/**
358+
* Checks if the object represents a true value
359+
* @param object The object to check
360+
* @return true if the object represents true
361+
*/
362+
private boolean isTrue(Object object) {
363+
if (object.equals(Boolean.TRUE)) return true;
364+
if (!(object instanceof String)) return false;
365+
return ((String) object).equalsIgnoreCase("true");
366+
}
367+
349368
/**
350369
* Get the double value associated with an index.
351370
*

0 commit comments

Comments
 (0)