Reduce allocations for empty ArgumentValue
Closes gh-1175 Signed-off-by: James Bodkin <james.bodkin@amphora.net> [brian.clozel@broadcom.com: apply code conventions] Signed-off-by: Brian Clozel <brian.clozel@broadcom.com>
This commit is contained in:
committed by
Brian Clozel
parent
e2f954a2cf
commit
eb5a83ab6a
@@ -49,6 +49,8 @@ import org.springframework.util.ObjectUtils;
|
||||
*/
|
||||
public final class ArgumentValue<T> {
|
||||
|
||||
private static final ArgumentValue<?> EMPTY = new ArgumentValue<>(null, false);
|
||||
|
||||
private static final ArgumentValue<?> OMITTED = new ArgumentValue<>(null, true);
|
||||
|
||||
|
||||
@@ -118,7 +120,7 @@ public final class ArgumentValue<T> {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
// This covers OMITTED constant
|
||||
// This covers EMPTY and OMITTED constant
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
@@ -142,8 +144,9 @@ public final class ArgumentValue<T> {
|
||||
* @param <T> the type of value
|
||||
* @param value the value to hold in the instance
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> ArgumentValue<T> ofNullable(@Nullable T value) {
|
||||
return new ArgumentValue<>(value, false);
|
||||
return (value != null) ? new ArgumentValue<>(value, false) : (ArgumentValue<T>) EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,7 +74,6 @@ class ArgumentValueTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
void ifPresentShouldSkipWhenOmitted() {
|
||||
AtomicBoolean called = new AtomicBoolean();
|
||||
ArgumentValue.omitted().ifPresent(value -> called.set(true));
|
||||
|
||||
Reference in New Issue
Block a user