Skip to content

Commit 015058b

Browse files
James Bodkinbclozel
James Bodkin
authored andcommitted
Add isEmpty and ifPresent methods in ArgumentValue
Closes gh-1172 Signed-off-by: James Bodkin <[email protected]> [[email protected]: add tests and reduce scope] Signed-off-by: Brian Clozel <[email protected]>
1 parent 9180a7e commit 015058b

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,10 @@
1818

1919

2020
import java.util.Optional;
21+
import java.util.function.Consumer;
2122

2223
import org.springframework.lang.Nullable;
24+
import org.springframework.util.Assert;
2325
import org.springframework.util.ObjectUtils;
2426

2527
/**
@@ -69,6 +71,15 @@ public boolean isPresent() {
6971
return (this.value != null);
7072
}
7173

74+
/**
75+
* Return {@code true} if the input value was present in the input but the value was {@code null},
76+
* and {@code false} otherwise.
77+
* @since 1.4.0
78+
*/
79+
public boolean isEmpty() {
80+
return !this.omitted && this.value == null;
81+
}
82+
7283
/**
7384
* Return {@code true} if the input value was omitted altogether from the
7485
* input, and {@code false} if it was provided, but possibly set to the
@@ -93,6 +104,17 @@ public Optional<T> asOptional() {
93104
return Optional.ofNullable(this.value);
94105
}
95106

107+
/**
108+
* If a value is present, performs the given action with the value, otherwise does nothing.
109+
* @param action the action to be performed, if a value is present
110+
*/
111+
public void ifPresent(Consumer<? super T> action) {
112+
Assert.notNull(action, "Action is required");
113+
if (this.value != null) {
114+
action.accept(this.value);
115+
}
116+
}
117+
96118
@Override
97119
public boolean equals(Object other) {
98120
// This covers OMITTED constant
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2020-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.graphql.data;
18+
19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Tests for {@link ArgumentValue}.
27+
* @author Brian Clozel
28+
*/
29+
class ArgumentValueTests {
30+
31+
@Test
32+
void existingValueShouldBePresent() {
33+
ArgumentValue<String> message = ArgumentValue.ofNullable("hello");
34+
assertThat(message.isOmitted()).isFalse();
35+
assertThat(message.isPresent()).isTrue();
36+
assertThat(message.isEmpty()).isFalse();
37+
}
38+
39+
@Test
40+
void nullValueShouldBePresent() {
41+
ArgumentValue<String> message = ArgumentValue.ofNullable(null);
42+
assertThat(message.isOmitted()).isFalse();
43+
assertThat(message.isPresent()).isFalse();
44+
assertThat(message.isEmpty()).isTrue();
45+
}
46+
47+
@Test
48+
void noValueShouldBeOmitted() {
49+
ArgumentValue<String> message = ArgumentValue.omitted();
50+
assertThat(message.isOmitted()).isTrue();
51+
assertThat(message.isPresent()).isFalse();
52+
assertThat(message.isEmpty()).isFalse();
53+
}
54+
55+
@Test
56+
void asOptionalShouldMapOmitted() {
57+
assertThat(ArgumentValue.omitted().asOptional()).isEmpty();
58+
assertThat(ArgumentValue.ofNullable(null).asOptional()).isEmpty();
59+
assertThat(ArgumentValue.ofNullable("hello").asOptional()).isPresent();
60+
}
61+
62+
@Test
63+
void ifPresentShouldExecuteWhenValue() {
64+
AtomicBoolean called = new AtomicBoolean();
65+
ArgumentValue.ofNullable("hello").ifPresent(value -> called.set(true));
66+
assertThat(called.get()).isTrue();
67+
}
68+
69+
@Test
70+
void ifPresentShouldSkipWhenNull() {
71+
AtomicBoolean called = new AtomicBoolean();
72+
ArgumentValue.ofNullable(null).ifPresent(value -> called.set(true));
73+
assertThat(called.get()).isFalse();
74+
}
75+
76+
@Test
77+
78+
void ifPresentShouldSkipWhenOmitted() {
79+
AtomicBoolean called = new AtomicBoolean();
80+
ArgumentValue.omitted().ifPresent(value -> called.set(true));
81+
assertThat(called.get()).isFalse();
82+
}
83+
84+
}

0 commit comments

Comments
 (0)