Skip to content

Reduce the number of memory allocations for empty argument values #1175

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
Show file tree
Hide file tree
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 @@ -49,6 +49,7 @@
*/
public final class ArgumentValue<T> {

private static final ArgumentValue<?> EMPTY = new ArgumentValue<>(null, false);
private static final ArgumentValue<?> OMITTED = new ArgumentValue<>(null, true);


Expand Down Expand Up @@ -118,7 +119,7 @@ public void ifPresent(Consumer<? super T> action) {

@Override
public boolean equals(Object other) {
// This covers OMITTED constant
// This covers EMPTY and OMITTED constant
if (this == other) {
return true;
}
Expand All @@ -142,8 +143,9 @@ public int hashCode() {
* @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 ? (ArgumentValue<T>) EMPTY : new ArgumentValue<>(value, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ void ifPresentShouldSkipWhenNull() {
}

@Test

void ifPresentShouldSkipWhenOmitted() {
AtomicBoolean called = new AtomicBoolean();
ArgumentValue.omitted().ifPresent(value -> called.set(true));
Expand Down