diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/ArgumentValue.java b/spring-graphql/src/main/java/org/springframework/graphql/data/ArgumentValue.java index b25f89c4..0eec07b8 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/ArgumentValue.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/ArgumentValue.java @@ -18,6 +18,7 @@ import java.util.Optional; +import java.util.function.Consumer; import org.springframework.lang.Nullable; import org.springframework.util.ObjectUtils; @@ -69,6 +70,14 @@ public boolean isPresent() { return (this.value != null); } + /** + * Return {@code true} if the input value was present in the input but the value was {@code null}, + * and {@code false} otherwise. + */ + public boolean isEmpty() { + return !this.omitted; + } + /** * Return {@code true} if the input value was omitted altogether from the * input, and {@code false} if it was provided, but possibly set to the @@ -93,6 +102,33 @@ public Optional asOptional() { return Optional.ofNullable(this.value); } + /** + * If a value is present, performs the given action with the value, otherwise does nothing. + * @param action the action to be performed, if a value is present + * @throws NullPointerException if value is present and the given action is {@code null} + */ + public void ifPresent(Consumer action) { + if (this.value != null) { + action.accept(value); + } + } + + /** + * If the value is present, performs the given action with the value, otherwise if the value is empty + * performs the given empty-based action. If the value is omitted, do nothing. + * @param action the action to be performed, if the value is present + * @param emptyAction the empty-based action to be performed, if the value is empty + * @throws NullPointerException if a value is present and the given action is {@code null}, or no value is present + * and the given empty-based action is {@code null} + */ + public void ifPresentOrEmpty(Consumer action, Runnable emptyAction) { + if (value != null) { + action.accept(value); + } else if (!omitted) { + emptyAction.run(); + } + } + @Override public boolean equals(Object other) { // This covers OMITTED constant