1 package org.djutils.data.serialization;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.lang.reflect.Method;
5 import java.util.LinkedHashMap;
6 import java.util.Map;
7
8 import org.djunits.unit.Unit;
9 import org.djunits.value.vfloat.scalar.base.FloatScalar;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class FloatScalarSerializer<U extends Unit<U>, S extends FloatScalar<U, S>> implements TextSerializer<S>
26 {
27
28 private static Map<String, Method> valueOfMethodCache = new LinkedHashMap<>();
29
30
31 private static Map<String, Unit<?>> unitCache = new LinkedHashMap<>();
32
33
34
35
36
37
38 @SuppressWarnings("unchecked")
39 @Override
40 public String serialize(final S value, final String unitString)
41 {
42 if (value == null)
43 {
44 return null;
45 }
46
47 String key = value.getClass().getSimpleName() + "_" + unitString;
48 Unit<?> unit = unitCache.get(key);
49 if (unit == null)
50 {
51 unit = value.getDisplayUnit().getQuantity().of(unitString);
52 unitCache.put(key, unit);
53 }
54 return String.valueOf(value.getInUnit((U) unit));
55 }
56
57
58
59
60
61
62 @SuppressWarnings("unchecked")
63 @Override
64 public S deserialize(final Class<S> type, final String text, final String unit)
65 {
66 if (text == null || text.isEmpty())
67 {
68 return null;
69 }
70 try
71 {
72 Method valueOfMethod = valueOfMethodCache.get(type.getName());
73 if (valueOfMethod == null)
74 {
75 valueOfMethod = type.getDeclaredMethod("valueOf", String.class);
76 valueOfMethodCache.put(type.getName(), valueOfMethod);
77 }
78 return (S) valueOfMethod.invoke(null, text + unit);
79 }
80 catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
81 | SecurityException exception)
82 {
83 throw new RuntimeException(exception);
84 }
85 }
86
87 }