20: Add AlarmVariableResolver class
This commit is contained in:
@@ -0,0 +1,29 @@
|
|||||||
|
package com.r35157.jupiterperpsalarm.impl.ref;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public final class AlarmVariableResolver {
|
||||||
|
|
||||||
|
public AlarmVariableResolver(Map<String, String> variables) {
|
||||||
|
this.variables = Objects.requireNonNull(variables, "variables");
|
||||||
|
}
|
||||||
|
|
||||||
|
public String resolve(String text) {
|
||||||
|
Objects.requireNonNull(text, "text");
|
||||||
|
|
||||||
|
String resolvedText = text;
|
||||||
|
|
||||||
|
for (Map.Entry<String, String> entry : variables.entrySet()) {
|
||||||
|
resolvedText = resolvedText.replace(entry.getKey(), entry.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resolvedText.contains("{{") || resolvedText.contains("}}")) {
|
||||||
|
throw new IllegalArgumentException("Unresolved variable in text: " + text);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolvedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Map<String, String> variables;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user