Skip to content

Commit

Permalink
Merge pull request #3142 from steffenaxer/StringDoubleMap
Browse files Browse the repository at this point in the history
Introduce StringDoubleMap and StringDoubleMapConverter
  • Loading branch information
steffenaxer authored Mar 6, 2024
2 parents c161839 + 1b3946c commit be4ad19
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public ObjectAttributesConverter() {
this.converters.put(PersonVehicles.class.getName(), new PersonVehiclesAttributeConverter());
this.converters.put(DisallowedNextLinks.class.getName(), new DisallowedNextLinksAttributeConverter());
this.converters.put(PersonVehicleTypes.class.getName(), new PersonVehicleTypesAttributeConverter());
this.converters.put(StringDoubleMap.class.getName(), new StringDoubleMapConverter());
}

//this is for reading
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.matsim.utils.objectattributes.attributeconverters;

import java.util.LinkedHashMap;
import java.util.Map;

public class StringDoubleMap extends LinkedHashMap<String, Double> {
public StringDoubleMap() {
super();
}

public StringDoubleMap(Map<String,Double> map) {
super(map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.matsim.utils.objectattributes.attributeconverters;

import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.utils.objectattributes.AttributeConverter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.databind.type.TypeFactory;

public class StringDoubleMapConverter implements AttributeConverter<StringDoubleMap> {
private static final Logger LOG = LogManager.getLogger(StringDoubleMapConverter.class);
private static final ObjectMapper mapper = new ObjectMapper();
private static final MapType mapType = TypeFactory.defaultInstance().constructMapType(Map.class, String.class,
Double.class);

@Override
public StringDoubleMap convert(String value) {
try {
return new StringDoubleMap(mapper.readValue(value, mapType));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

@Override
public String convertToString(Object o) {
if (!(o instanceof StringDoubleMap)) {
LOG.error("Object is not of type StringDoubleMap: {}", o.getClass());
return null;
}

return new StringStringMapConverter().convertToString(o);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.matsim.utils.objectattributes.attributeconverters;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class StringDoubleMapConverterTest {

@Test
void test() {
var expectedString = "{\"a\":0.1,\"b\":0.2}";
var converter = new StringDoubleMapConverter();
var serializedString = converter.convertToString(converter.convert(expectedString));
assertEquals(expectedString, serializedString);
}
}

0 comments on commit be4ad19

Please sign in to comment.