Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Minor: Updated value converters

Rich Turnerd3164646919d
diff --git a/model/src/main/java/org/openremote/model/value/Values.java b/model/src/main/java/org/openremote/model/value/Values.java
index 7eb6d2cf3..52e6de244 100644
--- a/model/src/main/java/org/openremote/model/value/Values.java
+++ b/model/src/main/java/org/openremote/model/value/Values.java
@@ -18,6 +18,7 @@ package org.openremote.model.value;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.google.gwt.core.shared.GwtIncompatible;
+import com.google.inject.internal.util.$FinalizableWeakReference;
import jsinterop.annotations.JsIgnore;
import jsinterop.annotations.JsMethod;
import jsinterop.base.Any;
@@ -26,6 +27,7 @@ import org.openremote.model.attribute.MetaItem;
import org.openremote.model.attribute.MetaItemDescriptor;
import org.openremote.model.value.impl.ValueFactoryImpl;
+import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
@@ -320,18 +322,11 @@ public class Values {
return Optional.ofNullable(outputValue);
}
- @SuppressWarnings("unchecked")
@JsIgnore
@GwtIncompatible
public static <T extends Value> Optional<T> convertToValue(Object object, ObjectWriter writer) {
- if (object == null || writer == null) {
- return Optional.empty();
- }
-
try {
- Value v;
- v = parse(writer.writeValueAsString(object)).orElse(null);
- return Optional.ofNullable((T)v);
+ return Optional.of(convertToValueOrThrow(object, writer));
} catch (Exception ignored) {
}
@@ -340,17 +335,35 @@ public class Values {
@JsIgnore
@GwtIncompatible
- public static <T> Optional<T> convertFromValue(Value value, Class<T> clazz, ObjectReader reader) {
- if (value == null || reader == null) {
- return Optional.empty();
+ public static <T extends Value> T convertToValueOrThrow(Object object, ObjectWriter writer) throws IOException {
+ if (object == null || writer == null) {
+ throw new IllegalArgumentException("Value and writer must be defined");
}
+ Value v;
+ v = parse(writer.writeValueAsString(object)).orElse(null);
+ return (T)v;
+ }
+
+ @JsIgnore
+ @GwtIncompatible
+ public static <T> Optional<T> convertFromValue(Value value, Class<T> clazz, ObjectReader reader) {
try {
- String str = value.toJson();
- return Optional.of(reader.forType(clazz).readValue(str));
+ return Optional.of(convertFromValueOrThrow(value, clazz, reader));
} catch (Exception ignored) {
}
return Optional.empty();
}
+
+ @JsIgnore
+ @GwtIncompatible
+ public static <T> T convertFromValueOrThrow(Value value, Class<T> clazz, ObjectReader reader) throws IOException {
+ if (value == null || clazz == null || reader == null) {
+ throw new IllegalArgumentException("Value, class and reader must be defined");
+ }
+
+ String str = value.toJson();
+ return reader.forType(clazz).readValue(str);
+ }
}