18
18
19
19
20
20
import java .util .Optional ;
21
+ import java .util .function .Consumer ;
21
22
22
23
import org .springframework .lang .Nullable ;
23
24
import org .springframework .util .ObjectUtils ;
@@ -69,6 +70,14 @@ public boolean isPresent() {
69
70
return (this .value != null );
70
71
}
71
72
73
+ /**
74
+ * Return {@code true} if the input value was present in the input but the value was {@code null},
75
+ * and {@code false} otherwise.
76
+ */
77
+ public boolean isEmpty () {
78
+ return !this .omitted ;
79
+ }
80
+
72
81
/**
73
82
* Return {@code true} if the input value was omitted altogether from the
74
83
* input, and {@code false} if it was provided, but possibly set to the
@@ -93,6 +102,33 @@ public Optional<T> asOptional() {
93
102
return Optional .ofNullable (this .value );
94
103
}
95
104
105
+ /**
106
+ * If a value is present, performs the given action with the value, otherwise does nothing.
107
+ * @param action the action to be performed, if a value is present
108
+ * @throws NullPointerException if value is present and the given action is {@code null}
109
+ */
110
+ public void ifPresent (Consumer <? super T > action ) {
111
+ if (this .value != null ) {
112
+ action .accept (value );
113
+ }
114
+ }
115
+
116
+ /**
117
+ * If the value is present, performs the given action with the value, otherwise if the value is empty
118
+ * performs the given empty-based action. If the value is omitted, do nothing.
119
+ * @param action the action to be performed, if the value is present
120
+ * @param emptyAction the empty-based action to be performed, if the value is empty
121
+ * @throws NullPointerException if a value is present and the given action is {@code null}, or no value is present
122
+ * and the given empty-based action is {@code null}
123
+ */
124
+ public void ifPresentOrEmpty (Consumer <? super T > action , Runnable emptyAction ) {
125
+ if (value != null ) {
126
+ action .accept (value );
127
+ } else if (!omitted ) {
128
+ emptyAction .run ();
129
+ }
130
+ }
131
+
96
132
@ Override
97
133
public boolean equals (Object other ) {
98
134
// This covers OMITTED constant
0 commit comments