1818
1919
2020import java .util .Optional ;
21+ import java .util .function .Consumer ;
2122
2223import org .springframework .lang .Nullable ;
2324import org .springframework .util .ObjectUtils ;
@@ -69,6 +70,14 @@ public boolean isPresent() {
6970 return (this .value != null );
7071 }
7172
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+
7281 /**
7382 * Return {@code true} if the input value was omitted altogether from the
7483 * input, and {@code false} if it was provided, but possibly set to the
@@ -93,6 +102,33 @@ public Optional<T> asOptional() {
93102 return Optional .ofNullable (this .value );
94103 }
95104
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+
96132 @ Override
97133 public boolean equals (Object other ) {
98134 // This covers OMITTED constant
0 commit comments