MATSIM
StringDoubleMapConverter.java
Go to the documentation of this file.
1 package org.matsim.utils.objectattributes.attributeconverters;
2 
3 import java.util.Map;
4 
5 import org.apache.logging.log4j.LogManager;
6 import org.apache.logging.log4j.Logger;
8 import com.fasterxml.jackson.core.JsonProcessingException;
9 import com.fasterxml.jackson.databind.ObjectMapper;
10 import com.fasterxml.jackson.databind.type.MapType;
11 import com.fasterxml.jackson.databind.type.TypeFactory;
13 
14 public class StringDoubleMapConverter implements AttributeConverter<StringDoubleMap> {
15  private static final Logger LOG = LogManager.getLogger(StringDoubleMapConverter.class);
16  private static final ObjectMapper mapper = new ObjectMapper();
17  private static final MapType mapType = TypeFactory.defaultInstance().constructMapType(Map.class, String.class,
18  Double.class);
19 
20  @Override
21  public StringDoubleMap convert(String value) {
22  try {
23  return new StringDoubleMap(mapper.readValue(value, mapType));
24  } catch (JsonProcessingException e) {
25  throw new RuntimeException(e);
26  }
27  }
28 
29  @Override
30  public String convertToString(Object o) {
31  if (!(o instanceof StringDoubleMap)) {
32  LOG.error("Object is not of type StringDoubleMap: {}", o.getClass());
33  return null;
34  }
35 
37  }
38 }