Skip to content

Commit eb5a83a

Browse files
James Bodkinbclozel
James Bodkin
authored andcommitted
Reduce allocations for empty ArgumentValue
Closes gh-1175 Signed-off-by: James Bodkin <[email protected]> [[email protected]: apply code conventions] Signed-off-by: Brian Clozel <[email protected]>
1 parent e2f954a commit eb5a83a

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

Diff for: spring-graphql/src/main/java/org/springframework/graphql/data/ArgumentValue.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
*/
5050
public final class ArgumentValue<T> {
5151

52+
private static final ArgumentValue<?> EMPTY = new ArgumentValue<>(null, false);
53+
5254
private static final ArgumentValue<?> OMITTED = new ArgumentValue<>(null, true);
5355

5456

@@ -118,7 +120,7 @@ public void ifPresent(Consumer<? super T> action) {
118120

119121
@Override
120122
public boolean equals(Object other) {
121-
// This covers OMITTED constant
123+
// This covers EMPTY and OMITTED constant
122124
if (this == other) {
123125
return true;
124126
}
@@ -142,8 +144,9 @@ public int hashCode() {
142144
* @param <T> the type of value
143145
* @param value the value to hold in the instance
144146
*/
147+
@SuppressWarnings("unchecked")
145148
public static <T> ArgumentValue<T> ofNullable(@Nullable T value) {
146-
return new ArgumentValue<>(value, false);
149+
return (value != null) ? new ArgumentValue<>(value, false) : (ArgumentValue<T>) EMPTY;
147150
}
148151

149152
/**

Diff for: spring-graphql/src/test/java/org/springframework/graphql/data/ArgumentValueTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ void ifPresentShouldSkipWhenNull() {
7474
}
7575

7676
@Test
77-
7877
void ifPresentShouldSkipWhenOmitted() {
7978
AtomicBoolean called = new AtomicBoolean();
8079
ArgumentValue.omitted().ifPresent(value -> called.set(true));

0 commit comments

Comments
 (0)