File tree Expand file tree Collapse file tree
main/java/org/openapitools/jackson/nullable
test/java/org/openapitools/jackson/nullable Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import java .io .Serializable ;
44import java .util .NoSuchElementException ;
5+ import java .util .function .Consumer ;
56
67public class JsonNullable <T > implements Serializable {
78
@@ -68,6 +69,20 @@ public boolean isPresent() {
6869 return isPresent ;
6970 }
7071
72+ /**
73+ * If a value is present, performs the given action with the value,
74+ * otherwise does nothing.
75+ *
76+ * @param action the action to be performed, if a value is present
77+ */
78+ public void ifPresent (
79+ Consumer <? super T > action ) {
80+
81+ if (this .isPresent ) {
82+ action .accept (value );
83+ }
84+ }
85+
7186 @ Override
7287 public boolean equals (Object obj ) {
7388 if (this == obj ) {
Original file line number Diff line number Diff line change 11package org .openapitools .jackson .nullable ;
22
33import com .fasterxml .jackson .core .Version ;
4+ import com .fasterxml .jackson .core .json .PackageVersion ;
45import com .fasterxml .jackson .databind .Module ;
56
67public class JsonNullableModule extends Module {
Original file line number Diff line number Diff line change @@ -57,6 +57,30 @@ public void orElseMissing() {
5757 assertEquals ("world" , test .orElse ("world" ));
5858 }
5959
60+ @ Test
61+ public void ifPresentWithValueNotPresent () {
62+ JsonNullable <String > test = JsonNullable .undefined ();
63+ test .ifPresent (string -> {
64+ throw new RuntimeException ();
65+ });
66+ }
67+
68+ @ Test (expected = RuntimeException .class )
69+ public void ifPresentWithNullValuePresent () {
70+ JsonNullable <String > test = JsonNullable .of (null );
71+ test .ifPresent (string -> {
72+ throw new RuntimeException ();
73+ });
74+ }
75+
76+ @ Test (expected = RuntimeException .class )
77+ public void ifPresentWithNonNullValuePresent () {
78+ JsonNullable <String > test = JsonNullable .of ("test" );
79+ test .ifPresent (string -> {
80+ throw new RuntimeException ();
81+ });
82+ }
83+
6084 @ Test
6185 public void serializeNonBeanProperty () throws JsonProcessingException {
6286 assertEquals ("null" , mapper .writeValueAsString (JsonNullable .of (null )));
You can’t perform that action at this time.
0 commit comments