Expand date filter by last execution overwrite

This commit is contained in:
2023-04-03 21:36:11 +02:00
parent 7ee51421e0
commit e3baae55d9
4 changed files with 56 additions and 12 deletions

View File

@@ -11,12 +11,11 @@ import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public abstract class ConfigValidator<FieldType> {
@@ -38,7 +37,9 @@ public abstract class ConfigValidator<FieldType> {
Optional<FieldType> cmdResult = provideDataCmd().apply(cmd, property);
Optional<FieldType> yamlResult = provideDataYaml().apply(yaml, property);
if (cmdResult.isPresent()) {
if (isOverwritingNecessary()){
result = overwriteValue();
} else if (cmdResult.isPresent()) {
result = cmdResult.get();
} else if (yamlResult.isPresent()) {
result = yamlResult.get();
@@ -98,6 +99,14 @@ public abstract class ConfigValidator<FieldType> {
};
}
protected boolean isOverwritingNecessary() {
return false;
}
protected FieldType overwriteValue() {
return null;
}
/**
* Parse input parameter to desired format.
*
@@ -145,5 +154,11 @@ public abstract class ConfigValidator<FieldType> {
&& StringUtils.equalsIgnoreCase(method.getName().replace("get", ""), property.prop().replace("-", ""));
}
protected static Set<Integer> getDependentValidators() {
return Set.of();
}
public static int getWeight() {
return getDependentValidators().stream().sorted().findFirst().orElse(100) - 1;
}
}

View File

@@ -1,22 +1,51 @@
package at.pcgamingfreaks.mkvaudiosubtitlechanger.config.validator;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.config.Config;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ConfigProperty;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
@Slf4j
public class DateValidator extends ConfigValidator<Date> {
private static final Date DEFAULT_DATE = new Date(0);
public DateValidator(ConfigProperty property, boolean required) {
super(property, required, null);
}
@Override
protected boolean isOverwritingNecessary() {
return Config.getInstance().isOnlyNewFiles();
}
@Override
protected Date overwriteValue() {
Properties prop = new Properties();
try {
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("/last-execution.properties"));
return parse(prop.getProperty(Config.getInstance().getLibraryPath().getAbsolutePath()));
} catch (IOException e) {
log.error("Couldn't open last-execution.properties");
return DEFAULT_DATE;
}
}
@Override
Date parse(String value) {
return DateUtils.convert(value); // TODO fix null return value
return DateUtils.convert(value, DEFAULT_DATE);
}
@Override
boolean isValid(Date result) {
return result != null;
return !result.equals(DEFAULT_DATE);
}
protected static Set<Integer> getDependentValidators() {
return Set.of(PathValidator.getWeight(), BooleanValidator.getWeight());
}
}