MATSIM
Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
org.matsim.utils.objectattributes.ObjectAttributesConverter Class Reference

Public Member Functions

 ObjectAttributesConverter (final Map< Class<?>, AttributeConverter<?>> converters)
 
 ObjectAttributesConverter ()
 
Object convert (String className, String value)
 
Map< String, AttributeConverter<?> > getConverters ()
 
String convertToString (Object o)
 
AttributeConverter putAttributeConverter (final Class<?> clazz, final AttributeConverter converter)
 
void putAttributeConverters (final Map< Class<?>, AttributeConverter<?>> converters)
 
AttributeConverter removeAttributeConverter (final Class<?> clazz)
 

Private Member Functions

AttributeConverter getConverter (String className)
 

Private Attributes

final Map< String, AttributeConverter<?> > converters = new HashMap<>()
 
final Set< String > missingConverters = new HashSet<>()
 

Static Private Attributes

static final Logger log = LogManager.getLogger(ObjectAttributesConverter.class)
 

Detailed Description

Object that converts arbitrary objects to and from strings based on the logic defined by {}s

Author
thibautd

Definition at line 40 of file ObjectAttributesConverter.java.

Constructor & Destructor Documentation

◆ ObjectAttributesConverter() [1/2]

org.matsim.utils.objectattributes.ObjectAttributesConverter.ObjectAttributesConverter ( final Map< Class<?>, AttributeConverter<?>>  converters)

Definition at line 47 of file ObjectAttributesConverter.java.

References org.matsim.utils.objectattributes.ObjectAttributesConverter.putAttributeConverters().

47  {
48  this();
50  }
void putAttributeConverters(final Map< Class<?>, AttributeConverter<?>> converters)
Here is the call graph for this function:

◆ ObjectAttributesConverter() [2/2]

org.matsim.utils.objectattributes.ObjectAttributesConverter.ObjectAttributesConverter ( )

Definition at line 52 of file ObjectAttributesConverter.java.

52  {
53  this.converters.put(String.class.getName(), new StringConverter());
54  this.converters.put(Integer.class.getName(), new IntegerConverter());
55  this.converters.put(Float.class.getName(), new FloatConverter());
56  this.converters.put(Double.class.getName(), new DoubleConverter());
57  this.converters.put(Boolean.class.getName(), new BooleanConverter());
58  this.converters.put(Long.class.getName(), new LongConverter());
59  this.converters.put(double[].class.getName(), new DoubleArrayConverter());
60  this.converters.put(Map.class.getName(), new StringStringMapConverter());
61  this.converters.put(Collection.class.getName(), new StringCollectionConverter());
62  this.converters.put(Coord.class.getName(), new CoordConverter());
63  this.converters.put(Coord[].class.getName(), new CoordArrayConverter());
64  this.converters.put(PersonVehicles.class.getName(), new PersonVehiclesAttributeConverter());
67  this.converters.put(StringDoubleMap.class.getName(), new StringDoubleMapConverter());
68  }

Member Function Documentation

◆ convert()

Object org.matsim.utils.objectattributes.ObjectAttributesConverter.convert ( String  className,
String  value 
)

◆ getConverters()

Map<String, AttributeConverter<?> > org.matsim.utils.objectattributes.ObjectAttributesConverter.getConverters ( )

◆ getConverter()

AttributeConverter org.matsim.utils.objectattributes.ObjectAttributesConverter.getConverter ( String  className)
private

Definition at line 81 of file ObjectAttributesConverter.java.

Referenced by org.matsim.utils.objectattributes.ObjectAttributesConverter.convert(), and org.matsim.utils.objectattributes.ObjectAttributesConverter.convertToString().

