|
|
@@ -0,0 +1,255 @@
|
|
|
+// Copyright (c) 2020 UWB & LESP.
|
|
|
+// The UWB & LESP license this file to you under the MIT license.
|
|
|
+
|
|
|
+package cz.senslog.messaging.app;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.LocalTime;
|
|
|
+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.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Lukas Cerny
|
|
|
+ * @version 1.0
|
|
|
+ * @since 1.0
|
|
|
+ */
|
|
|
+public class ConfigProperty {
|
|
|
+
|
|
|
+ /** Path delimiter separates nodes. */
|
|
|
+ private static final String PATH_DELIMITER = ".";
|
|
|
+
|
|
|
+ /** Identifier of path. */
|
|
|
+ private final String id;
|
|
|
+
|
|
|
+ private final String fullId;
|
|
|
+
|
|
|
+ /** Map of properties. */
|
|
|
+ private final Map<String, Object> properties;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Constructor sets new identifier of node.
|
|
|
+ * @param id - identifier of node.
|
|
|
+ */
|
|
|
+ protected ConfigProperty(String id, String fullId) {
|
|
|
+ this.id = id;
|
|
|
+ this.fullId = fullId;
|
|
|
+ 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
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public <T> Optional<T> getOptionalProperty(String name, Class<T> aClass) {
|
|
|
+ Object value = properties.get(name);
|
|
|
+ if (value == null || value.getClass() != aClass) {
|
|
|
+ return empty();
|
|
|
+ }
|
|
|
+
|
|
|
+ return of((T)value);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public <T> List<T> getArrayPropertyOf(String name, Class<T> aClass) {
|
|
|
+ Object value = getProperty(name);
|
|
|
+ List<?> list = value instanceof List ? (List<?>)value : null;
|
|
|
+ if (list == null) {
|
|
|
+ throw new IllegalArgumentException("Cast error " + value.getClass() + " to " + List.class);
|
|
|
+ }
|
|
|
+ List<T> result = new ArrayList<>(list.size());
|
|
|
+ for (Object v : list) {
|
|
|
+ if (v.getClass() == aClass) {
|
|
|
+ result.add((T)v);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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
|
|
|
+ ));
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean getBooleanProperty(String name) {
|
|
|
+ Object value = getProperty(name);
|
|
|
+ if (value instanceof Boolean) {
|
|
|
+ return (Boolean)value;
|
|
|
+ }
|
|
|
+ throw new ClassCastException(format(
|
|
|
+ "Value '%s' can not be cast to Boolean", 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 an Integer. If the property is null, will return default value.
|
|
|
+ * @param name - name of property.
|
|
|
+ * @param defaultValue - default value if the property is null
|
|
|
+ * @return integer value
|
|
|
+ */
|
|
|
+ public Integer getIntegerProperty(String name, int defaultValue) {
|
|
|
+ Object value = getProperty(name);
|
|
|
+ if (value instanceof Integer) {
|
|
|
+ return (Integer)value;
|
|
|
+ }
|
|
|
+
|
|
|
+ return defaultValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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 LocalTime getLocalTimeProperty(String name) {
|
|
|
+ Object object = getProperty(name);
|
|
|
+
|
|
|
+ if (object instanceof LocalTime) {
|
|
|
+ return (LocalTime) object;
|
|
|
+ } else if (object instanceof String) {
|
|
|
+ return LocalTime.parse((String) object);
|
|
|
+ } else {
|
|
|
+ throw new ClassCastException(format(
|
|
|
+ "Property '%s' can not be cast to %s", getNewPropertyId(name), LocalTime.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 ConfigProperty getConfigProperty(String name) {
|
|
|
+ Object property = getProperty(name);
|
|
|
+ ConfigProperty config = new ConfigProperty(name, 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 fullId + PATH_DELIMITER + name;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getId() {
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|