-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3142 from steffenaxer/StringDoubleMap
Introduce StringDoubleMap and StringDoubleMapConverter
- Loading branch information
Showing
4 changed files
with
68 additions
and
0 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
14 changes: 14 additions & 0 deletions
14
.../src/main/java/org/matsim/utils/objectattributes/attributeconverters/StringDoubleMap.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,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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
.../java/org/matsim/utils/objectattributes/attributeconverters/StringDoubleMapConverter.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,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); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...a/org/matsim/utils/objectattributes/attributeconverters/StringDoubleMapConverterTest.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,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); | ||
} | ||
} |