From e48ecf4fa27d48ea1a8ee52082b3f6322dd6c1e5 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 6 Apr 2016 18:56:33 +0000 Subject: [PATCH] Refactor AutoCompleteMatcher --- .../ui/rename/AutoCompleteMatcher.java | 3 ++- .../filebot/ui/rename/EpisodeListMatcher.java | 12 ++++++------ .../filebot/ui/rename/MovieHashMatcher.java | 19 +++++++------------ .../filebot/ui/rename/PlainFileMatcher.java | 14 +++++++------- source/net/filebot/web/AcoustIDClient.java | 2 +- 5 files changed, 23 insertions(+), 27 deletions(-) diff --git a/source/net/filebot/ui/rename/AutoCompleteMatcher.java b/source/net/filebot/ui/rename/AutoCompleteMatcher.java index c73ca335..97759c55 100644 --- a/source/net/filebot/ui/rename/AutoCompleteMatcher.java +++ b/source/net/filebot/ui/rename/AutoCompleteMatcher.java @@ -2,6 +2,7 @@ package net.filebot.ui.rename; import java.awt.Component; import java.io.File; +import java.util.Collection; import java.util.List; import java.util.Locale; @@ -10,5 +11,5 @@ import net.filebot.web.SortOrder; interface AutoCompleteMatcher { - List> match(List files, boolean strict, SortOrder order, Locale locale, boolean autodetection, Component parent) throws Exception; + List> match(Collection files, boolean strict, SortOrder order, Locale locale, boolean autodetection, Component parent) throws Exception; } diff --git a/source/net/filebot/ui/rename/EpisodeListMatcher.java b/source/net/filebot/ui/rename/EpisodeListMatcher.java index 89a5edbf..75045991 100644 --- a/source/net/filebot/ui/rename/EpisodeListMatcher.java +++ b/source/net/filebot/ui/rename/EpisodeListMatcher.java @@ -184,28 +184,28 @@ class EpisodeListMatcher implements AutoCompleteMatcher { } @Override - public List> match(List files, final boolean strict, final SortOrder sortOrder, final Locale locale, final boolean autodetection, final Component parent) throws Exception { + public List> match(Collection files, boolean strict, SortOrder sortOrder, Locale locale, boolean autodetection, Component parent) throws Exception { if (files.isEmpty()) { return justFetchEpisodeList(sortOrder, locale, parent); } // ignore sample files - final List fileset = autodetection ? filter(files, not(getClutterFileFilter())) : files; + List fileset = autodetection ? filter(files, not(getClutterFileFilter())) : new ArrayList(files); // focus on movie and subtitle files - final List mediaFiles = filter(fileset, VIDEO_FILES, SUBTITLE_FILES); + List mediaFiles = filter(fileset, VIDEO_FILES, SUBTITLE_FILES); // assume that many shows will be matched, do it folder by folder List>>> tasks = new ArrayList>>>(); // remember user decisions and only bother user once - final Map selectionMemory = new TreeMap(CommonSequenceMatcher.getLenientCollator(Locale.ENGLISH)); - final Map> inputMemory = new TreeMap>(CommonSequenceMatcher.getLenientCollator(Locale.ENGLISH)); + Map selectionMemory = new TreeMap(CommonSequenceMatcher.getLenientCollator(Locale.ENGLISH)); + Map> inputMemory = new TreeMap>(CommonSequenceMatcher.getLenientCollator(Locale.ENGLISH)); // detect series names and create episode list fetch tasks if (strict) { // in strict mode simply process file-by-file (ignoring all files that don't contain clear SxE patterns) - for (final File file : mediaFiles) { + for (File file : mediaFiles) { if (parseEpisodeNumber(file, false) != null || parseDate(file) != null) { tasks.add(new Callable>>() { diff --git a/source/net/filebot/ui/rename/MovieHashMatcher.java b/source/net/filebot/ui/rename/MovieHashMatcher.java index dd7f01a4..6d11015d 100644 --- a/source/net/filebot/ui/rename/MovieHashMatcher.java +++ b/source/net/filebot/ui/rename/MovieHashMatcher.java @@ -1,6 +1,6 @@ package net.filebot.ui.rename; -import static java.util.Collections.*; +import static java.util.stream.Collectors.*; import static net.filebot.Logging.*; import static net.filebot.MediaTypes.*; import static net.filebot.Settings.*; @@ -15,7 +15,6 @@ import java.awt.Dimension; import java.io.File; import java.util.ArrayList; import java.util.Collection; -import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -35,6 +34,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; import java.util.concurrent.RunnableFuture; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import javax.swing.Action; @@ -60,13 +60,13 @@ class MovieHashMatcher implements AutoCompleteMatcher { } @Override - public List> match(final List files, final boolean strict, final SortOrder sortOrder, final Locale locale, final boolean autodetect, final Component parent) throws Exception { + public List> match(Collection files, boolean strict, SortOrder sortOrder, Locale locale, boolean autodetect, Component parent) throws Exception { if (files.isEmpty()) { return justFetchMovieInfo(locale, parent); } // ignore sample files - List fileset = autodetect ? filter(files, not(getClutterFileFilter())) : files; + List fileset = autodetect ? filter(files, not(getClutterFileFilter())) : new ArrayList(files); // handle movie files Set movieFiles = new TreeSet(filter(fileset, VIDEO_FILES)); @@ -255,14 +255,9 @@ class MovieHashMatcher implements AutoCompleteMatcher { } // restore original order - sort(matches, new Comparator>() { - - @Override - public int compare(Match o1, Match o2) { - return files.indexOf(o1.getValue()) - files.indexOf(o2.getValue()); - } - }); - + AtomicInteger index = new AtomicInteger(0); + Map indexMap = files.stream().collect(toMap(f -> f, f -> index.getAndIncrement())); + matches.sort((a, b) -> indexMap.get(a.getValue()).compareTo(indexMap.get(b.getValue()))); return matches; } diff --git a/source/net/filebot/ui/rename/PlainFileMatcher.java b/source/net/filebot/ui/rename/PlainFileMatcher.java index fcbd760a..a0ff4507 100644 --- a/source/net/filebot/ui/rename/PlainFileMatcher.java +++ b/source/net/filebot/ui/rename/PlainFileMatcher.java @@ -1,8 +1,10 @@ package net.filebot.ui.rename; +import static java.util.stream.Collectors.*; + import java.awt.Component; import java.io.File; -import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Locale; @@ -30,12 +32,10 @@ public class PlainFileMatcher implements Datasource, AutoCompleteMatcher { } @Override - public List> match(List files, boolean strict, SortOrder order, Locale locale, boolean autodetection, Component parent) throws Exception { - List> matches = new ArrayList<>(); - for (File f : files) { - matches.add(new Match(f, f)); - } - return matches; + public List> match(Collection files, boolean strict, SortOrder order, Locale locale, boolean autodetection, Component parent) throws Exception { + return files.stream().map(f -> { + return new Match(f, f); + }).collect(toList()); } } diff --git a/source/net/filebot/web/AcoustIDClient.java b/source/net/filebot/web/AcoustIDClient.java index 950d6fa9..08c303e2 100644 --- a/source/net/filebot/web/AcoustIDClient.java +++ b/source/net/filebot/web/AcoustIDClient.java @@ -190,7 +190,7 @@ public class AcoustIDClient implements MusicIdentificationService { List command = new ArrayList(); command.add(getChromaprintCommand()); for (File f : files) { - command.add(f.toString()); + command.add(f.getPath()); } Process process = null;