Code cleanup

This commit is contained in:
0xb00bface 2021-12-21 14:23:46 +01:00
parent 14f9cce1ae
commit 04d8507baa
1 changed files with 10 additions and 22 deletions

View File

@ -1,22 +1,18 @@
package ctbrec.recorder.postprocessing;
import static ctbrec.StringUtil.*;
import static java.util.Optional.*;
import ctbrec.Config;
import ctbrec.Recording;
import ctbrec.StringUtil;
import ctbrec.variableexpansion.ModelVariableExpander;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.*;
import java.util.function.Function;
import ctbrec.Config;
import ctbrec.Recording;
import ctbrec.variableexpansion.ModelVariableExpander;
import static java.util.Optional.ofNullable;
public abstract class AbstractPlaceholderAwarePostProcessor extends AbstractPostProcessor {
@ -24,18 +20,14 @@ public abstract class AbstractPlaceholderAwarePostProcessor extends AbstractPost
Recording rec = ctx.getRecording();
Config config = ctx.getConfig();
Map<String, Function<String, Optional<String>>> placeholderValueSuppliers = new HashMap<>();
ModelVariableExpander modelExpander = new ModelVariableExpander(rec.getModel(), config, ctx.getRecorder());
placeholderValueSuppliers.putAll(modelExpander.getPlaceholderValueSuppliers());
Map<String, Function<String, Optional<String>>> placeholderValueSuppliers = new HashMap<>(modelExpander.getPlaceholderValueSuppliers());
placeholderValueSuppliers.put("recordingNotes", r -> getSanitizedRecordingNotes(rec));
placeholderValueSuppliers.put("fileSuffix", r -> getFileSuffix(rec));
placeholderValueSuppliers.put("recordingsDir", r -> Optional.of(config.getSettings().recordingsDir));
placeholderValueSuppliers.put("absolutePath", r -> Optional.of(rec.getPostProcessedFile().getAbsolutePath()));
placeholderValueSuppliers.put("absoluteParentPath", r -> Optional.of(rec.getPostProcessedFile().getParentFile().getAbsolutePath()));
placeholderValueSuppliers.put("epochSecond", r -> ofNullable(rec.getStartDate()).map(Instant::getEpochSecond).map(l -> Long.toString(l))); // NOSONAR
placeholderValueSuppliers.put("utcDateTime", pattern -> replaceUtcDateTime(rec, pattern));
placeholderValueSuppliers.put("localDateTime", pattern -> replaceLocalDateTime(rec, pattern));
@ -45,7 +37,7 @@ public abstract class AbstractPlaceholderAwarePostProcessor extends AbstractPost
}
private String fillInPlaceHolders(String input, Map<String, Function<String, Optional<String>>> placeholderValueSuppliers) {
boolean somethingReplaced = false;
boolean somethingReplaced;
do {
somethingReplaced = false;
int end = input.indexOf("}");
@ -54,7 +46,7 @@ public abstract class AbstractPlaceholderAwarePostProcessor extends AbstractPost
if (start >= 0) {
String placeholder = input.substring(start, end + 1);
String placeholderName = placeholder.substring(2, placeholder.length() - 1);
String defaultValue = null;
String defaultValue;
String expression = null;
int questionMark = placeholder.indexOf('?');
if (questionMark > 0) {
@ -114,10 +106,6 @@ public abstract class AbstractPlaceholderAwarePostProcessor extends AbstractPost
private Optional<String> getSanitizedRecordingNotes(Recording rec) {
Optional<String> notes = ofNullable(rec.getNote());
if (notes.isPresent()) {
return Optional.of(sanitize(notes.get()));
} else {
return Optional.empty();
}
return notes.map(StringUtil::sanitize);
}
}