1
0
mirror of https://github.com/mitb-archive/filebot synced 2025-01-11 05:48:01 -05:00

Refactor RenameWorkers

This commit is contained in:
Reinhard Pointner 2016-08-17 02:32:25 +08:00
parent 08c6382839
commit 150ce19092

View File

@ -90,11 +90,11 @@ class RenameAction extends AbstractAction {
try { try {
if (useNativeShell() && NativeRenameAction.isSupported(action)) { if (useNativeShell() && NativeRenameAction.isSupported(action)) {
// call on EDT // call on EDT
RenameWorker worker = new NativeRenameWorker(renameMap, renameLog, NativeRenameAction.valueOf(action.name())); NativeRenameWorker worker = new NativeRenameWorker(renameMap, renameLog, NativeRenameAction.valueOf(action.name()));
worker.call(null, null, null); worker.call(null, null, null);
} else { } else {
// call and wait // call and wait
RenameWorker worker = new RenameWorker(renameMap, renameLog, action); StandardRenameWorker worker = new StandardRenameWorker(renameMap, renameLog, action);
String message = String.format("%sing %d %s. This may take a while.", action.getDisplayName(), renameMap.size(), renameMap.size() == 1 ? "file" : "files"); String message = String.format("%sing %d %s. This may take a while.", action.getDisplayName(), renameMap.size(), renameMap.size() == 1 ? "file" : "files");
ProgressMonitor.runTask(action.getDisplayName(), message, worker).get(); ProgressMonitor.runTask(action.getDisplayName(), message, worker).get();
} }
@ -288,16 +288,14 @@ class RenameAction extends AbstractAction {
return emptyList(); return emptyList();
} }
protected static class RenameWorker implements ProgressWorker<Map<File, File>> { protected static class StandardRenameWorker implements ProgressWorker<Map<File, File>> {
protected final Map<File, File> renameMap; private Map<File, File> renameMap;
protected final Map<File, File> renameLog; private Map<File, File> renameLog;
protected final net.filebot.RenameAction action; private StandardRenameAction action;
protected boolean cancelled = false; public StandardRenameWorker(Map<File, File> renameMap, Map<File, File> renameLog, StandardRenameAction action) {
public RenameWorker(Map<File, File> renameMap, Map<File, File> renameLog, net.filebot.RenameAction action) {
this.renameMap = renameMap; this.renameMap = renameMap;
this.renameLog = renameLog; this.renameLog = renameLog;
this.action = action; this.action = action;
@ -328,40 +326,47 @@ class RenameAction extends AbstractAction {
} }
} }
protected static class NativeRenameWorker extends RenameWorker { protected static class NativeRenameWorker implements ProgressWorker<Map<File, File>> {
private Map<File, File> renameMap;
private Map<File, File> renameLog;
private NativeRenameAction action;
public NativeRenameWorker(Map<File, File> renameMap, Map<File, File> renameLog, NativeRenameAction action) { public NativeRenameWorker(Map<File, File> renameMap, Map<File, File> renameLog, NativeRenameAction action) {
super(renameMap, renameLog, action); this.renameMap = renameMap;
this.renameLog = renameLog;
this.action = action;
} }
@Override @Override
public Map<File, File> call(Consumer<String> message, BiConsumer<Long, Long> progress, Supplier<Boolean> cancelled) throws Exception { public Map<File, File> call(Consumer<String> message, BiConsumer<Long, Long> progress, Supplier<Boolean> cancelled) throws Exception {
NativeRenameAction shell = (NativeRenameAction) action;
// prepare delta, ignore files already named as desired // prepare delta, ignore files already named as desired
Map<File, File> renamePlan = new LinkedHashMap<File, File>(); Map<File, File> renamePlan = new LinkedHashMap<File, File>();
renameMap.forEach((source, destination) -> { renameMap.forEach((from, to) -> {
// resolve relative paths // resolve relative paths
destination = resolve(source, destination); to = resolve(from, to);
if (!equalsCaseSensitive(source, destination)) { if (!equalsCaseSensitive(from, to)) {
renamePlan.put(source, destination); renamePlan.put(from, to);
} }
}); });
// call native shell move/copy // call native shell move/copy
try { try {
shell.rename(renamePlan); action.rename(renamePlan);
} catch (CancellationException e) { } catch (CancellationException e) {
debug.finest(e::getMessage); debug.finest(e::getMessage);
} }
for (Entry<File, File> it : renameMap.entrySet()) { // confirm results
if (resolve(it.getKey(), it.getValue()).exists()) { renameMap.forEach((from, to) -> {
renameLog.put(it.getKey(), it.getValue()); // resolve relative paths
} if (resolve(from, to).exists()) {
renameLog.put(from, to);
} }
});
return renameLog; return renameLog;
} }