From 059cfcbad26422bff114cab9e94da03219243d70 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 3 Aug 2022 04:38:47 +0530 Subject: [PATCH] Use Comparator factory methods in ListHelper. --- .../org/schabi/newpipe/util/ListHelper.java | 62 ++++--------------- 1 file changed, 12 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java index fbbe43513..45234706b 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -343,7 +343,10 @@ public final class ListHelper { */ private static List sortStreamList(final List videoStreams, final boolean ascendingOrder) { - final Comparator comparator = ListHelper::compareVideoStreamResolution; + // Compares the quality of two video streams. + final Comparator comparator = Comparator.nullsLast(Comparator + .comparing(VideoStream::getResolution, ListHelper::compareVideoStreamResolution) + .thenComparingInt(s -> VIDEO_FORMAT_QUALITY_RANKING.indexOf(s.getFormat()))); Collections.sort(videoStreams, ascendingOrder ? comparator : comparator.reversed()); return videoStreams; } @@ -360,8 +363,7 @@ public final class ListHelper { @Nullable final List audioStreams) { return getAudioIndexByHighestRank(format, audioStreams, // Compares descending (last = highest rank) - (s1, s2) -> compareAudioStreamBitrate(s1, s2, AUDIO_FORMAT_QUALITY_RANKING) - ); + getAudioStreamComparator(AUDIO_FORMAT_QUALITY_RANKING)); } /** @@ -374,11 +376,15 @@ public final class ListHelper { */ static int getMostCompactAudioIndex(@Nullable final MediaFormat format, @Nullable final List audioStreams) { - return getAudioIndexByHighestRank(format, audioStreams, // The "-" is important -> Compares ascending (first = highest rank) - (s1, s2) -> -compareAudioStreamBitrate(s1, s2, AUDIO_FORMAT_EFFICIENCY_RANKING) - ); + getAudioStreamComparator(AUDIO_FORMAT_EFFICIENCY_RANKING).reversed()); + } + + private static Comparator getAudioStreamComparator( + final List formatRanking) { + return Comparator.nullsLast(Comparator.comparingInt(AudioStream::getAverageBitrate)) + .thenComparingInt(stream -> formatRanking.indexOf(stream.getFormat())); } /** @@ -544,28 +550,6 @@ public final class ListHelper { return format; } - // Compares the quality of two audio streams - private static int compareAudioStreamBitrate(final AudioStream streamA, - final AudioStream streamB, - final List formatRanking) { - if (streamA == null) { - return -1; - } - if (streamB == null) { - return 1; - } - if (streamA.getAverageBitrate() < streamB.getAverageBitrate()) { - return -1; - } - if (streamA.getAverageBitrate() > streamB.getAverageBitrate()) { - return 1; - } - - // Same bitrate and format - return formatRanking.indexOf(streamA.getFormat()) - - formatRanking.indexOf(streamB.getFormat()); - } - private static int compareVideoStreamResolution(@NonNull final String r1, @NonNull final String r2) { try { @@ -582,28 +566,6 @@ public final class ListHelper { } } - // Compares the quality of two video streams. - private static int compareVideoStreamResolution(final VideoStream streamA, - final VideoStream streamB) { - if (streamA == null) { - return -1; - } - if (streamB == null) { - return 1; - } - - final int resComp = compareVideoStreamResolution(streamA.getResolution(), - streamB.getResolution()); - if (resComp != 0) { - return resComp; - } - - // Same bitrate and format - return ListHelper.VIDEO_FORMAT_QUALITY_RANKING.indexOf(streamA.getFormat()) - - ListHelper.VIDEO_FORMAT_QUALITY_RANKING.indexOf(streamB.getFormat()); - } - - private static boolean isLimitingDataUsage(final Context context) { return getResolutionLimit(context) != null; }