Fixed checkstyle problems

Unable to compile!

* Cleaned up ``getMostCompactAudioIndex`` and ``getHighestQualityAudioIndex`` into a new method ``getAudioIndexByHighestRank``
* Removed unreadable code and use Java Streams API
* Tests work as expected
This commit is contained in:
litetex 2021-11-16 21:00:04 +01:00 committed by TiA4f8R
parent 8ed87e7fbb
commit a489f40b76
No known key found for this signature in database
GPG Key ID: E6D3E7F5949450DD
1 changed files with 46 additions and 43 deletions

View File

@ -288,28 +288,12 @@ public final class ListHelper {
* @param audioStreams List of audio streams * @param audioStreams List of audio streams
* @return Index of audio stream that produces the most compact results or -1 if not found * @return Index of audio stream that produces the most compact results or -1 if not found
*/ */
static int getHighestQualityAudioIndex(@Nullable MediaFormat format, static int getHighestQualityAudioIndex(@Nullable final MediaFormat format,
final List<AudioStream> audioStreams) { @Nullable final List<AudioStream> audioStreams) {
int result = -1; return getAudioIndexByHighestRank(format, audioStreams,
if (audioStreams != null) { // Compares descending (last = highest rank)
while (result == -1) { (s1, s2) -> compareAudioStreamBitrate(s1, s2, AUDIO_FORMAT_QUALITY_RANKING)
AudioStream prevStream = null; );
for (int idx = 0; idx < audioStreams.size(); idx++) {
final AudioStream stream = audioStreams.get(idx);
if ((format == null || stream.getFormat() == format)
&& (prevStream == null || compareAudioStreamBitrate(prevStream, stream,
AUDIO_FORMAT_QUALITY_RANKING) < 0)) {
prevStream = stream;
result = idx;
}
}
if (result == -1 && format == null) {
break;
}
format = null;
}
}
return result;
} }
/** /**
@ -320,28 +304,47 @@ public final class ListHelper {
* @param audioStreams List of audio streams * @param audioStreams List of audio streams
* @return Index of audio stream that produces the most compact results or -1 if not found * @return Index of audio stream that produces the most compact results or -1 if not found
*/ */
static int getMostCompactAudioIndex(@Nullable MediaFormat format, static int getMostCompactAudioIndex(@Nullable final MediaFormat format,
final List<AudioStream> audioStreams) { @Nullable final List<AudioStream> audioStreams) {
int result = -1;
if (audioStreams != null) { return getAudioIndexByHighestRank(format, audioStreams,
while (result == -1) { // The "-" is important -> Compares ascending (first = highest rank)
AudioStream prevStream = null; (s1, s2) -> -compareAudioStreamBitrate(s1, s2, AUDIO_FORMAT_EFFICIENCY_RANKING)
for (int idx = 0; idx < audioStreams.size(); idx++) { );
final AudioStream stream = audioStreams.get(idx);
if ((format == null || stream.getFormat() == format)
&& (prevStream == null || compareAudioStreamBitrate(prevStream, stream,
AUDIO_FORMAT_EFFICIENCY_RANKING) > 0)) {
prevStream = stream;
result = idx;
} }
/**
* Get the audio-stream from the list with the highest rank, depending on the comparator.
* Format will be ignored if it yields no results.
*
* @param targetedFormat The target format type or null if it doesn't matter
* @param audioStreams List of audio streams
* @param comparator The comparator used for determining the max/best/highest ranked value
* @return Index of audio stream that produces the most compact results or -1 if not found
*/
private static int getAudioIndexByHighestRank(@Nullable final MediaFormat targetedFormat,
@Nullable final List<AudioStream> audioStreams,
final Comparator<AudioStream> comparator) {
if (audioStreams == null || audioStreams.isEmpty()) {
return -1;
} }
if (result == -1 && format == null) {
break; final AudioStream highestRankedAudioStream = audioStreams.stream()
.filter(audioStream -> targetedFormat == null
|| audioStream.getFormat() == targetedFormat)
.max(comparator)
.orElse(null);
if (highestRankedAudioStream == null) {
// Fallback: Ignore targetedFormat if not null
if (targetedFormat != null) {
return getAudioIndexByHighestRank(null, audioStreams, comparator);
} }
format = null; // targetedFormat is already null -> return -1
return -1;
} }
}
return result; return audioStreams.indexOf(highestRankedAudioStream);
} }
/** /**