Using `.sorted((o1, o2) -> ThreadLocalRandom.current().nextInt(-1, 2))` on a stream causes *java.lang.IllegalArgumentException: Comparison method violates its general contract!*.
Because the ordering is not determinist.
This commit is contained in:
Anthony RAYMOND 2022-12-01 00:48:22 +01:00
parent e05d419195
commit a039ffac23

View file

@ -16,7 +16,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.*; import java.util.*;
import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors;
import static java.lang.String.format; import static java.lang.String.format;
import static java.util.Optional.ofNullable; import static java.util.Optional.ofNullable;
@ -120,7 +120,10 @@ public class TorrentFileProvider extends FileAlterationListenerAdaptor {
return this.torrentFiles.values().stream() return this.torrentFiles.values().stream()
.filter(torrent -> !unwantedTorrents.contains(torrent.getTorrentInfoHash())) .filter(torrent -> !unwantedTorrents.contains(torrent.getTorrentInfoHash()))
.sorted((o1, o2) -> ThreadLocalRandom.current().nextInt(-1, 2)) .collect(Collectors.collectingAndThen(Collectors.toList(), collected -> {
Collections.shuffle(collected);
return collected.stream();
}))
.findAny() .findAny()
.orElseThrow(() -> new NoMoreTorrentsFileAvailableException("No more torrent file available.")); .orElseThrow(() -> new NoMoreTorrentsFileAvailableException("No more torrent file available."));
} }