From 8aa33750a62d56492472bfdb22791b3272050e8c Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Thu, 20 Mar 2014 07:45:56 +0000 Subject: [PATCH] * ensure rename map is always sorted by path --- .../filebot/cli/CmdlineOperations.java | 13 ++++++------- .../net/sourceforge/tuned/FileUtilities.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/source/net/sourceforge/filebot/cli/CmdlineOperations.java b/source/net/sourceforge/filebot/cli/CmdlineOperations.java index 0857fb62..81d41106 100644 --- a/source/net/sourceforge/filebot/cli/CmdlineOperations.java +++ b/source/net/sourceforge/filebot/cli/CmdlineOperations.java @@ -22,7 +22,6 @@ import java.util.AbstractMap.SimpleImmutableEntry; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; @@ -142,7 +141,7 @@ public class CmdlineOperations implements CmdlineInterface { CLILogger.config(format("Rename episodes using [%s]", db.getName())); // ignore sample files - List fileset = filter(files, not(getClutterFileFilter())); + List fileset = sortByUniquePath((filter(files, not(getClutterFileFilter())))); List mediaFiles = filter(fileset, VIDEO_FILES, SUBTITLE_FILES); if (mediaFiles.isEmpty()) { @@ -224,7 +223,7 @@ public class CmdlineOperations implements CmdlineInterface { matches.addAll(derivateMatches); // map old files to new paths by applying formatting and validating filenames - Map renameMap = new LinkedHashMap(); + Map renameMap = new TreeMap(CASE_INSENSITIVE_PATH); for (Match match : matches) { File file = match.getValue(); @@ -286,7 +285,7 @@ public class CmdlineOperations implements CmdlineInterface { CLILogger.config(format("Rename movies using [%s]", service.getName())); // ignore sample files - List fileset = filter(files, not(getClutterFileFilter())); + List fileset = sortByUniquePath(filter(files, not(getClutterFileFilter()))); // handle movie files Set movieFiles = new TreeSet(filter(fileset, VIDEO_FILES)); @@ -467,7 +466,7 @@ public class CmdlineOperations implements CmdlineInterface { } // map old files to new paths by applying formatting and validating filenames - Map renameMap = new LinkedHashMap(); + Map renameMap = new TreeMap(CASE_INSENSITIVE_PATH); for (Match match : matches) { File file = match.getValue(); @@ -484,7 +483,7 @@ public class CmdlineOperations implements CmdlineInterface { public List renameMusic(Collection files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, MusicIdentificationService service) throws Exception { CLILogger.config(format("Rename music using [%s]", service.getName())); - List audioFiles = filter(files, AUDIO_FILES); + List audioFiles = sortByUniquePath(filter(files, AUDIO_FILES)); // check audio files against acoustid List> matches = new ArrayList>(); @@ -495,7 +494,7 @@ public class CmdlineOperations implements CmdlineInterface { } // map old files to new paths by applying formatting and validating filenames - Map renameMap = new LinkedHashMap(); + Map renameMap = new TreeMap(CASE_INSENSITIVE_PATH); for (Match it : matches) { File file = it.getValue(); diff --git a/source/net/sourceforge/tuned/FileUtilities.java b/source/net/sourceforge/tuned/FileUtilities.java index ebc09684..e8970379 100644 --- a/source/net/sourceforge/tuned/FileUtilities.java +++ b/source/net/sourceforge/tuned/FileUtilities.java @@ -18,6 +18,7 @@ import java.nio.file.AtomicMoveNotSupportedException; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -26,6 +27,7 @@ import java.util.Map; import java.util.Scanner; import java.util.SortedMap; import java.util.TreeMap; +import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -322,6 +324,14 @@ public final class FileUtilities { return true; } + public static List sortByUniquePath(Collection files) { + // sort by unique lower-case paths + TreeSet sortedSet = new TreeSet(CASE_INSENSITIVE_PATH); + sortedSet.addAll(files); + + return new ArrayList(sortedSet); + } + public static List filter(Iterable files, FileFilter... filters) { List accepted = new ArrayList(); @@ -665,6 +675,14 @@ public final class FileUtilities { } } + public static final Comparator CASE_INSENSITIVE_PATH = new Comparator() { + + @Override + public int compare(File o1, File o2) { + return o1.getPath().compareToIgnoreCase(o2.getPath()); + } + }; + /** * Dummy constructor to prevent instantiation. */