001package net.filebot.postprocess;
002
003import java.io.File;
004import java.util.stream.Stream;
005
006import groovy.lang.Closure;
007
008import net.filebot.format.MediaBindingBean;
009
010public class ApplyClosure implements ApplyStep {
011
012        private final Closure<?> closure;
013
014        public ApplyClosure(Closure<?> closure) {
015                this.closure = closure;
016        }
017
018        @Override
019        public boolean accept(File file, Object object) {
020                return true;
021        }
022
023        @Override
024        public void apply(File source, File destination, Object object, Feedback log) {
025                log.trace("Run Closure", destination);
026
027                // make standard format bindings available in the closure
028                closure.setDelegate(new MediaBindingBean(object, destination).getSelf());
029
030                // call with suitable number or arguments (source, destination, object)
031                Object result = closure.call(Stream.of(source, destination, object).limit(closure.getMaximumNumberOfParameters()).toArray());
032
033                log.trace(result, destination);
034        }
035
036        @Override
037        public String toString() {
038                return "CLOSURE";
039        }
040
041}