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