1 package org.djutils.serialization.serializers;
2
3 import org.djunits.unit.Unit;
4 import org.djunits.unit.scale.IdentityScale;
5 import org.djunits.value.ValueRuntimeException;
6 import org.djunits.value.storage.StorageType;
7 import org.djunits.value.vdouble.scalar.base.DoubleScalar;
8 import org.djunits.value.vdouble.vector.base.DoubleVector;
9 import org.djunits.value.vdouble.vector.data.DoubleVectorData;
10 import org.djutils.exceptions.Throw;
11 import org.djutils.serialization.EndianUtil;
12 import org.djutils.serialization.FieldTypes;
13 import org.djutils.serialization.SerializationException;
14
15
16
17
18
19
20
21
22
23
24
25
26 public class DoubleVectorArraySerializer<U extends Unit<U>, S extends DoubleScalar<U, S>,
27 V extends DoubleVector<U, S, V>> extends ObjectWithUnitSerializer<U, V[]>
28 {
29
30 public DoubleVectorArraySerializer()
31 {
32 super(FieldTypes.DOUBLE_64_UNIT_COLUMN_ARRAY, "Djunits_vector_array");
33 }
34
35 @Override
36 public int size(final V[] adva) throws SerializationException
37 {
38 int result = 4 + 4;
39 int width = adva.length;
40 int height = adva[0].size();
41 for (int i = 0; i < width; i++)
42 {
43 V adv = adva[i];
44 Throw.when(adv.size() != height, SerializationException.class,
45 "All AbstractDoubleVectors in array must have same size");
46 result += 2;
47 }
48 result += height * width * 8;
49 return result;
50 }
51
52 @Override
53 public void serialize(final V[] adva, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
54 throws SerializationException
55 {
56 int width = adva.length;
57 int height = adva[0].size();
58 endianUtil.encodeInt(height, buffer, pointer.getAndIncrement(4));
59 endianUtil.encodeInt(adva.length, buffer, pointer.getAndIncrement(4));
60 for (int i = 0; i < width; i++)
61 {
62 V adv = adva[i];
63 Throw.when(adv.size() != height, SerializationException.class,
64 "All AbstractDoubleVectors in array must have same size");
65 encodeUnit(adv.getDisplayUnit(), buffer, pointer, endianUtil);
66 }
67 for (int row = 0; row < height; row++)
68 {
69 for (int col = 0; col < width; col++)
70 {
71 try
72 {
73 endianUtil.encodeDouble(adva[col].getSI(row), buffer, pointer.getAndIncrement(8));
74 }
75 catch (ValueRuntimeException e)
76 {
77 throw new SerializationException(e);
78 }
79 }
80 }
81
82 }
83
84 @Override
85 public V[] deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
86 throws SerializationException
87 {
88 int height = endianUtil.decodeInt(buffer, pointer.getAndIncrement(4));
89 int width = endianUtil.decodeInt(buffer, pointer.getAndIncrement(4));
90 @SuppressWarnings("unchecked")
91 V[] result = (V[]) new DoubleVector[width];
92 Unit<? extends Unit<?>>[] units = new Unit<?>[width];
93 for (int col = 0; col < width; col++)
94 {
95 units[col] = getUnit(buffer, pointer, endianUtil);
96 }
97 double[][] values = new double[width][height];
98 for (int row = 0; row < height; row++)
99 {
100 for (int col = 0; col < width; col++)
101 {
102 values[col][row] = endianUtil.decodeDouble(buffer, pointer.getAndIncrement(8));
103 }
104 }
105 for (int col = 0; col < width; col++)
106 {
107 try
108 {
109 DoubleVectorData fvd = DoubleVectorData.instantiate(values[col], IdentityScale.SCALE, StorageType.DENSE);
110 result[col] = DoubleVectorSerializer.instantiateAnonymous(fvd, units[col]);
111 }
112 catch (ValueRuntimeException e)
113 {
114 throw new SerializationException(e);
115 }
116 }
117 return result;
118 }
119
120 }