81  {
82  if (converters.containsKey(className)) return converters.get(className);
83  try {
84  Class<?> clazz = Class.forName(className);
85 
86  if (clazz.isEnum()) {
87  AttributeConverter converter = new EnumConverter(clazz);
88  converters.put(className, converter);
89  return converter;
90  }
91 
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());
94 
95  if (missingConverters.add(className)) {
96  log.warn("No AttributeConverter found for class " + className + ". Not all attribute values can be converted.");
97  }
98  }
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.");
102  }
103  }
104 
105  return null;
106  }

◆ convertToString()

String org.matsim.utils.objectattributes.ObjectAttributesConverter.convertToString ( Object  o)

Definition at line 108 of file ObjectAttributesConverter.java.

References org.matsim.utils.objectattributes.AttributeConverter< T >.convertToString(), and org.matsim.utils.objectattributes.ObjectAttributesConverter.getConverter().

Referenced by org.matsim.lanes.LanesWriter.convertDataToXml(), org.matsim.utils.objectattributes.attributable.AttributesXmlWriterDelegate.writeAttributes(), and org.matsim.utils.objectattributes.ObjectAttributesXmlWriter.writeFile().

108  {
109 
110  AttributeConverter converter = getConverter(o.getClass().getName());
111 
112  //handle map and collection converter - check for string elements
113  //we pass in a lot of maps here that we can and (maybe) do not want to write
114  {
115  if(converter instanceof StringStringMapConverter){
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;
120  }
121  }
122  if(converter instanceof StringCollectionConverter){
123  Collection collection = ((Collection) o);
124  if(! collection.isEmpty()){
125  if(! ( collection.iterator().next() instanceof String) ) return null;
126  }
127  }
128  }
129 
130  // is returning null the right approach there?
131  return converter == null ? null : converter.convertToString(o);
132  }
Here is the call graph for this function:

◆ putAttributeConverter()

AttributeConverter org.matsim.utils.objectattributes.ObjectAttributesConverter.putAttributeConverter ( final Class<?>  clazz,
final AttributeConverter  converter 
)

Sets the converter for reading attributes of the specified class.

Parameters
clazz
converter
Returns
the previously registered converter for this class, or null if none was set before.

Definition at line 141 of file ObjectAttributesConverter.java.

Referenced by org.matsim.utils.objectattributes.ObjectAttributesConverter.putAttributeConverters().

141  {
142  return this.converters.put(clazz.getName(), converter);
143  }

◆ putAttributeConverters()

void org.matsim.utils.objectattributes.ObjectAttributesConverter.putAttributeConverters ( final Map< Class<?>, AttributeConverter<?>>  converters)

◆ removeAttributeConverter()

AttributeConverter org.matsim.utils.objectattributes.ObjectAttributesConverter.removeAttributeConverter ( final Class<?>  clazz)

Removes the converter for reading attributes of the specified class.

Parameters
clazz
Returns
the previously registered converter for this class, of null if none was set.

Definition at line 157 of file ObjectAttributesConverter.java.

Referenced by org.matsim.utils.objectattributes.attributable.AttributesXmlReaderDelegate.removeAttributeConverter(), org.matsim.utils.objectattributes.ObjectAttributesXmlWriter.removeAttributeConverter(), and org.matsim.utils.objectattributes.ObjectAttributesXmlReader.removeAttributeConverter().

157  {
158  return this.converters.remove(clazz.getName());
159  }

Member Data Documentation

◆ log

final Logger org.matsim.utils.objectattributes.ObjectAttributesConverter.log = LogManager.getLogger(ObjectAttributesConverter.class)
staticprivate

Definition at line 41 of file ObjectAttributesConverter.java.

◆ converters

final Map<String, AttributeConverter<?> > org.matsim.utils.objectattributes.ObjectAttributesConverter.converters = new HashMap<>()
private

◆ missingConverters

final Set<String> org.matsim.utils.objectattributes.ObjectAttributesConverter.missingConverters = new HashSet<>()
private

Definition at line 44 of file ObjectAttributesConverter.java.


The documentation for this class was generated from the following file: