2525import java .util .function .Supplier ;
2626
2727public class ContentAsUtil {
28+ private static Try <ContentTypes > getNullContentType (){
29+ return new Try <>(ContentTypes .newBuilder ().
30+ setContentAsNull (ContentTypes .NullValue .newBuilder ()
31+ .getDefaultInstanceForType ()).build ()
32+ );
33+ }
34+
2835 public static Try <ContentTypes > contentType (ContentAs contentAs ,
2936 Supplier <byte []> asByteArray ,
3037 Supplier <String > asString ,
@@ -35,32 +42,46 @@ public static Try<ContentTypes> contentType(ContentAs contentAs,
3542 Supplier <Double > asDouble ) {
3643 try {
3744 if (contentAs .hasAsByteArray ()) {
45+ byte [] byteArray = asByteArray .get ();
46+ if (byteArray == null ) return getNullContentType ();
3847 return new Try <>(ContentTypes .newBuilder ()
39- .setContentAsBytes (ByteString .copyFrom (asByteArray . get () ))
48+ .setContentAsBytes (ByteString .copyFrom (byteArray ))
4049 .build ());
4150 } else if (contentAs .hasAsString ()) {
51+ String string = asString .get ();
52+ if (string == null ) return getNullContentType ();
4253 return new Try <>(ContentTypes .newBuilder ()
43- .setContentAsString (asString . get () )
54+ .setContentAsString (string )
4455 .build ());
4556 } else if (contentAs .hasAsJsonObject ()) {
57+ JsonObject jsonObject = asJsonObject .get ();
58+ if (jsonObject == null ) return getNullContentType ();
4659 return new Try <>(ContentTypes .newBuilder ()
47- .setContentAsBytes (ByteString .copyFrom (asJsonObject . get () .toBytes ()))
60+ .setContentAsBytes (ByteString .copyFrom (jsonObject .toBytes ()))
4861 .build ());
4962 } else if (contentAs .hasAsJsonArray ()) {
63+ JsonArray jsonArray = asJsonArray .get ();
64+ if (jsonArray == null ) return getNullContentType ();
5065 return new Try <>(ContentTypes .newBuilder ()
51- .setContentAsBytes (ByteString .copyFrom (asJsonArray . get () .toBytes ()))
66+ .setContentAsBytes (ByteString .copyFrom (jsonArray .toBytes ()))
5267 .build ());
5368 } else if (contentAs .getAsBoolean ()) {
69+ Boolean bool = asBoolean .get ();
70+ if (bool == null ) return getNullContentType ();
5471 return new Try <>(ContentTypes .newBuilder ()
55- .setContentAsBool (asBoolean . get () )
72+ .setContentAsBool (bool )
5673 .build ());
5774 } else if (contentAs .hasAsInteger ()) {
75+ Integer integer = asInteger .get ();
76+ if (integer == null ) return getNullContentType ();
5877 return new Try <>(ContentTypes .newBuilder ()
59- .setContentAsInt64 (asInteger . get () )
78+ .setContentAsInt64 (integer )
6079 .build ());
6180 } else if (contentAs .hasAsFloatingPoint ()) {
81+ Double dbl = asDouble .get ();
82+ if (dbl == null ) return getNullContentType ();
6283 return new Try <>(ContentTypes .newBuilder ()
63- .setContentAsDouble (asDouble . get () )
84+ .setContentAsDouble (dbl )
6485 .build ());
6586 } else {
6687 throw new UnsupportedOperationException ("Java performer cannot handle contentAs " + contentAs .toString ());
@@ -81,31 +102,44 @@ public static Try<List<ContentTypes>> contentTypeList(ContentAs contentAs,
81102 try {
82103 if (contentAs .hasAsByteArray ()) {
83104 return new Try <>(asByteArray .get ().stream ()
84- .map (v -> ContentTypes .newBuilder ().setContentAsBytes (ByteString .copyFrom (v )).build ())
105+ .map (v -> v != null
106+ ? ContentTypes .newBuilder ().setContentAsBytes (ByteString .copyFrom (v )).build ()
107+ : getNullContentType ().value ())
85108 .toList ());
86109 } else if (contentAs .hasAsString ()) {
87110 return new Try <>(asString .get ().stream ()
88- .map (v -> ContentTypes .newBuilder ().setContentAsString (v ).build ())
89- .toList ());
111+ .map (v -> v != null
112+ ? ContentTypes .newBuilder ().setContentAsString (v ).build ()
113+ : getNullContentType ().value ()).toList ());
90114 } else if (contentAs .hasAsJsonObject ()) {
91115 return new Try <>(asJsonObject .get ().stream ()
92- .map (v -> ContentTypes .newBuilder ().setContentAsBytes (ByteString .copyFrom (v .toBytes ())).build ())
116+ .map (v -> v != null
117+ ? ContentTypes .newBuilder ().setContentAsBytes (ByteString .copyFrom (v .toBytes ())).build ()
118+ : getNullContentType ().value ())
93119 .toList ());
94120 } else if (contentAs .hasAsJsonArray ()) {
95121 return new Try <>(asJsonArray .get ().stream ()
96- .map (v -> ContentTypes .newBuilder ().setContentAsBytes (ByteString .copyFrom (v .toBytes ())).build ())
122+ .map (v -> v != null
123+ ? ContentTypes .newBuilder ().setContentAsBytes (ByteString .copyFrom (v .toBytes ())).build ()
124+ : getNullContentType ().value ())
97125 .toList ());
98126 } else if (contentAs .getAsBoolean ()) {
99127 return new Try <>(asBoolean .get ().stream ()
100- .map (v -> ContentTypes .newBuilder ().setContentAsBool (v ).build ())
128+ .map (v -> v != null
129+ ? ContentTypes .newBuilder ().setContentAsBool (v ).build ()
130+ : getNullContentType ().value ())
101131 .toList ());
102132 } else if (contentAs .hasAsInteger ()) {
103133 return new Try <>(asInteger .get ().stream ()
104- .map (v -> ContentTypes .newBuilder ().setContentAsInt64 (v ).build ())
134+ .map (v -> v != null
135+ ? ContentTypes .newBuilder ().setContentAsInt64 (v ).build ()
136+ : getNullContentType ().value ())
105137 .toList ());
106138 } else if (contentAs .hasAsFloatingPoint ()) {
107139 return new Try <>(asDouble .get ().stream ()
108- .map (v -> ContentTypes .newBuilder ().setContentAsDouble (v ).build ())
140+ .map (v -> v != null
141+ ?ContentTypes .newBuilder ().setContentAsDouble (v ).build ()
142+ : getNullContentType ().value ())
109143 .toList ());
110144 } else {
111145 throw new UnsupportedOperationException ("Java performer cannot handle contentAs " + contentAs .toString ());
0 commit comments