1 package org.djutils.serialization.serializers;
2
3 import org.djunits.unit.Unit;
4 import org.djutils.serialization.DisplayType;
5 import org.djutils.serialization.EndianUtil;
6 import org.djutils.serialization.SerializationException;
7 import org.djutils.serialization.SerializationUnits;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 public abstract class ArrayOrMatrixWithUnitSerializer<U extends Unit<U>, T> extends BasicSerializer<T>
23 {
24
25 private final int numberOfDimensions;
26
27
28
29
30
31
32
33 public ArrayOrMatrixWithUnitSerializer(final byte type, final String dataClassName, final int numberOfDimensions)
34 {
35 super(type, dataClassName);
36 this.numberOfDimensions = numberOfDimensions;
37 }
38
39 @Override
40 public final int sizeWithPrefix(final T object) throws SerializationException
41 {
42 return 1 + size(object);
43 }
44
45 @Override
46 public final void serializeWithPrefix(final T object, final byte[] buffer, final Pointer pointer,
47 final EndianUtil endianUtil) throws SerializationException
48 {
49 buffer[pointer.getAndIncrement(1)] = fieldType();
50 serialize(object, buffer, pointer, endianUtil);
51 }
52
53 @Override
54 public final int getNumberOfDimensions()
55 {
56 return this.numberOfDimensions;
57 }
58
59
60
61
62
63
64
65
66 protected void encodeUnit(final U unit, final byte[] message, final Pointer pointer, final EndianUtil endianUtil)
67 {
68 SerializationUnits unitType = SerializationUnits.getUnitType(unit);
69 message[pointer.getAndIncrement(1)] = unitType.getCode();
70 DisplayType displayType = DisplayType.getDisplayType(unit);
71 message[pointer.getAndIncrement(1)] = displayType.getByteCode();
72 }
73
74
75
76
77
78
79
80
81 @SuppressWarnings("unchecked")
82 protected U getUnit(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
83 {
84 SerializationUnits unitType = SerializationUnits.getUnitType(buffer[pointer.getAndIncrement(1)]);
85 DisplayType displayType = DisplayType.getDisplayType(unitType, 0 + buffer[pointer.getAndIncrement(1)]);
86 return (U) displayType.getDjunitsType();
87 }
88
89 }