Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various optimizations for nullity checks #362

Closed
Katsute opened this issue Feb 2, 2023 · 2 comments · Fixed by #363
Closed

Various optimizations for nullity checks #362

Katsute opened this issue Feb 2, 2023 · 2 comments · Fixed by #363
Assignees

Comments

@Katsute
Copy link
Member

Katsute commented Feb 2, 2023

public final int getInt(final String key){
final Object value = map.get(key);
return value instanceof String ? Integer.parseInt((String) value) : ((Number) value).intValue();
}
public final double getDouble(final String key){
final Object value = map.get(key);
return value instanceof String ? Double.parseDouble((String) value) : ((Number) value).doubleValue();
}
public final float getFloat(final String key){
final Object value = map.get(key);
return value instanceof String ? Float.parseFloat((String) value) : ((Number) value).floatValue();
}
public final long getLong(final String key){
final Object value = map.get(key);
return value instanceof String ? Long.parseLong((String) value) : ((Number) value).longValue();
}
public final boolean getBoolean(final String key){
final Object value = map.get(key);
return value instanceof String ? Boolean.parseBoolean((String) value) : (boolean) value;
}

Return null instead of throwing a NPE.

Use .valueOf rather than .parse, try catch on NFE.

public final JsonObject getJsonObject(final String key){
return (JsonObject) map.get(key);
}

Return an empty JsonObject if null, this will protect against chained nulls.

public final JsonObject[] getJsonArray(final String key){
final List<?> list = (List<?>) map.get(key);
final List<JsonObject> arr = new ArrayList<>();
for(final Object o : list)
arr.add((JsonObject) o);
return arr.toArray(new JsonObject[0]);
}

Return an empty array if null, this will protect against null loops.

Various as methods need to be rewritten to return null if null object.

These changes should remove the need for requireNonNull.

@Katsute Katsute self-assigned this Feb 2, 2023
@Katsute

This comment has been minimized.

@ghost

This comment has been minimized.

@ghost ghost locked and limited conversation to collaborators Feb 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant