Renamed object methods from ...Obj to ...Object.

Added object method for optDoubleObject (returns Double vice double).
Added similar methods in JSONArray.
Added test methods.
This commit is contained in:
dburbrid
2023-06-29 09:39:34 +01:00
parent 8ce0019a5d
commit 4951ec48c8
5 changed files with 467 additions and 2 deletions

View File

@@ -599,6 +599,38 @@ public class JSONArray implements Iterable<Object> {
}
}
/**
* Get the optional Boolean object associated with an index. It returns false
* if there is no value at that index, or if the value is not Boolean.TRUE
* or the String "true".
*
* @param index
* The index must be between 0 and length() - 1.
* @return The truth.
*/
public Boolean optBooleanObject(int index) {
return this.optBooleanObject(index, false);
}
/**
* Get the optional Boolean object associated with an index. It returns the
* defaultValue if there is no value at that index or if it is not a Boolean
* or the String "true" or "false" (case insensitive).
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* A boolean default.
* @return The truth.
*/
public Boolean optBooleanObject(int index, Boolean defaultValue) {
try {
return this.getBoolean(index);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get the optional double value associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
@@ -635,6 +667,42 @@ public class JSONArray implements Iterable<Object> {
return doubleValue;
}
/**
* Get the optional Double object associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The object.
*/
public Double optDoubleObject(int index) {
return this.optDoubleObject(index, Double.NaN);
}
/**
* Get the optional double value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default object.
* @return The object.
*/
public Double optDoubleObject(int index, Double defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
final Double doubleValue = val.doubleValue();
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
// return defaultValue;
// }
return doubleValue;
}
/**
* Get the optional float value associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
@@ -671,6 +739,42 @@ public class JSONArray implements Iterable<Object> {
return floatValue;
}
/**
* Get the optional Float object associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The object.
*/
public Float optFloatObject(int index) {
return this.optFloatObject(index, Float.NaN);
}
/**
* Get the optional Float object associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default object.
* @return The object.
*/
public Float optFloatObject(int index, Float defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
final Float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return floatValue;
// }
return floatValue;
}
/**
* Get the optional int value associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
@@ -703,6 +807,38 @@ public class JSONArray implements Iterable<Object> {
return val.intValue();
}
/**
* Get the optional Integer object associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The object.
*/
public Integer optIntegerObject(int index) {
return this.optIntegerObject(index, 0);
}
/**
* Get the optional Integer object associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default object.
* @return The object.
*/
public Integer optIntegerObject(int index, Integer defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.intValue();
}
/**
* Get the enum value associated with a key.
*
@@ -846,6 +982,38 @@ public class JSONArray implements Iterable<Object> {
return val.longValue();
}
/**
* Get the optional Long object associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The object.
*/
public Long optLongObject(int index) {
return this.optLongObject(index, 0L);
}
/**
* Get the optional Long object associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default object.
* @return The object.
*/
public Long optLongObject(int index, Long defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.longValue();
}
/**
* Get an optional {@link Number} value associated with a key, or <code>null</code>
* if there is no such key or if the value is not a number. If the value is a string,