package tv.mangrana.worker; import java.nio.file.Path; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class MissingFilesDetector { void printDifferencesBetween(List torrentFiles, List sonarrFiles) { Map> torrentFileLengths = getFileLengthsMapFrom(torrentFiles); if (hasFilesWithSameSize(torrentFileLengths)) return; Map> sonarrFileLengths = getFileLengthsMapFrom(sonarrFiles); if (hasFilesWithSameSize(sonarrFileLengths)) return; torrentFileLengths.keySet().forEach(fileSize -> takeDecisionForComparing(fileSize, torrentFileLengths)); } Map> getFileLengthsMapFrom(List files) { return files.stream() .collect(Collectors.groupingBy(p -> p.toFile().length())); } boolean hasFilesWithSameSize(Map> torrentFileLengths) { return torrentFileLengths .values() .stream() .anyMatch(this::hasMultipleElements); } boolean hasMultipleElements(List paths) { if (paths.size() > 1) { var sampleFile = paths.get(0).toFile(); System.out.printf("There is more than one file with the same size of %s. Name: %s %n", sampleFile.length(), sampleFile.getName()); return true; } return false; } void takeDecisionForComparing(Long fileSize, Map> torrentFileLengths) { var alreadyExists = torrentFileLengths.containsKey(fileSize); if (!alreadyExists) { System.out.printf("- needs to copy %s%n", torrentFileLengths.get(fileSize)); } } }