-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored Property Types and how they are (de)serialised
- Loading branch information
Showing
30 changed files
with
763 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...erver.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ComplexValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131 | ||
* Karlsruhe, Germany. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package de.fraunhofer.iosb.ilt.frostserver.model; | ||
|
||
import de.fraunhofer.iosb.ilt.frostserver.property.Property; | ||
|
||
/** | ||
* Interface that values of complex properties should implement to make it | ||
* easier to access sub-properties. | ||
* | ||
* @param <S> The type of the complex value (for fluent API) | ||
*/ | ||
public interface ComplexValue<S extends ComplexValue<S>> { | ||
|
||
/** | ||
* Get the value of the given property. | ||
* | ||
* @param <P> The type of the property and value. | ||
* @param property The property to get the value of. | ||
* @return the value of the requested property. | ||
*/ | ||
public <P> P getProperty(Property<P> property); | ||
|
||
/** | ||
* Set the given property to the given value. | ||
* | ||
* @param <P> The type of the property. | ||
* @param property The property to set. | ||
* @param value The value to set the property to. | ||
* @return this. | ||
*/ | ||
public <P> S setProperty(Property<P> property, P value); | ||
|
||
/** | ||
* Get the custom property with the given name. Only valid for ComplexTypes | ||
* that are classed as openType, returns null for non-openTypes. | ||
* | ||
* @param name The name of the custom property to fetch. | ||
* @return The value of the custom property. | ||
*/ | ||
public Object getProperty(String name); | ||
|
||
/** | ||
* Set the custom property with the given name to the given value. Only | ||
* valid for ComplexTypes that are classed as openType. | ||
* | ||
* @param name The name of the custom property to set. | ||
* @param value The value of the custom property to set. | ||
* @return this. | ||
*/ | ||
public S setProperty(String name, Object value); | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...r.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ComplexValueImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131 | ||
* Karlsruhe, Germany. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package de.fraunhofer.iosb.ilt.frostserver.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import de.fraunhofer.iosb.ilt.frostserver.property.Property; | ||
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeComplex; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public class ComplexValueImpl implements ComplexValue<ComplexValueImpl> { | ||
|
||
public static final TypeReference<ComplexValueImpl> TYPE_REFERENCE = new TypeReference<ComplexValueImpl>() { | ||
// Empty by design. | ||
}; | ||
|
||
private final TypeComplex type; | ||
private final Map<String, Object> properties = new LinkedHashMap<>(); | ||
|
||
public ComplexValueImpl(TypeComplex type) { | ||
this.type = type; | ||
} | ||
|
||
@JsonIgnore | ||
public TypeComplex getType() { | ||
return type; | ||
} | ||
|
||
@Override | ||
public <P> P getProperty(Property<P> property) { | ||
return (P) properties.get(property.getJsonName()); | ||
} | ||
|
||
@Override | ||
public <P> ComplexValueImpl setProperty(Property<P> property, P value) { | ||
properties.put(property.getJsonName(), value); | ||
return this; | ||
} | ||
|
||
@JsonAnyGetter | ||
public Map<String, Object> getAllProperties() { | ||
return Collections.unmodifiableMap(properties); | ||
} | ||
|
||
@JsonAnySetter | ||
public void setAnyProperty(String name, Object value) { | ||
properties.put(name, value); | ||
} | ||
|
||
@Override | ||
public Object getProperty(String name) { | ||
return properties.get(name); | ||
} | ||
|
||
@Override | ||
public ComplexValueImpl setProperty(String name, Object value) { | ||
if (!type.isOpenType()) { | ||
throw new IllegalArgumentException("Can not set custom properties on non-openType " + type); | ||
} | ||
properties.put(name, value); | ||
return this; | ||
} | ||
|
||
public static TypeComplex.Instantiator createFor(TypeComplex type) { | ||
return () -> new ComplexValueImpl(type); | ||
} | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
...erver.Core.Model/src/main/java/de/fraunhofer/iosb/ilt/frostserver/model/ext/MapValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright (C) 2023 Fraunhofer Institut IOSB, Fraunhoferstr. 1, D 76131 | ||
* Karlsruhe, Germany. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package de.fraunhofer.iosb.ilt.frostserver.model.ext; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import de.fraunhofer.iosb.ilt.frostserver.model.ComplexValue; | ||
import de.fraunhofer.iosb.ilt.frostserver.property.Property; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* The complex value for an open type. | ||
*/ | ||
public class MapValue implements ComplexValue<MapValue> { | ||
|
||
private final Map<String, Object> content; | ||
|
||
public MapValue() { | ||
this.content = new LinkedHashMap<>(); | ||
} | ||
|
||
public MapValue(Map<String, Object> content) { | ||
this.content = content; | ||
} | ||
|
||
@JsonIgnore | ||
public boolean isEmpty() { | ||
return content.isEmpty(); | ||
} | ||
|
||
@JsonAnyGetter | ||
public Map<String, Object> getContent() { | ||
return content; | ||
} | ||
|
||
public Set<Map.Entry<String, Object>> entrySet() { | ||
return content.entrySet(); | ||
} | ||
|
||
@JsonIgnore | ||
public Object get(String name) { | ||
return content.get(name); | ||
} | ||
|
||
public boolean containsKey(String name) { | ||
return content.containsKey(name); | ||
} | ||
|
||
@JsonAnySetter | ||
public MapValue put(String name, Object value) { | ||
content.put(name, value); | ||
return this; | ||
} | ||
|
||
@JsonIgnore | ||
@Override | ||
public <P> P getProperty(Property<P> property) { | ||
return (P) content.get(property.getJsonName()); | ||
} | ||
|
||
@JsonIgnore | ||
@Override | ||
public <P> MapValue setProperty(Property<P> property, P value) { | ||
content.put(property.getJsonName(), value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public Object getProperty(String name) { | ||
return content.get(name); | ||
} | ||
|
||
@Override | ||
public MapValue setProperty(String name, Object value) { | ||
content.put(name, value); | ||
return this; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj instanceof MapValue o) { | ||
return content.equals(o.content); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int hash = 5; | ||
return 79 * hash + Objects.hashCode(content); | ||
} | ||
|
||
} |
Oops, something went wrong.