diff --git a/.gitignore b/.gitignore index b0ab204..f64e2c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ bin/ target/ .history/ + +# IntelliJ IDEA files +.idea/ +*.iml \ No newline at end of file diff --git a/com.eclipsesource.json/pom.xml b/com.eclipsesource.json/pom.xml index f682ba4..e527b39 100644 --- a/com.eclipsesource.json/pom.xml +++ b/com.eclipsesource.json/pom.xml @@ -32,13 +32,13 @@ org.codehaus.mojo.signature - java15 + java18 1.0 - ensure-java-1.5-class-library + ensure-java-1.8-class-library test check @@ -82,7 +82,7 @@ ${project.artifactId} ${project.name} ${project.version} - J2SE-1.5 + JavaSE-1.8 com.eclipsesource.json;version="${project.version}" diff --git a/com.eclipsesource.json/src/main/java/com/eclipsesource/json/JsonObject.java b/com.eclipsesource.json/src/main/java/com/eclipsesource/json/JsonObject.java index 37908f6..ce1dc05 100644 --- a/com.eclipsesource.json/src/main/java/com/eclipsesource/json/JsonObject.java +++ b/com.eclipsesource.json/src/main/java/com/eclipsesource/json/JsonObject.java @@ -30,6 +30,10 @@ import java.util.List; import com.eclipsesource.json.JsonObject.Member; +import java.util.Optional; +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.OptionalLong; /** @@ -565,6 +569,8 @@ public JsonObject merge(JsonObject object) { * the name of the member whose value is to be returned * @return the value of the last member with the specified name, or null if this * object does not contain a member with that name + * @see #getOptional(java.lang.String) + * equivalent using an Optional */ public JsonValue get(String name) { if (name == null) { @@ -573,6 +579,21 @@ public JsonValue get(String name) { int index = indexOf(name); return index != -1 ? values.get(index) : null; } + + /** + * Returns the value of the member with the specified name in this object. If this object contains + * multiple members with the given name, this method will return the last one. + * + * @param name + * the name of the member whose value is to be returned + * @return the value of the last member with the specified name, or an empty optional if this + * object does not contain a member with that name + * @see #get(java.lang.String) + * equivalent without Optional instantiation + */ + public Optional getOptional(String name) { + return Optional.ofNullable(get(name)); + } /** * Returns the int value of the member with the specified name in this object. If @@ -587,11 +608,32 @@ public JsonValue get(String name) { * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name + * @see #getInt(java.lang.String) + * equivalent using an OptionalInt */ public int getInt(String name, int defaultValue) { JsonValue value = get(name); return value != null ? value.asInt() : defaultValue; } + + /** + * Returns the int value of the member with the specified name in this object. If + * this object does not contain a member with this name, an empty Optional is returned. If + * this object contains multiple members with the given name, the last one will be picked. If this + * member's value does not represent a JSON number or if it cannot be interpreted as Java + * int, an exception is thrown. + * + * @param name + * the name of the member whose value is to be returned + * @return the value of the last member with the specified name, or an empty Optional if + * this object does not contain a member with that name + * @see #getInt(java.lang.String, int) + * equivalent without Optional instantiation + */ + public OptionalInt getInt(String name) { + JsonValue value = get(name); + return value != null ? OptionalInt.of(value.asInt()) : OptionalInt.empty(); + } /** * Returns the long value of the member with the specified name in this object. If @@ -606,12 +648,33 @@ public int getInt(String name, int defaultValue) { * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name + * @see #getLong(java.lang.String) + * equivalent using an OptionalLong */ public long getLong(String name, long defaultValue) { JsonValue value = get(name); return value != null ? value.asLong() : defaultValue; } + /** + * Returns the long value of the member with the specified name in this object. If + * this object does not contain a member with this name, an empty Optional is returned. If + * this object contains multiple members with the given name, the last one will be picked. If this + * member's value does not represent a JSON number or if it cannot be interpreted as Java + * long, an exception is thrown. + * + * @param name + * the name of the member whose value is to be returned + * @return the value of the last member with the specified name, or an empty Optional if + * this object does not contain a member with that name + * @see #getLong(java.lang.String, long) + * equivalent without Optional instantiation + */ + public OptionalLong getLong(String name) { + JsonValue value = get(name); + return value != null ? OptionalLong.of(value.asLong()) : OptionalLong.empty(); + } + /** * Returns the float value of the member with the specified name in this object. If * this object does not contain a member with this name, the given default value is returned. If @@ -625,11 +688,32 @@ public long getLong(String name, long defaultValue) { * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name + * @see #getFloat(java.lang.String) + * equivalent using an Optional */ public float getFloat(String name, float defaultValue) { JsonValue value = get(name); return value != null ? value.asFloat() : defaultValue; } + + /** + * Returns the float value of the member with the specified name in this object. If + * this object does not contain a member with this name, an empty Optional is returned. If + * this object contains multiple members with the given name, the last one will be picked. If this + * member's value does not represent a JSON number or if it cannot be interpreted as Java + * float, an exception is thrown. + * + * @param name + * the name of the member whose value is to be returned + * @return the value of the last member with the specified name, or an empty Optional if + * this object does not contain a member with that name + * @see #getFloat(java.lang.String, float) + * equivalent without Optional instantiation + */ + public Optional getFloat(String name) { + JsonValue value = get(name); + return value != null ? Optional.of(value.asFloat()) : Optional.empty(); + } /** * Returns the double value of the member with the specified name in this object. If @@ -644,11 +728,32 @@ public float getFloat(String name, float defaultValue) { * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name + * @see #getDouble(java.lang.String) + * equivalent using an OptionalDouble */ public double getDouble(String name, double defaultValue) { JsonValue value = get(name); return value != null ? value.asDouble() : defaultValue; } + + /** + * Returns the double value of the member with the specified name in this object. If + * this object does not contain a member with this name, an empty Optional is returned. If + * this object contains multiple members with the given name, the last one will be picked. If this + * member's value does not represent a JSON number or if it cannot be interpreted as Java + * double, an exception is thrown. + * + * @param name + * the name of the member whose value is to be returned + * @return the value of the last member with the specified name, or an empty Optional if + * this object does not contain a member with that name + * @see #getDouble(java.lang.String, double) + * equivalent without Optional instantiation + */ + public OptionalDouble getDouble(String name) { + JsonValue value = get(name); + return value != null ? OptionalDouble.of(value.asDouble()) : OptionalDouble.empty(); + } /** * Returns the boolean value of the member with the specified name in this object. If @@ -663,11 +768,32 @@ public double getDouble(String name, double defaultValue) { * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name + * @see #getBoolean(java.lang.String) + * equivalent using an Optional */ public boolean getBoolean(String name, boolean defaultValue) { JsonValue value = get(name); return value != null ? value.asBoolean() : defaultValue; } + + /** + * Returns the boolean value of the member with the specified name in this object. If + * this object does not contain a member with this name, an empty Optional is returned. If + * this object contains multiple members with the given name, the last one will be picked. If this + * member's value does not represent a JSON number or if it cannot be interpreted as Java + * boolean, an exception is thrown. + * + * @param name + * the name of the member whose value is to be returned + * @return the value of the last member with the specified name, or an empty Optional if + * this object does not contain a member with that name + * @see #getBoolean(java.lang.String, boolean) + * equivalent without Optional instantiation + */ + public Optional getBoolean(String name) { + JsonValue value = get(name); + return value != null ? Optional.of(value.asBoolean()) : Optional.empty(); + } /** * Returns the String value of the member with the specified name in this object. If @@ -681,12 +807,33 @@ public boolean getBoolean(String name, boolean defaultValue) { * the value to be returned if the requested member is missing * @return the value of the last member with the specified name, or the given default value if * this object does not contain a member with that name + * @see #getString(java.lang.String) + * equivalent using an Optional */ public String getString(String name, String defaultValue) { JsonValue value = get(name); return value != null ? value.asString() : defaultValue; } + /** + * Returns the String value of the member with the specified name in this object. If + * this object does not contain a member with this name, an empty Optional is returned. If + * this object contains multiple members with the given name, the last one will be picked. If this + * member's value does not represent a JSON number or if it cannot be interpreted as Java + * String, an exception is thrown. + * + * @param name + * the name of the member whose value is to be returned + * @return the value of the last member with the specified name, or an empty Optional if + * this object does not contain a member with that name + * @see #getString(java.lang.String, String) + * equivalent without Optional instantiation + */ + public Optional getString(String name) { + JsonValue value = get(name); + return value != null ? Optional.of(value.asString()) : Optional.empty(); + } + /** * Returns the number of members (name/value pairs) in this object. *