Skip to content

Commit cb463f2

Browse files
committed
Cleaned up the way complex properties work
1 parent 7e16463 commit cb463f2

65 files changed

Lines changed: 1011 additions & 854 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ComplexValueImpl.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
import com.fasterxml.jackson.annotation.JsonAnyGetter;
2121
import com.fasterxml.jackson.annotation.JsonAnySetter;
22+
import de.fraunhofer.iosb.ilt.frostserver.property.EntityPropertyMain;
2223
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
2324
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeComplex;
2425
import java.util.Collections;
2526
import java.util.LinkedHashMap;
2627
import java.util.Map;
28+
import java.util.Objects;
2729
import tools.jackson.core.type.TypeReference;
2830

2931
public class ComplexValueImpl implements ComplexValue<ComplexValueImpl> {
@@ -33,7 +35,7 @@ public class ComplexValueImpl implements ComplexValue<ComplexValueImpl> {
3335
};
3436

3537
private final TypeComplex type;
36-
private final Map<String, Object> properties = new LinkedHashMap<>();
38+
private final Map<Property, Object> properties = new LinkedHashMap<>();
3739

3840
public ComplexValueImpl(TypeComplex type) {
3941
this.type = type;
@@ -46,46 +48,81 @@ public TypeComplex getType() {
4648

4749
@Override
4850
public <P> P getProperty(Property<P> property) {
49-
return (P) properties.get(property.getJsonName());
51+
return (P) properties.get(property);
5052
}
5153

5254
@Override
5355
public <P> ComplexValueImpl setProperty(Property<P> property, P value) {
54-
properties.put(property.getJsonName(), value);
56+
properties.put(property, value);
5557
return this;
5658
}
5759

5860
@JsonAnyGetter
59-
public Map<String, Object> getAllProperties() {
61+
public Map<Property, Object> getAllProperties() {
6062
return Collections.unmodifiableMap(properties);
6163
}
6264

6365
@JsonAnySetter
6466
public void setAnyProperty(String name, Object value) {
65-
properties.put(name, value);
67+
EntityPropertyMain property = type.getEntityProperty(name);
68+
if (property == null) {
69+
throw new IllegalArgumentException("Unknown property " + name + " for type " + type);
70+
}
71+
properties.put(property, value);
6672
}
6773

6874
@Override
6975
public Object getProperty(String name) {
70-
return properties.get(name);
76+
EntityPropertyMain property = type.getEntityProperty(name);
77+
if (property == null) {
78+
throw new IllegalArgumentException("Unknown property " + name + " for type " + type);
79+
}
80+
return properties.get(property);
7181
}
7282

7383
@Override
7484
public ComplexValueImpl setProperty(String name, Object value) {
75-
if (!type.isOpenType()) {
76-
throw new IllegalArgumentException("Can not set custom properties on non-openType " + type);
85+
EntityPropertyMain property = type.getEntityProperty(name);
86+
if (property == null) {
87+
throw new IllegalArgumentException("Unknown property " + name + " for type " + type);
7788
}
78-
properties.put(name, value);
89+
properties.put(property, value);
7990
return this;
8091
}
8192

8293
@Override
8394
public boolean isSetProperty(Property property) {
84-
return properties.containsKey(property.getJsonName());
95+
return properties.containsKey(property);
8596
}
8697

8798
public static TypeComplex.Instantiator createFor(TypeComplex type) {
8899
return t -> new ComplexValueImpl(t);
89100
}
90101

102+
@Override
103+
public int hashCode() {
104+
int hash = 7;
105+
hash = 97 * hash + Objects.hashCode(this.type);
106+
hash = 97 * hash + Objects.hashCode(this.properties);
107+
return hash;
108+
}
109+
110+
@Override
111+
public boolean equals(Object obj) {
112+
if (this == obj) {
113+
return true;
114+
}
115+
if (obj == null) {
116+
return false;
117+
}
118+
if (getClass() != obj.getClass()) {
119+
return false;
120+
}
121+
final ComplexValueImpl other = (ComplexValueImpl) obj;
122+
if (!Objects.equals(this.type, other.type)) {
123+
return false;
124+
}
125+
return Objects.equals(this.properties, other.properties);
126+
}
127+
91128
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (C) 2024 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131
3+
* Karlsruhe, Germany.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package de.fraunhofer.iosb.ilt.frostserver.model;
19+
20+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
21+
import com.fasterxml.jackson.annotation.JsonAnySetter;
22+
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
23+
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeComplex;
24+
import java.util.Collections;
25+
import java.util.LinkedHashMap;
26+
import java.util.Map;
27+
import java.util.Objects;
28+
import tools.jackson.core.type.TypeReference;
29+
30+
public class ComplexValueImplOpen implements ComplexValue<ComplexValueImplOpen> {
31+
32+
public static final TypeReference<ComplexValueImpl> TYPE_REFERENCE = new TypeReference<ComplexValueImpl>() {
33+
// Empty by design.
34+
};
35+
36+
private final TypeComplex type;
37+
private final Map<String, Object> properties = new LinkedHashMap<>();
38+
39+
public ComplexValueImplOpen(TypeComplex type) {
40+
this.type = type;
41+
}
42+
43+
@Override
44+
public TypeComplex getType() {
45+
return type;
46+
}
47+
48+
@Override
49+
public <P> P getProperty(Property<P> property) {
50+
return (P) properties.get(property.getJsonName());
51+
}
52+
53+
@Override
54+
public <P> ComplexValueImplOpen setProperty(Property<P> property, P value) {
55+
properties.put(property.getJsonName(), value);
56+
return this;
57+
}
58+
59+
@JsonAnyGetter
60+
public Map<String, Object> getAllProperties() {
61+
return Collections.unmodifiableMap(properties);
62+
}
63+
64+
@JsonAnySetter
65+
public void setAnyProperty(String name, Object value) {
66+
properties.put(name, value);
67+
}
68+
69+
@Override
70+
public Object getProperty(String name) {
71+
return properties.get(name);
72+
}
73+
74+
@Override
75+
public ComplexValueImplOpen setProperty(String name, Object value) {
76+
if (!type.isOpenType()) {
77+
throw new IllegalArgumentException("Can not set custom properties on non-openType " + type);
78+
}
79+
properties.put(name, value);
80+
return this;
81+
}
82+
83+
@Override
84+
public boolean isSetProperty(Property property) {
85+
return properties.containsKey(property.getJsonName());
86+
}
87+
88+
public static TypeComplex.Instantiator createFor(TypeComplex type) {
89+
return t -> new ComplexValueImpl(t);
90+
}
91+
92+
@Override
93+
public int hashCode() {
94+
int hash = 7;
95+
hash = 97 * hash + Objects.hashCode(this.type);
96+
hash = 97 * hash + Objects.hashCode(this.properties);
97+
return hash;
98+
}
99+
100+
@Override
101+
public boolean equals(Object obj) {
102+
if (this == obj) {
103+
return true;
104+
}
105+
if (obj == null) {
106+
return false;
107+
}
108+
if (getClass() != obj.getClass()) {
109+
return false;
110+
}
111+
final ComplexValueImplOpen other = (ComplexValueImplOpen) obj;
112+
if (!Objects.equals(this.type, other.type)) {
113+
return false;
114+
}
115+
return Objects.equals(this.properties, other.properties);
116+
}
117+
118+
}

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ext/TimeInterval.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ public TimeInterval setProperty(Property property, Object value) {
166166
moment = m;
167167
} else if (value instanceof Instant i) {
168168
moment = Moment.from(i);
169+
} else if (value instanceof TimeInstant i) {
170+
moment = i.getDateTime();
169171
} else {
170172
throw new IllegalArgumentException("TimeInterval only accepts Moment or Instant, not " + value.getClass().getName());
171173
}

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ext/TimeValue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public class TimeValue implements TimeObject, ComplexValue<TimeValue> {
3636

3737
public static EntityPropertyMain<TimeInstant> EP_START_TIME = TypeComplex.EP_START_TIME;
38-
public static EntityPropertyMain<TimeInstant> EP_END_TIME = TypeComplex.EP_INTERVAL_END_TIME;
38+
public static EntityPropertyMain<TimeInstant> EP_END_TIME = TypeComplex.EP_VALUE_END_TIME;
3939

4040
private TimeInstant instant;
4141
private TimeInterval interval;
@@ -147,6 +147,8 @@ public TimeValue setProperty(Property property, Object value) {
147147
moment = m;
148148
} else if (value instanceof Instant i) {
149149
moment = Moment.from(i);
150+
} else if (value instanceof TimeInstant i) {
151+
moment = i.getDateTime();
150152
} else {
151153
throw new IllegalArgumentException("TimeInterval only accepts Moment or Instant, not " + value.getClass().getName());
152154
}

FROST-Server.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ext/TypeReferencesHelper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ public class TypeReferencesHelper {
7070
public static final TypeReference<Long> TYPE_REFERENCE_LONG = new TypeReference<Long>() {
7171
// Empty on purpose.
7272
};
73-
public static final TypeReference<List<UnitOfMeasurement>> TYPE_REFERENCE_LIST_UOM = new TypeReference<List<UnitOfMeasurement>>() {
74-
// Empty on purpose.
75-
};
7673
public static final TypeReference<Map<String, Object>> TYPE_REFERENCE_MAP = new TypeReference<Map<String, Object>>() {
7774
// Empty on purpose.
7875
};
@@ -97,9 +94,6 @@ public class TypeReferencesHelper {
9794
public static final TypeReference<TimeValue> TYPE_REFERENCE_TIMEVALUE = new TypeReference<TimeValue>() {
9895
// Empty on purpose.
9996
};
100-
public static final TypeReference<UnitOfMeasurement> TYPE_REFERENCE_UOM = new TypeReference<UnitOfMeasurement>() {
101-
// Empty on purpose.
102-
};
10397
public static final TypeReference<UUID> TYPE_REFERENCE_UUID = new TypeReference<UUID>() {
10498
// Empty on purpose.
10599
};

0 commit comments

Comments
 (0)