Skip to content

Commit 80fb21e

Browse files
authored
Merge pull request #8728 from Isira-Seneviratne/Comparator_factory
Use Comparator factory methods.
2 parents 6f86e21 + ebd06bd commit 80fb21e

2 files changed

Lines changed: 14 additions & 52 deletions

File tree

app/src/main/java/org/schabi/newpipe/settings/preferencesearch/PreferenceFuzzySearchFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Stream<PreferenceSearchItem> search(
3131
// Specific search - Used for determining order of search results
3232
// Calculate a score based on specific search fields
3333
.map(item -> new FuzzySearchSpecificDTO(item, keyword))
34-
.sorted(Comparator.comparing(FuzzySearchSpecificDTO::getScore).reversed())
34+
.sorted(Comparator.comparingDouble(FuzzySearchSpecificDTO::getScore).reversed())
3535
.map(FuzzySearchSpecificDTO::getItem)
3636
// Limit the amount of search results
3737
.limit(20);

app/src/main/java/org/schabi/newpipe/util/ListHelper.java

Lines changed: 13 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,10 @@ static List<VideoStream> getSortedStreamVideosList(
343343
*/
344344
private static List<VideoStream> sortStreamList(final List<VideoStream> videoStreams,
345345
final boolean ascendingOrder) {
346-
final Comparator<VideoStream> comparator = ListHelper::compareVideoStreamResolution;
346+
// Compares the quality of two video streams.
347+
final Comparator<VideoStream> comparator = Comparator.nullsLast(Comparator
348+
.comparing(VideoStream::getResolution, ListHelper::compareVideoStreamResolution)
349+
.thenComparingInt(s -> VIDEO_FORMAT_QUALITY_RANKING.indexOf(s.getFormat())));
347350
Collections.sort(videoStreams, ascendingOrder ? comparator : comparator.reversed());
348351
return videoStreams;
349352
}
@@ -360,8 +363,7 @@ static int getHighestQualityAudioIndex(@Nullable final MediaFormat format,
360363
@Nullable final List<AudioStream> audioStreams) {
361364
return getAudioIndexByHighestRank(format, audioStreams,
362365
// Compares descending (last = highest rank)
363-
(s1, s2) -> compareAudioStreamBitrate(s1, s2, AUDIO_FORMAT_QUALITY_RANKING)
364-
);
366+
getAudioStreamComparator(AUDIO_FORMAT_QUALITY_RANKING));
365367
}
366368

367369
/**
@@ -374,11 +376,15 @@ static int getHighestQualityAudioIndex(@Nullable final MediaFormat format,
374376
*/
375377
static int getMostCompactAudioIndex(@Nullable final MediaFormat format,
376378
@Nullable final List<AudioStream> audioStreams) {
377-
378379
return getAudioIndexByHighestRank(format, audioStreams,
379-
// The "-" is important -> Compares ascending (first = highest rank)
380-
(s1, s2) -> -compareAudioStreamBitrate(s1, s2, AUDIO_FORMAT_EFFICIENCY_RANKING)
381-
);
380+
// The "reversed()" is important -> Compares ascending (first = highest rank)
381+
getAudioStreamComparator(AUDIO_FORMAT_EFFICIENCY_RANKING).reversed());
382+
}
383+
384+
private static Comparator<AudioStream> getAudioStreamComparator(
385+
final List<MediaFormat> formatRanking) {
386+
return Comparator.nullsLast(Comparator.comparingInt(AudioStream::getAverageBitrate))
387+
.thenComparingInt(stream -> formatRanking.indexOf(stream.getFormat()));
382388
}
383389

384390
/**
@@ -544,28 +550,6 @@ private static MediaFormat getMediaFormatFromKey(@NonNull final Context context,
544550
return format;
545551
}
546552

547-
// Compares the quality of two audio streams
548-
private static int compareAudioStreamBitrate(final AudioStream streamA,
549-
final AudioStream streamB,
550-
final List<MediaFormat> formatRanking) {
551-
if (streamA == null) {
552-
return -1;
553-
}
554-
if (streamB == null) {
555-
return 1;
556-
}
557-
if (streamA.getAverageBitrate() < streamB.getAverageBitrate()) {
558-
return -1;
559-
}
560-
if (streamA.getAverageBitrate() > streamB.getAverageBitrate()) {
561-
return 1;
562-
}
563-
564-
// Same bitrate and format
565-
return formatRanking.indexOf(streamA.getFormat())
566-
- formatRanking.indexOf(streamB.getFormat());
567-
}
568-
569553
private static int compareVideoStreamResolution(@NonNull final String r1,
570554
@NonNull final String r2) {
571555
try {
@@ -582,28 +566,6 @@ private static int compareVideoStreamResolution(@NonNull final String r1,
582566
}
583567
}
584568

585-
// Compares the quality of two video streams.
586-
private static int compareVideoStreamResolution(final VideoStream streamA,
587-
final VideoStream streamB) {
588-
if (streamA == null) {
589-
return -1;
590-
}
591-
if (streamB == null) {
592-
return 1;
593-
}
594-
595-
final int resComp = compareVideoStreamResolution(streamA.getResolution(),
596-
streamB.getResolution());
597-
if (resComp != 0) {
598-
return resComp;
599-
}
600-
601-
// Same bitrate and format
602-
return ListHelper.VIDEO_FORMAT_QUALITY_RANKING.indexOf(streamA.getFormat())
603-
- ListHelper.VIDEO_FORMAT_QUALITY_RANKING.indexOf(streamB.getFormat());
604-
}
605-
606-
607569
private static boolean isLimitingDataUsage(final Context context) {
608570
return getResolutionLimit(context) != null;
609571
}

0 commit comments

Comments
 (0)