Skip to content

Add additional methods to interact with an ArgumentValue #1172

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


import java.util.Optional;
import java.util.function.Consumer;

import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -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
Expand All @@ -93,6 +102,33 @@ public Optional<T> 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<? super T> 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<? super T> action, Runnable emptyAction) {
if (value != null) {
action.accept(value);
} else if (!omitted) {
emptyAction.run();
}
}

@Override
public boolean equals(Object other) {
// This covers OMITTED constant
Expand Down