diff options
Diffstat (limited to 'src/org/traccar/command/StringCommandTemplate.java')
-rw-r--r-- | src/org/traccar/command/StringCommandTemplate.java | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/src/org/traccar/command/StringCommandTemplate.java b/src/org/traccar/command/StringCommandTemplate.java deleted file mode 100644 index 3f326e534..000000000 --- a/src/org/traccar/command/StringCommandTemplate.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.traccar.command; - -import org.traccar.database.ActiveDevice; - -import java.util.HashMap; -import java.util.Map; - -public class StringCommandTemplate<T extends GpsCommand> implements CommandTemplate<T> { - - private String messageTemplate; - private Map<Class<?>, CommandValueConversion> converters = new HashMap<>(); - - public StringCommandTemplate(String template, Object... replacements) { - this.messageTemplate = String.format(template, replacements); - } - - @Override - public Object applyTo(ActiveDevice activeDevice, T command) { - String currentMessage = messageTemplate; - currentMessage = this.replace(currentMessage, GpsCommand.UNIQUE_ID, activeDevice.getUniqueId()); - - Map<String, Object> replacements = command.getReplacements(); - - for (Map.Entry<String, Object> entry : replacements.entrySet()) { - currentMessage = this.replace(currentMessage, entry.getKey(), entry.getValue()); - } - - return currentMessage; - } - - public CommandTemplate addConverter(Class<?> type, CommandValueConversion converter) { - converters.put(type, converter); - return this; - } - - protected CommandValueConversion getConverter(Class<?> type) { - return converters.containsKey(type) ? converters.get(type) : idConverter(); - } - - private CommandValueConversion idConverter() { - return new CommandValueConversion() { - @Override - public String convert(Object value) { - return value.toString(); - } - }; - } - - private String replace(String currentMessage, String key, Object value) { - String replacementValue = getConverter(value.getClass()).convert(value); - return currentMessage.replace("[" + key + "]", replacementValue); - } - -} |