@@ -776,125 +776,80 @@ public static String getUrlFromNavigationEndpoint(
776776 *
777777 * @param textObject JSON object to get the text from
778778 * @param html whether to return HTML, by parsing the {@code navigationEndpoint}
779- * @return text in the JSON object or {@code null }
779+ * @return text in the JSON object as an {@link Optional }
780780 */
781- @ Nullable
782- public static String getTextFromObject (final JsonObject textObject , final boolean html ) {
783- if (isNullOrEmpty (textObject )) {
784- return null ;
785- }
786-
787- if (textObject .has ("simpleText" )) {
788- return textObject .getString ("simpleText" );
789- }
790-
791- final JsonArray runs = textObject .getArray ("runs" );
792- if (runs .isEmpty ()) {
793- return null ;
794- }
795-
796- final StringBuilder textBuilder = new StringBuilder ();
797- for (final Object o : runs ) {
798- final JsonObject run = (JsonObject ) o ;
799- String text = run .getString ("text" );
800-
801- if (html ) {
802- if (run .has ("navigationEndpoint" )) {
803- final String url = getUrlFromNavigationEndpoint (
804- run .getObject ("navigationEndpoint" ));
805- if (!isNullOrEmpty (url )) {
806- text = "<a href=\" " + Entities .escape (url ) + "\" >" + Entities .escape (text )
807- + "</a>" ;
781+ @ Nonnull
782+ public static Optional <String > getTextFromObject (@ Nonnull final JsonObject textObject ,
783+ final boolean html ) {
784+ return Optional .ofNullable (textObject .getString ("simpleText" ))
785+ .or (() -> {
786+ final var runs = textObject .getArray ("runs" );
787+ final String text = runs .streamAsJsonObjects ()
788+ .map (run -> {
789+ String textString = run .getString ("text" );
790+
791+ if (html ) {
792+ final String url = getUrlFromNavigationEndpoint (
793+ run .getObject ("navigationEndpoint" ));
794+ if (!isNullOrEmpty (url )) {
795+ textString = "<a href=\" " + Entities .escape (url ) + "\" >"
796+ + Entities .escape (textString ) + "</a>" ;
797+ }
798+
799+ if (run .getBoolean ("strikethrough" )) {
800+ textString = "<s>" + textString + "</s>" ;
801+ }
802+ if (run .getBoolean ("italics" )) {
803+ textString = "<i>" + textString + "</i>" ;
804+ }
805+ if (run .getBoolean ("bold" )) {
806+ textString = "<b>" + textString + "</b>" ;
807+ }
808+ }
809+
810+ return textString ;
811+ })
812+ .collect (Collectors .joining ());
813+
814+ final String string ;
815+ if (html ) {
816+ string = text .replaceAll ("\\ n" , "<br>" )
817+ .replaceAll (" {2}" , " " );
818+ } else {
819+ string = text ;
808820 }
809- }
810-
811- final boolean bold = run .has ("bold" )
812- && run .getBoolean ("bold" );
813- final boolean italic = run .has ("italics" )
814- && run .getBoolean ("italics" );
815- final boolean strikethrough = run .has ("strikethrough" )
816- && run .getBoolean ("strikethrough" );
817-
818- if (bold ) {
819- textBuilder .append ("<b>" );
820- }
821- if (italic ) {
822- textBuilder .append ("<i>" );
823- }
824- if (strikethrough ) {
825- textBuilder .append ("<s>" );
826- }
827-
828- textBuilder .append (text );
829-
830- if (strikethrough ) {
831- textBuilder .append ("</s>" );
832- }
833- if (italic ) {
834- textBuilder .append ("</i>" );
835- }
836- if (bold ) {
837- textBuilder .append ("</b>" );
838- }
839- } else {
840- textBuilder .append (text );
841- }
842- }
843-
844- String text = textBuilder .toString ();
845-
846- if (html ) {
847- text = text .replaceAll ("\\ n" , "<br>" );
848- text = text .replaceAll (" {2}" , " " );
849- }
850-
851- return text ;
821+ return Optional .of (string );
822+ })
823+ .filter (text -> !text .isEmpty ());
852824 }
853825
854826 @ Nonnull
855827 public static String getTextFromObjectOrThrow (final JsonObject textObject , final String error )
856828 throws ParsingException {
857- final String result = getTextFromObject (textObject );
858- if (result == null ) {
859- throw new ParsingException ("Could not extract text: " + error );
860- }
861- return result ;
829+ return getTextFromObject (textObject )
830+ .orElseThrow (() -> new ParsingException ("Could not extract text: " + error ));
862831 }
863832
864- @ Nullable
865- public static String getTextFromObject (final JsonObject textObject ) {
833+ @ Nonnull
834+ public static Optional < String > getTextFromObject (@ Nonnull final JsonObject textObject ) {
866835 return getTextFromObject (textObject , false );
867836 }
868837
869- @ Nullable
870- public static String getUrlFromObject (final JsonObject textObject ) {
871- if (isNullOrEmpty (textObject )) {
872- return null ;
873- }
874-
875- final JsonArray runs = textObject .getArray ("runs" );
876- if (runs .isEmpty ()) {
877- return null ;
878- }
879-
880- for (final Object textPart : runs ) {
881- final String url = getUrlFromNavigationEndpoint (((JsonObject ) textPart )
882- .getObject ("navigationEndpoint" ));
883- if (!isNullOrEmpty (url )) {
884- return url ;
885- }
886- }
887-
888- return null ;
838+ @ Nonnull
839+ public static Optional <String > getUrlFromObject (@ Nonnull final JsonObject textObject ) {
840+ return textObject .getArray ("runs" ).streamAsJsonObjects ()
841+ .map (textPart -> getUrlFromNavigationEndpoint (textPart
842+ .getObject ("navigationEndpoint" )))
843+ .filter (url -> !isNullOrEmpty (url ))
844+ .findFirst ();
889845 }
890846
891- @ Nullable
892- public static String getTextAtKey (@ Nonnull final JsonObject jsonObject , final String theKey ) {
893- if (jsonObject .isString (theKey )) {
894- return jsonObject .getString (theKey );
895- } else {
896- return getTextFromObject (jsonObject .getObject (theKey ));
897- }
847+ @ Nonnull
848+ public static Optional <String > getTextAtKey (@ Nonnull final JsonObject jsonObject ,
849+ final String theKey ) {
850+ return Optional .ofNullable (jsonObject .getString (theKey ))
851+ .filter (text -> !text .isEmpty ())
852+ .or (() -> getTextFromObject (jsonObject .getObject (theKey )));
898853 }
899854
900855 public static String fixThumbnailUrl (@ Nonnull final String thumbnailUrl ) {
@@ -1222,7 +1177,8 @@ public static void defaultAlertsCheck(@Nonnull final JsonObject initialData)
12221177 final JsonArray alerts = initialData .getArray ("alerts" );
12231178 if (!isNullOrEmpty (alerts )) {
12241179 final JsonObject alertRenderer = alerts .getObject (0 ).getObject ("alertRenderer" );
1225- final String alertText = getTextFromObject (alertRenderer .getObject ("text" ));
1180+ final String alertText = getTextFromObject (alertRenderer .getObject ("text" ))
1181+ .orElse (null );
12261182 final String alertType = alertRenderer .getString ("type" , "" );
12271183 if (alertType .equalsIgnoreCase ("ERROR" )) {
12281184 if (alertText != null
0 commit comments