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