* ensure rename map is always sorted by path

This commit is contained in:
Reinhard Pointner 2014-03-20 07:45:56 +00:00
parent c745ea01a3
commit 8aa33750a6
2 changed files with 24 additions and 7 deletions

View File

@ -22,7 +22,6 @@ import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -142,7 +141,7 @@ public class CmdlineOperations implements CmdlineInterface {
CLILogger.config(format("Rename episodes using [%s]", db.getName())); CLILogger.config(format("Rename episodes using [%s]", db.getName()));
// ignore sample files // ignore sample files
List<File> fileset = filter(files, not(getClutterFileFilter())); List<File> fileset = sortByUniquePath((filter(files, not(getClutterFileFilter()))));
List<File> mediaFiles = filter(fileset, VIDEO_FILES, SUBTITLE_FILES); List<File> mediaFiles = filter(fileset, VIDEO_FILES, SUBTITLE_FILES);
if (mediaFiles.isEmpty()) { if (mediaFiles.isEmpty()) {
@ -224,7 +223,7 @@ public class CmdlineOperations implements CmdlineInterface {
matches.addAll(derivateMatches); matches.addAll(derivateMatches);
// map old files to new paths by applying formatting and validating filenames // map old files to new paths by applying formatting and validating filenames
Map<File, File> renameMap = new LinkedHashMap<File, File>(); Map<File, File> renameMap = new TreeMap<File, File>(CASE_INSENSITIVE_PATH);
for (Match<File, ?> match : matches) { for (Match<File, ?> match : matches) {
File file = match.getValue(); File file = match.getValue();
@ -286,7 +285,7 @@ public class CmdlineOperations implements CmdlineInterface {
CLILogger.config(format("Rename movies using [%s]", service.getName())); CLILogger.config(format("Rename movies using [%s]", service.getName()));
// ignore sample files // ignore sample files
List<File> fileset = filter(files, not(getClutterFileFilter())); List<File> fileset = sortByUniquePath(filter(files, not(getClutterFileFilter())));
// handle movie files // handle movie files
Set<File> movieFiles = new TreeSet<File>(filter(fileset, VIDEO_FILES)); Set<File> movieFiles = new TreeSet<File>(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 old files to new paths by applying formatting and validating filenames
Map<File, File> renameMap = new LinkedHashMap<File, File>(); Map<File, File> renameMap = new TreeMap<File, File>(CASE_INSENSITIVE_PATH);
for (Match<File, ?> match : matches) { for (Match<File, ?> match : matches) {
File file = match.getValue(); File file = match.getValue();
@ -484,7 +483,7 @@ public class CmdlineOperations implements CmdlineInterface {
public List<File> renameMusic(Collection<File> files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, MusicIdentificationService service) throws Exception { public List<File> renameMusic(Collection<File> files, RenameAction renameAction, ConflictAction conflictAction, File outputDir, ExpressionFormat format, MusicIdentificationService service) throws Exception {
CLILogger.config(format("Rename music using [%s]", service.getName())); CLILogger.config(format("Rename music using [%s]", service.getName()));
List<File> audioFiles = filter(files, AUDIO_FILES); List<File> audioFiles = sortByUniquePath(filter(files, AUDIO_FILES));
// check audio files against acoustid // check audio files against acoustid
List<Match<File, ?>> matches = new ArrayList<Match<File, ?>>(); List<Match<File, ?>> matches = new ArrayList<Match<File, ?>>();
@ -495,7 +494,7 @@ public class CmdlineOperations implements CmdlineInterface {
} }
// map old files to new paths by applying formatting and validating filenames // map old files to new paths by applying formatting and validating filenames
Map<File, File> renameMap = new LinkedHashMap<File, File>(); Map<File, File> renameMap = new TreeMap<File, File>(CASE_INSENSITIVE_PATH);
for (Match<File, ?> it : matches) { for (Match<File, ?> it : matches) {
File file = it.getValue(); File file = it.getValue();

View File

@ -18,6 +18,7 @@ import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
@ -26,6 +27,7 @@ import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.SortedMap; import java.util.SortedMap;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -322,6 +324,14 @@ public final class FileUtilities {
return true; return true;
} }
public static List<File> sortByUniquePath(Collection<File> files) {
// sort by unique lower-case paths
TreeSet<File> sortedSet = new TreeSet<File>(CASE_INSENSITIVE_PATH);
sortedSet.addAll(files);
return new ArrayList<File>(sortedSet);
}
public static List<File> filter(Iterable<File> files, FileFilter... filters) { public static List<File> filter(Iterable<File> files, FileFilter... filters) {
List<File> accepted = new ArrayList<File>(); List<File> accepted = new ArrayList<File>();
@ -665,6 +675,14 @@ public final class FileUtilities {
} }
} }
public static final Comparator<File> CASE_INSENSITIVE_PATH = new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.getPath().compareToIgnoreCase(o2.getPath());
}
};
/** /**
* Dummy constructor to prevent instantiation. * Dummy constructor to prevent instantiation.
*/ */