View Javadoc
1   package org.djutils.serialization.serializers;
2   
3   import org.djunits.unit.Unit;
4   import org.djunits.unit.scale.IdentityScale;
5   import org.djunits.unit.util.UnitRuntimeException;
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   * (De)serializes an array of (same length) DJUNITS DoubleVectors.
17   * <p>
18   * Copyright (c) 2019-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
19   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
20   * </p>
21   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
22   * @param <U> the unit type
23   * @param <S> the scalar type
24   * @param <V> the vector type
25   */
26  public class DoubleVectorArraySerializer<U extends Unit<U>, S extends DoubleScalar<U, S>, V extends DoubleVector<U, S, V>>
27          extends ObjectWithUnitSerializer<U, V[]>
28  {
29      /** */
30      public DoubleVectorArraySerializer()
31      {
32          super(FieldTypes.DOUBLE_64_UNIT_COLUMN_MATRIX, "Djunits_double_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; // quantity and display unit
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                  endianUtil.encodeDouble(adva[col].getSI(row), buffer, pointer.getAndIncrement(8));
72              }
73          }
74  
75      }
76  
77      @Override
78      public V[] deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
79              throws SerializationException
80      {
81          int height = endianUtil.decodeInt(buffer, pointer.getAndIncrement(4));
82          int width = endianUtil.decodeInt(buffer, pointer.getAndIncrement(4));
83          @SuppressWarnings("unchecked")
84          V[] result = (V[]) new DoubleVector[width];
85          Unit<? extends Unit<?>>[] units = new Unit<?>[width];
86          for (int col = 0; col < width; col++)
87          {
88              units[col] = getUnit(buffer, pointer, endianUtil);
89          }
90          double[][] values = new double[width][height];
91          for (int row = 0; row < height; row++)
92          {
93              for (int col = 0; col < width; col++)
94              {
95                  values[col][row] = endianUtil.decodeDouble(buffer, pointer.getAndIncrement(8));
96              }
97          }
98          for (int col = 0; col < width; col++)
99          {
100             try
101             {
102                 DoubleVectorData fvd = DoubleVectorData.instantiate(values[col], IdentityScale.SCALE, StorageType.DENSE);
103                 result[col] = DoubleVectorSerializer.instantiateAnonymous(fvd, units[col]);
104             }
105             catch (UnitRuntimeException e)
106             {
107                 throw new SerializationException(e);
108             }
109         }
110         return result;
111     }
112 
113     @Override
114     public int getNumberOfDimensions()
115     {
116         return 2;
117     }
118 
119 }