|
|
@@ -0,0 +1,189 @@
|
|
|
+// Copyright (c) 2020 UWB & LESP.
|
|
|
+// The UWB & LESP license this file to you under the MIT license.
|
|
|
+
|
|
|
+package cz.senslog.watchdog.config;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.time.ZonedDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.time.format.DateTimeFormatterBuilder;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import static java.lang.String.format;
|
|
|
+import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
|
|
+import static java.util.Optional.ofNullable;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Lukas Cerny
|
|
|
+ * @version 1.0
|
|
|
+ * @since 1.0
|
|
|
+ */
|
|
|
+public class PropertyConfig {
|
|
|
+
|
|
|
+ /** Path delimiter separates nodes. */
|
|
|
+ private static final String PATH_DELIMITER = ".";
|
|
|
+
|
|
|
+ /** Identifier of path. */
|
|
|
+ private final String id;
|
|
|
+
|
|
|
+ /** Map of properties. */
|
|
|
+ private final Map<String, Object> properties;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Constructor sets new identifier of node.
|
|
|
+ * @param id - identifier of node.
|
|
|
+ */
|
|
|
+ protected PropertyConfig(String id) {
|
|
|
+ this.id = id;
|
|
|
+ this.properties = new HashMap<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Adds new property to properties.
|
|
|
+ * @param name - name of new property.
|
|
|
+ */
|
|
|
+ public void setProperty(String name, Object value) {
|
|
|
+ properties.put(name, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns value. It could be anything.
|
|
|
+ * @param name - name of property.
|
|
|
+ * @return object of value.
|
|
|
+ */
|
|
|
+ public Object getProperty(String name) {
|
|
|
+ if (properties.containsKey(name)) {
|
|
|
+ return properties.get(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new IllegalArgumentException(format(
|
|
|
+ "Property '%s' does not exist.", getNewPropertyId(name))
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Checks if property key is presents in properties.
|
|
|
+ * @param name - name of property
|
|
|
+ * @return boolean
|
|
|
+ */
|
|
|
+ public boolean containsProperty(String name) {
|
|
|
+ return properties.containsKey(name);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns optional value. It could be anything.
|
|
|
+ * @param name - name of property.
|
|
|
+ * @return optional object
|
|
|
+ */
|
|
|
+ public Optional<Object> getOptionalProperty(String name) {
|
|
|
+ return ofNullable(properties.get(name));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns property as a String.
|
|
|
+ * @param name - name of property.
|
|
|
+ * @return string value.
|
|
|
+ */
|
|
|
+ public String getStringProperty(String name) {
|
|
|
+ Object value = getProperty(name);
|
|
|
+ if (value instanceof String) {
|
|
|
+ return (String)value;
|
|
|
+ }
|
|
|
+ throw new ClassCastException(format(
|
|
|
+ "Value '%s' can not be cast to String", value
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns property as an Integer.
|
|
|
+ * @param name - name of property.
|
|
|
+ * @return integer value.
|
|
|
+ */
|
|
|
+ public Integer getIntegerProperty(String name) {
|
|
|
+ Object value = getProperty(name);
|
|
|
+ if (value instanceof Integer) {
|
|
|
+ return (Integer)value;
|
|
|
+ }
|
|
|
+ throw new ClassCastException(format(
|
|
|
+ "Value '%s' can not be cast to Integer", value
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns property as a LocalDateTime.
|
|
|
+ * @param name - name of property.
|
|
|
+ * @return localDateTime value.
|
|
|
+ */
|
|
|
+ public LocalDateTime getLocalDateTimeProperty(String name) {
|
|
|
+ Object object = getProperty(name);
|
|
|
+
|
|
|
+ if (object instanceof LocalDateTime) {
|
|
|
+ return (LocalDateTime) object;
|
|
|
+ } else if (object instanceof Date) {
|
|
|
+ Date date = (Date) object;
|
|
|
+ return date.toInstant().atZone(ZoneOffset.systemDefault()).toLocalDateTime();
|
|
|
+ } else if (object instanceof String) {
|
|
|
+ return LocalDateTime.parse((String)object, DateTimeFormatter.ISO_DATE_TIME);
|
|
|
+ } else {
|
|
|
+ throw new ClassCastException(format(
|
|
|
+ "Property '%s' can not be cast to %s", getNewPropertyId(name), LocalDateTime.class)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public ZonedDateTime getZonedDateTimeProperty(String name) {
|
|
|
+ Object object = getProperty(name);
|
|
|
+
|
|
|
+ if (object instanceof ZonedDateTime) {
|
|
|
+ return (ZonedDateTime)object;
|
|
|
+ } else if (object instanceof String) {
|
|
|
+ final DateTimeFormatter formatter = new DateTimeFormatterBuilder()
|
|
|
+ .append(ISO_LOCAL_DATE_TIME)
|
|
|
+ .optionalStart()
|
|
|
+ .appendLiteral('[')
|
|
|
+ .parseCaseSensitive()
|
|
|
+ .appendZoneRegionId()
|
|
|
+ .appendLiteral(']')
|
|
|
+ .toFormatter();
|
|
|
+ return ZonedDateTime.parse((String)object, formatter);
|
|
|
+ } else {
|
|
|
+ throw new ClassCastException(format(
|
|
|
+ "Property '%s' can not be cast to %s", getNewPropertyId(name), ZonedDateTime.class)
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns new node of configuration.
|
|
|
+ * @param name - name of property.
|
|
|
+ * @return node of configuration.
|
|
|
+ */
|
|
|
+ public PropertyConfig getPropertyConfig(String name) {
|
|
|
+ Object property = getProperty(name);
|
|
|
+ PropertyConfig config = new PropertyConfig(getNewPropertyId(name));
|
|
|
+
|
|
|
+ if (property instanceof Map) {
|
|
|
+ Map<?, ?> properties = (Map<?, ?>) property;
|
|
|
+ for (Map.Entry<?, ?> propertyEntry : properties.entrySet()) {
|
|
|
+ Object propertyName = propertyEntry.getKey();
|
|
|
+ config.setProperty(propertyName.toString(), propertyEntry.getValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Set<String> getAttributes() {
|
|
|
+ return properties.keySet();
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getNewPropertyId(String name) {
|
|
|
+ return id + PATH_DELIMITER + name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getId() {
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+}
|