From 8cac43485998cd2fd85185f7c919f0dfd1f8f83d Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Sun, 14 Aug 2022 16:51:43 +0200 Subject: [PATCH] Improve Java 8's Streams used to create image lists Make use of collect method of the Java 8 Stream's API and Collectors.collectingAndThen(Collectors.toList(),Collections::unmodifiableList) to return unmodifiable lists, as Collectors.toUnmodifiableList() is only available on Java 10+. This replaces creation of ArrayLists wrapped in unmodifiable lists by Java 8's Stream API users, so the extractors and their helper classes. Co-authored-by: Isira Seneviratne --- .../extractors/BandcampChannelExtractor.java | 20 +++++++----------- .../extractors/BandcampExtractorHelper.java | 14 ++++++------- .../peertube/PeertubeParsingHelper.java | 21 +++++++------------ .../youtube/YoutubeParsingHelper.java | 15 ++++++------- .../YoutubeMixPlaylistExtractor.java | 15 +++++++------ 5 files changed, 35 insertions(+), 50 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelExtractor.java index 08decffab9..0c63aaeaa7 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelExtractor.java @@ -27,10 +27,10 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nonnull; @@ -60,20 +60,14 @@ public List getBanners() throws ParsingException { .get(replaceHttpWithHttps(channelInfo.getString("bandcamp_url"))) .responseBody(); - final List imageList = new ArrayList<>(); - - Stream.of(Jsoup.parse(html).getElementById("customHeader")) + return Stream.of(Jsoup.parse(html).getElementById("customHeader")) .filter(Objects::nonNull) .flatMap(element -> element.getElementsByTag("img").stream()) - .forEach(element -> { - final String url = element.attr("src"); - if (!isNullOrEmpty(url)) { - imageList.add(new Image(replaceHttpWithHttps(url), - HEIGHT_UNKNOWN, WIDTH_UNKNOWN)); - } - }); - - return Collections.unmodifiableList(imageList); + .map(element -> element.attr("src")) + .filter(url -> !isNullOrEmpty(url)) + .map(url -> new Image(replaceHttpWithHttps(url), HEIGHT_UNKNOWN, WIDTH_UNKNOWN)) + .collect(Collectors.collectingAndThen(Collectors.toList(), + Collections::unmodifiableList)); } catch (final IOException | ReCaptchaException e) { throw new ParsingException("Could not download artist web site", e); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java index 148f28cc34..89aa99163f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java @@ -20,11 +20,11 @@ import java.time.DateTimeException; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.stream.Collectors; import javax.annotation.Nonnull; @@ -288,12 +288,10 @@ public static List getImagesFromImageId(final long id, final boolean isAl */ @Nonnull private static List getImagesFromImageBaseUrl(@Nonnull final String baseUrl) { - final List imageList = new ArrayList<>(); - IMAGE_URL_SUFFIXES_AND_RESOLUTIONS.stream() - .forEach(imageSuffix -> imageList.add( - new Image(baseUrl + imageSuffix.getSuffix(), imageSuffix.getHeight(), - imageSuffix.getWidth()))); - - return Collections.unmodifiableList(imageList); + return IMAGE_URL_SUFFIXES_AND_RESOLUTIONS.stream() + .map(imageSuffix -> new Image(baseUrl + imageSuffix.getSuffix(), + imageSuffix.getHeight(), imageSuffix.getWidth())) + .collect(Collectors.collectingAndThen(Collectors.toList(), + Collections::unmodifiableList)); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java index a03beb1064..956c6230cc 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java @@ -17,9 +17,9 @@ import java.time.OffsetDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeParseException; -import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import static org.schabi.newpipe.extractor.Image.HEIGHT_UNKNOWN; import static org.schabi.newpipe.extractor.Image.WIDTH_UNKNOWN; @@ -248,20 +248,13 @@ private static List getImagesFromAvatarsOrBanners( private static List getImagesFromAvatarOrBannerArray( @Nonnull final String baseUrl, @Nonnull final JsonArray avatarsOrBannersArray) { - final List imageList = new ArrayList<>(); - avatarsOrBannersArray.stream() + return avatarsOrBannersArray.stream() .filter(JsonObject.class::isInstance) .map(JsonObject.class::cast) - .forEach(image -> { - final String path = image.getString("path"); - if (isNullOrEmpty(path)) { - return; - } - - imageList.add(new Image(baseUrl + path, HEIGHT_UNKNOWN, - image.getInt("width", WIDTH_UNKNOWN))); - }); - - return Collections.unmodifiableList(imageList); + .filter(image -> !isNullOrEmpty(image.getString("path"))) + .map(image -> new Image(baseUrl + image.getString("path"), HEIGHT_UNKNOWN, + image.getInt("width", WIDTH_UNKNOWN))) + .collect(Collectors.collectingAndThen(Collectors.toList(), + Collections::unmodifiableList)); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java index e0cb031f36..bf3b9f356f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java @@ -72,6 +72,7 @@ import java.util.Optional; import java.util.Random; import java.util.regex.Pattern; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nonnull; @@ -1081,15 +1082,15 @@ public static List getThumbnailsFromInfoItem(@Nonnull final JsonObject in @Nonnull public static List getImagesFromThumbnailsArray( @Nonnull final JsonArray thumbnails) { - final List imageList = new ArrayList<>(); - thumbnails.stream() + return thumbnails.stream() .filter(JsonObject.class::isInstance) .map(JsonObject.class::cast) - .forEach(thumbnail -> imageList.add( - new Image(fixThumbnailUrl(thumbnail.getString("url")), - thumbnail.getInt("height", Image.HEIGHT_UNKNOWN), - thumbnail.getInt("width", Image.WIDTH_UNKNOWN)))); - return Collections.unmodifiableList(imageList); + .filter(thumbnail -> !isNullOrEmpty(thumbnail.getString("url"))) + .map(thumbnail -> new Image(fixThumbnailUrl(thumbnail.getString("url")), + thumbnail.getInt("height", Image.HEIGHT_UNKNOWN), + thumbnail.getInt("width", Image.WIDTH_UNKNOWN))) + .collect(Collectors.collectingAndThen(Collectors.toList(), + Collections::unmodifiableList)); } @Nonnull diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java index efd8d16115..a8fe9ff02b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeMixPlaylistExtractor.java @@ -40,13 +40,13 @@ import java.io.IOException; import java.net.URL; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -276,14 +276,13 @@ private List getThumbnailsFromPlaylistId(@Nonnull final String playlistId } @Nonnull - private List getThumbnailsFromVideoId(final String videoId) { + private List getThumbnailsFromVideoId(@Nonnull final String videoId) { final String baseUrl = "https://i.ytimg.com/vi/" + videoId + "/"; - final List imageList = new ArrayList<>(IMAGE_URL_SUFFIXES_AND_RESOLUTIONS.size()); - IMAGE_URL_SUFFIXES_AND_RESOLUTIONS.stream() - .forEach(imageSuffix -> imageList.add( - new Image(baseUrl + imageSuffix.getSuffix(), imageSuffix.getHeight(), - imageSuffix.getWidth()))); - return Collections.unmodifiableList(imageList); + return IMAGE_URL_SUFFIXES_AND_RESOLUTIONS.stream() + .map(imageSuffix -> new Image(baseUrl + imageSuffix.getSuffix(), + imageSuffix.getHeight(), imageSuffix.getWidth())) + .collect(Collectors.collectingAndThen(Collectors.toList(), + Collections::unmodifiableList)); } @Nonnull