View Javadoc
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   * Serializer for Djunits arrays and matrices.
11   * <p>
12   * Copyright (c) 2019-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
14   * <p>
15   * @version $Revision$, $LastChangedDate$, by $Author$, <br>
16   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
17   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
18   * @param <U> the unit type
19   * @param <T> The object type to (de)serialize
20   */
21  public abstract class ArrayOrMatrixWithUnitSerializer<U extends Unit<U>, T> extends BasicSerializer<T>
22  {
23      /** Number of dimension; 1 for array, 2 for matrix. */
24      private final int numberOfDimensions;
25  
26      /**
27       * Construct a new serializer for Djunits arrays or matrices.
28       * @param type byte; the field type (returned by the <code>fieldType</code> method)
29       * @param dataClassName String; returned by the dataClassName method
30       * @param numberOfDimensions int; should be 1 for array serializer and 2 for matrix serializer
31       */
32      public ArrayOrMatrixWithUnitSerializer(final byte type, final String dataClassName, final int numberOfDimensions)
33      {
34          super(type, dataClassName);
35          this.numberOfDimensions = numberOfDimensions;
36      }
37  
38      @Override
39      public final int sizeWithPrefix(final T object) throws SerializationException
40      {
41          return 1 + size(object);
42      }
43  
44      @Override
45      public final void serializeWithPrefix(final T object, final byte[] buffer, final Pointer pointer,
46              final EndianUtil endianUtil) throws SerializationException
47      {
48          buffer[pointer.getAndIncrement(1)] = fieldType();
49          serialize(object, buffer, pointer, endianUtil);
50      }
51  
52      @Override
53      public final int getNumberOfDimensions()
54      {
55          return this.numberOfDimensions;
56      }
57  
58      /**
59       * Code a unit, including MoneyUnits.
60       * @param unit U; the unit to code in the byte array
61       * @param message byte[]; the byte array
62       * @param pointer Pointer; the start pointer in the byte array
63       * @param endianUtil EndianUtil; encoder to use for multi-byte values
64       */
65      protected void encodeUnit(final U unit, final byte[] message, final Pointer pointer, final EndianUtil endianUtil)
66      {
67          SerializationUnits unitType = SerializationUnits.getUnitType(unit);
68          message[pointer.getAndIncrement(1)] = unitType.getCode();
69          DisplayType displayType = DisplayType.getDisplayType(unit);
70          message[pointer.getAndIncrement(1)] = displayType.getByteCode();
71      }
72  
73      /**
74       * Retrieve and decode a DJUNITS unit.
75       * @param buffer byte[]; the encoded data
76       * @param pointer Pointer; position in the encoded data where the unit is to be decoded from
77       * @param endianUtil EndianUtil; decoder for multi-byte values
78       * @return Unit
79       */
80      @SuppressWarnings("unchecked")
81      protected U getUnit(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
82      {
83          SerializationUnits unitType = SerializationUnits.getUnitType(buffer[pointer.getAndIncrement(1)]);
84          DisplayType displayType = DisplayType.getDisplayType(unitType, 0 + buffer[pointer.getAndIncrement(1)]);
85          return (U) displayType.getDjunitsType();
86      }
87  
88  }