001package net.filebot.postprocess;
002
003import static java.util.stream.Collectors.*;
004import static net.filebot.Logging.*;
005
006import java.io.File;
007import java.util.List;
008import java.util.Map;
009import java.util.Map.Entry;
010import java.util.concurrent.CancellationException;
011
012import net.filebot.RenameAction;
013import net.filebot.similarity.Match;
014
015public interface ApplyStep extends Apply {
016
017        @Override
018        default void apply(Map<File, Match<File, ?>> map, RenameAction operation, Feedback log) {
019                List<Entry<File, Match<File, ?>>> selection = map.entrySet().stream().filter(m -> {
020                        return accept(m.getKey(), m.getValue().getCandidate());
021                }).collect(toList());
022
023                int progress = 0;
024                int maximum = selection.size();
025
026                for (Entry<File, Match<File, ?>> m : selection) {
027                        File source = m.getValue().getValue();
028                        File destination = m.getKey();
029                        Object object = m.getValue().getCandidate();
030
031                        if (log.isCancelled()) {
032                                throw new CancellationException();
033                        }
034
035                        log.progress(progress++, maximum);
036
037                        try {
038                                apply(source, destination, object, log);
039                        } catch (CancellationException e) {
040                                throw e;
041                        } catch (Exception e) {
042                                log.warning(cause(e), destination);
043                        }
044                }
045        }
046
047        boolean accept(File file, Object object);
048
049        void apply(File source, File destination, Object object, Feedback log) throws Exception;
050
051}