22 package org.matsim.utils.objectattributes;
24 import com.google.inject.Inject;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
42 private final Map<String, AttributeConverter<?>>
converters =
new HashMap<>();
58 this.converters.put(Long.class.getName(),
new LongConverter());
71 public Object
convert(String className, String value) {
73 return converter == null ? null : converter.
convert(value);
82 if (converters.containsKey(className))
return converters.get(className);
84 Class<?> clazz = Class.forName(className);
88 converters.put(className, converter);
92 if(Map.class.isAssignableFrom(clazz))
return this.converters.get(Map.class.getName());
93 if(Collection.class.isAssignableFrom(clazz))
return this.converters.get(Collection.class.getName());
95 if (missingConverters.add(className)) {
96 log.warn(
"No AttributeConverter found for class " + className +
". Not all attribute values can be converted.");
99 catch (ClassNotFoundException e) {
100 if (missingConverters.add(className)) {
101 log.warn(
"No AttributeConverter found for class " + className +
", and class is not on classpath. Not all attribute values can be converted.");
116 Map<Object, Object> map = ((Map<Object, Object>) o);
117 if (! map.isEmpty()){
118 Map.Entry firstEntry = map.entrySet().iterator().next();
119 if(! (firstEntry.getKey() instanceof String && firstEntry.getValue() instanceof String) )
return null;
123 Collection collection = ((Collection) o);
124 if(! collection.isEmpty()){
125 if(! ( collection.iterator().next() instanceof String) )
return null;
142 return this.converters.put(clazz.getName(), converter);
158 return this.converters.remove(clazz.getName());
AttributeConverter getConverter(String className)
T convert(final String value)
ObjectAttributesConverter(final Map< Class<?>, AttributeConverter<?>> converters)
ObjectAttributesConverter()
String convertToString(final Object o)
Map< String, AttributeConverter<?> > getConverters()
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
final Map< String, AttributeConverter<?> > converters
final Set< String > missingConverters
AttributeConverter removeAttributeConverter(final Class<?> clazz)
AttributeConverter putAttributeConverter(final Class<?> clazz, final AttributeConverter converter)
Object convert(String className, String value)
String convertToString(Object o)