Skip to content

Commit

Permalink
Refactored Property Types and how they are (de)serialised
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Mar 15, 2024
1 parent 74fb618 commit 147552f
Show file tree
Hide file tree
Showing 30 changed files with 763 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* 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.util;
package de.fraunhofer.iosb.ilt.frostserver.model;

import de.fraunhofer.iosb.ilt.frostserver.property.ComplexValue;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -85,7 +84,7 @@ private static Object getFrom(final Object mapOrList, final List<String> path) {
if (currentEntry instanceof Map map) {
currentEntry = map.get(key);
} else if (currentEntry instanceof ComplexValue cv) {
currentEntry = cv.get(key);
currentEntry = cv.getProperty(key);
} else if (currentEntry instanceof List list) {
try {
currentEntry = list.get(Integer.parseInt(key));
Expand Down
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);

}
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);
}

}
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);
}

}
Loading

0 comments on commit 147552f

Please sign in to comment.