View Javadoc
1   package org.djutils.serialization.serializers;
2   
3   import java.lang.reflect.Constructor;
4   import java.lang.reflect.InvocationTargetException;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import org.djunits.unit.SIUnit;
9   import org.djunits.unit.Unit;
10  import org.djunits.unit.scale.IdentityScale;
11  import org.djunits.unit.util.UnitRuntimeException;
12  import org.djunits.value.ValueRuntimeException;
13  import org.djunits.value.storage.StorageType;
14  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
15  import org.djunits.value.vdouble.vector.SIVector;
16  import org.djunits.value.vdouble.vector.base.DoubleVector;
17  import org.djunits.value.vdouble.vector.data.DoubleVectorData;
18  import org.djutils.serialization.EndianUtil;
19  import org.djutils.serialization.FieldTypes;
20  import org.djutils.serialization.SerializationException;
21  
22  /**
23   * (De)serializes a DJUNITS DoubleVector.
24   * <p>
25   * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
27   * <p>
28   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
29   * @param <U> the unit type
30   * @param <S> the scalar type
31   * @param <V> the vector type
32   */
33  public class DoubleVectorSerializer<U extends Unit<U>, S extends DoubleScalar<U, S>, V extends DoubleVector<U, S, V>>
34          extends ArrayOrMatrixWithUnitSerializer<U, V>
35  {
36      /** The cache to make the lookup of the constructor for a Vevtor belonging to a unit faster. */
37      private static final Map<Unit<?>, Constructor<? extends DoubleVector<?, ?, ?>>> CACHE = new HashMap<>();
38  
39      /** */
40      public DoubleVectorSerializer()
41      {
42          super(FieldTypes.DOUBLE_64_UNIT_ARRAY, "Djunits_DoubleVector", 1);
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public int size(final V adv) throws SerializationException
48      {
49          try
50          {
51              return 4 + 2 + 8 * adv.size();
52          }
53          catch (ValueRuntimeException e)
54          {
55              throw new SerializationException(e);
56          }
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public void serialize(final V adv, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
62              throws SerializationException
63      {
64          try
65          {
66              endianUtil.encodeInt(adv.size(), buffer, pointer.getAndIncrement(4));
67              encodeUnit(adv.getDisplayUnit(), buffer, pointer, endianUtil);
68              for (int i = 0; i < adv.size(); i++)
69              {
70                  endianUtil.encodeDouble(adv.get(i).getSI(), buffer, pointer.getAndIncrement(8));
71              }
72          }
73          catch (ValueRuntimeException e)
74          {
75              throw new SerializationException(e);
76          }
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public V deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil) throws SerializationException
82      {
83          int size = endianUtil.decodeInt(buffer, pointer.getAndIncrement(4));
84          Unit<? extends Unit<?>> unit = getUnit(buffer, pointer, endianUtil);
85          double[] array = new double[size];
86          for (int i = 0; i < size; i++)
87          {
88              array[i] = endianUtil.decodeDouble(buffer, pointer.getAndIncrement(8));
89          }
90          try
91          {
92              DoubleVectorData fvd = DoubleVectorData.instantiate(array, IdentityScale.SCALE, StorageType.DENSE);
93              return instantiateAnonymous(fvd, unit);
94          }
95          catch (ValueRuntimeException exception)
96          {
97              throw new SerializationException(exception);
98          }
99      }
100 
101     /**
102      * Instantiate the DoubleVector based on its unit. Loose check for types on the compiler. This allows the unit to be
103      * specified as a Unit&lt;?&gt; type.<br>
104      * <b>Note</b> that it is possible to make mistakes with anonymous units.
105      * @param data DoubleVectorData; the values
106      * @param unit Unit&lt;?&gt;; the unit in which the value is expressed
107      * @return V; an instantiated DoubleVector with the provided displayUunit
108      * @param <U> the unit type
109      * @param <S> the scalar type
110      * @param <V> the vector type
111      */
112     @SuppressWarnings("unchecked")
113     public static <U extends Unit<U>, S extends DoubleScalar<U, S>,
114             V extends DoubleVector<U, S, V>> V instantiateAnonymous(final DoubleVectorData data, final Unit<?> unit)
115     {
116         try
117         {
118             Constructor<? extends DoubleVector<?, ?, ?>> vectorConstructor = CACHE.get(unit);
119             if (vectorConstructor == null)
120             {
121                 if (!unit.getClass().getSimpleName().endsWith("Unit"))
122                 {
123                     throw new ClassNotFoundException("Unit " + unit.getClass().getSimpleName()
124                             + " name does noet end with 'Unit'. Cannot find corresponding scalar");
125                 }
126                 Class<? extends DoubleVector<?, ?, ?>> vectorClass;
127                 if (unit instanceof SIUnit)
128                 {
129                     vectorClass = SIVector.class;
130                 }
131                 else
132                 {
133                     vectorClass = (Class<DoubleVector<?, ?, ?>>) Class.forName("org.djunits.value.vdouble.vector."
134                             + unit.getClass().getSimpleName().replace("Unit", "") + "Vector");
135                 }
136                 vectorConstructor = vectorClass.getDeclaredConstructor(DoubleVectorData.class, unit.getClass());
137                 CACHE.put(unit, vectorConstructor);
138             }
139             return (V) vectorConstructor.newInstance(data, unit);
140         }
141         catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
142                 | IllegalAccessException | IllegalArgumentException | InvocationTargetException exception)
143         {
144             throw new UnitRuntimeException(
145                     "Cannot instantiate DoubleVector of unit " + unit.toString() + ". Reason: " + exception.getMessage());
146         }
147     }
148 }