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.value.ValueRuntimeException;
6   import org.djunits.value.storage.StorageType;
7   import org.djunits.value.vdouble.scalar.base.DoubleScalarInterface;
8   import org.djunits.value.vdouble.vector.base.DoubleVector;
9   import org.djunits.value.vdouble.vector.base.DoubleVectorInterface;
10  import org.djunits.value.vdouble.vector.data.DoubleVectorData;
11  import org.djutils.serialization.EndianUtil;
12  import org.djutils.serialization.FieldTypes;
13  import org.djutils.serialization.SerializationException;
14  
15  /**
16   * (De)serializes a DJUNITS DoubleVector.
17   * <p>
18   * Copyright (c) 2019-2022 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 DoubleVectorSerializer<U extends Unit<U>, S extends DoubleScalarInterface<U, S>,
27          V extends DoubleVectorInterface<U, S, V>> extends ArrayOrMatrixWithUnitSerializer<U, V>
28  {
29      /** */
30      public DoubleVectorSerializer()
31      {
32          super(FieldTypes.DOUBLE_64_UNIT_ARRAY, "Djunits_DoubleVector", 1);
33      }
34  
35      /** {@inheritDoc} */
36      @Override
37      public int size(final V adv) throws SerializationException
38      {
39          try
40          {
41              return 4 + 2 + 8 * adv.size();
42          }
43          catch (ValueRuntimeException e)
44          {
45              throw new SerializationException(e);
46          }
47      }
48  
49      /** {@inheritDoc} */
50      @Override
51      public void serialize(final V adv, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
52              throws SerializationException
53      {
54          try
55          {
56              endianUtil.encodeInt(adv.size(), buffer, pointer.getAndIncrement(4));
57              encodeUnit(adv.getDisplayUnit(), buffer, pointer, endianUtil);
58              for (int i = 0; i < adv.size(); i++)
59              {
60                  endianUtil.encodeDouble(adv.get(i).getSI(), buffer, pointer.getAndIncrement(8));
61              }
62          }
63          catch (ValueRuntimeException e)
64          {
65              throw new SerializationException(e);
66          }
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public V deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil) throws SerializationException
72      {
73          int size = endianUtil.decodeInt(buffer, pointer.getAndIncrement(4));
74          Unit<? extends Unit<?>> unit = getUnit(buffer, pointer, endianUtil);
75          double[] array = new double[size];
76          for (int i = 0; i < size; i++)
77          {
78              array[i] = endianUtil.decodeDouble(buffer, pointer.getAndIncrement(8));
79          }
80          try
81          {
82              DoubleVectorData fvd = DoubleVectorData.instantiate(array, IdentityScale.SCALE, StorageType.DENSE);
83              return DoubleVector.instantiateAnonymous(fvd, unit);
84          }
85          catch (ValueRuntimeException exception)
86          {
87              throw new SerializationException(exception);
88          }
89      }
90  
91  }