Skip to content

Commit

Permalink
Improve Java 8's Streams used to create image lists
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
AudricV and Isira-Seneviratne committed Aug 24, 2022
1 parent 823d874 commit 8cac434
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,20 +60,14 @@ public List<Image> getBanners() throws ParsingException {
.get(replaceHttpWithHttps(channelInfo.getString("bandcamp_url")))
.responseBody();

final List<Image> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -288,12 +288,10 @@ public static List<Image> getImagesFromImageId(final long id, final boolean isAl
*/
@Nonnull
private static List<Image> getImagesFromImageBaseUrl(@Nonnull final String baseUrl) {
final List<Image> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -248,20 +248,13 @@ private static List<Image> getImagesFromAvatarsOrBanners(
private static List<Image> getImagesFromAvatarOrBannerArray(
@Nonnull final String baseUrl,
@Nonnull final JsonArray avatarsOrBannersArray) {
final List<Image> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1081,15 +1082,15 @@ public static List<Image> getThumbnailsFromInfoItem(@Nonnull final JsonObject in
@Nonnull
public static List<Image> getImagesFromThumbnailsArray(
@Nonnull final JsonArray thumbnails) {
final List<Image> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -276,14 +276,13 @@ private List<Image> getThumbnailsFromPlaylistId(@Nonnull final String playlistId
}

@Nonnull
private List<Image> getThumbnailsFromVideoId(final String videoId) {
private List<Image> getThumbnailsFromVideoId(@Nonnull final String videoId) {
final String baseUrl = "https://i.ytimg.com/vi/" + videoId + "/";
final List<Image> 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
Expand Down

0 comments on commit 8cac434

Please sign in to comment.