filebot/source/net/filebot/format/ExpressionFileFilter.java

41 lines
948 B
Java
Raw Permalink Normal View History

package net.filebot.format;
2015-07-26 07:52:11 -04:00
2016-03-09 15:36:28 -05:00
import static net.filebot.Logging.*;
2015-07-26 07:52:11 -04:00
import java.io.File;
import java.io.FileFilter;
import java.util.function.Function;
2015-07-26 07:52:11 -04:00
import javax.script.ScriptException;
2015-07-26 07:52:11 -04:00
public class ExpressionFileFilter implements FileFilter {
private ExpressionFilter filter;
private Function<File, Object> match;
2015-07-26 07:52:11 -04:00
public ExpressionFileFilter(String expression) throws ScriptException {
// use file object as match object by default
this(expression, f -> f);
}
public ExpressionFileFilter(String expression, Function<File, Object> match) throws ScriptException {
this.filter = new ExpressionFilter(expression);
this.match = match;
2015-07-26 07:52:11 -04:00
}
2015-07-27 08:40:55 -04:00
public ExpressionFilter getExpressionFilter() {
return filter;
}
2015-07-26 07:52:11 -04:00
@Override
public boolean accept(File f) {
try {
return filter.matches(new MediaBindingBean(match.apply(f), f));
2015-07-26 07:52:11 -04:00
} catch (Exception e) {
debug.warning("Filter expression failed: " + e);
2015-07-26 07:52:11 -04:00
}
return false;
2015-07-26 07:52:11 -04:00
}
}