class FileFilterBuilder {

  public static FileFilter build(String filterExpression, String filterImpl) {
    FileFilter result = null;
    try {
      if (isEmpty(filterExpression)) {
        result = (file) -> true;
      } else {
        Class filterClass = Class.forName(filterImpl);
        for (Map.Entry<Class, Function<String, Object>> converter : SUPPORTED_CNST_ARGS.entrySet()) {
          if (hasConstructor(converter.getKey(), filterClass)) {
            // It has a constructor, we try it, bearing in mind that the converter
            // may fail to convert so
            // If someone passes in "a-string" with SizeFileFilter; then we will
            // fail to convert to a long and throw an exception at this point.
            Object constructorArg = converter.getValue().apply(filterExpression);
            Constructor constructor = filterClass.getDeclaredConstructor(new Class[] {converter.getKey()});
            result = (FileFilter) constructor.newInstance(new Object[] {constructorArg});
            break;
          }
        }
      }
      if (result == null) {
        throw new InstantiationException(filterImpl + " could not be created with [" + filterExpression + "]");
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return result;
  }

}