/
SimulatorElement.java
138 lines (119 loc) · 4.45 KB
/
SimulatorElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright 2016, OpenRemote Inc.
*
* See the CONTRIBUTORS.txt file in the distribution for a
* full listing of individual contributors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openremote.model.simulator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.openremote.model.ValidationFailure;
import org.openremote.model.ValueHolder;
import org.openremote.model.attribute.AttributeRef;
import org.openremote.model.attribute.AttributeValueDescriptor;
import org.openremote.model.attribute.AttributeValueDescriptorImpl;
import org.openremote.model.attribute.AttributeValueType;
import org.openremote.model.simulator.element.ColorSimulatorElement;
import org.openremote.model.simulator.element.NumberSimulatorElement;
import org.openremote.model.simulator.element.SwitchSimulatorElement;
import org.openremote.model.value.ArrayValue;
import org.openremote.model.value.ObjectValue;
import org.openremote.model.value.Value;
import org.openremote.model.value.Values;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@JsonSubTypes({
// Events used on client and server (serializable)
@JsonSubTypes.Type(value = NumberSimulatorElement.class, name = NumberSimulatorElement.ELEMENT_NAME),
@JsonSubTypes.Type(value = SwitchSimulatorElement.class, name = SwitchSimulatorElement.ELEMENT_NAME),
@JsonSubTypes.Type(value = ColorSimulatorElement.class, name = ColorSimulatorElement.ELEMENT_NAME),
})
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "elementType"
)
public abstract class SimulatorElement implements ValueHolder {
public AttributeRef attributeRef;
public AttributeValueDescriptor expectedType;
// TODO GWT jackson doesn't like fields with the same name as getters in the hierarchy
@JsonProperty("value")
public Value elementValue = null;
protected SimulatorElement() {
}
public SimulatorElement(AttributeRef attributeRef, AttributeValueType expectedType) {
this.attributeRef = attributeRef;
this.expectedType = expectedType;
}
public AttributeRef getAttributeRef() {
return attributeRef;
}
public AttributeValueDescriptor getExpectedType() {
return expectedType;
}
@Override
public Optional<Value> getValue() {
return Optional.ofNullable(elementValue);
}
@Override
public void setValue(Value value) {
this.elementValue = value;
}
@Override
public void clearValue() {
this.elementValue = null;
}
@Override
public Optional<String> getValueAsString() {
return Values.getString(elementValue);
}
@Override
public Optional<Double> getValueAsNumber() {
return Values.getNumber(elementValue);
}
@Override
public Optional<Integer> getValueAsInteger() {
return Values.getNumber(elementValue).map(Double::intValue);
}
@Override
public Optional<Boolean> getValueAsBoolean() {
return Values.getBoolean(elementValue);
}
@Override
public Optional<ObjectValue> getValueAsObject() {
return Values.getObject(elementValue);
}
@Override
public Optional<ArrayValue> getValueAsArray() {
return Values.getArray(elementValue);
}
@Override
public List<ValidationFailure> getValidationFailures() {
List<ValidationFailure> failures = new ArrayList<>();
if (elementValue != null) {
expectedType.getValidator().flatMap(v -> v.apply(elementValue)).ifPresent(failures::add);
}
return failures;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"value=" + elementValue +
"}";
}
}