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-2025 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   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
17   * @param <U> the unit type
18   * @param <T> The object type to (de)serialize
19   */
20  public abstract class ArrayOrMatrixWithUnitSerializer<U extends Unit<U>, T> extends BasicSerializer<T>
21  {
22      /** Number of dimension; 1 for array, 2 for matrix. */
23      private final int numberOfDimensions;
24  
25      /**
26       * Construct a new serializer for Djunits arrays or matrices.
27       * @param type the field type (returned by the <code>fieldType</code> method)
28       * @param dataClassName returned by the dataClassName method
29       * @param numberOfDimensions should be 1 for array serializer and 2 for matrix serializer
30       */
31      public ArrayOrMatrixWithUnitSerializer(final byte type, final String dataClassName, final int numberOfDimensions)
32      {
33          super(type, dataClassName);
34          this.numberOfDimensions = numberOfDimensions;
35      }
36  
37      @Override
38      public final int sizeWithPrefix(final T object) throws SerializationException
39      {
40          return 1 + size(object);
41      }
42  
43      @Override
44      public final void serializeWithPrefix(final T object, final byte[] buffer, final Pointer pointer,
45              final EndianUtil endianUtil) throws SerializationException
46      {
47          buffer[pointer.getAndIncrement(1)] = endianUtil.isBigEndian() ? fieldType() : (byte) (fieldType() + 128);
48          serialize(object, buffer, pointer, endianUtil);
49      }
50  
51      @Override
52      public final int getNumberOfDimensions()
53      {
54          return this.numberOfDimensions;
55      }
56  
57      /**
58       * Return the size of an element in the djunits vector or array.
59       * @return the size of one element in number of bytes
60       */
61      public abstract int getElementSize();
62      
63      @Override
64      public boolean hasUnit()
65      {
66          return true;
67      }
68  
69      /**
70       * Code a unit.
71       * @param unit the unit to code in the byte array
72       * @param message the byte array
73       * @param pointer the start pointer in the byte array
74       * @param endianUtil encoder to use for multi-byte values
75       */
76      protected void encodeUnit(final U unit, final byte[] message, final Pointer pointer, final EndianUtil endianUtil)
77      {
78          SerializationUnits unitType = SerializationUnits.getUnitType(unit);
79          message[pointer.getAndIncrement(1)] = unitType.getCode();
80          DisplayType displayType = DisplayType.getDisplayType(unit);
81          message[pointer.getAndIncrement(1)] = displayType.getByteCode();
82      }
83  
84      /**
85       * Retrieve and decode a DJUNITS unit.
86       * @param buffer the encoded data
87       * @param pointer position in the encoded data where the unit is to be decoded from
88       * @param endianUtil decoder for multi-byte values
89       * @return Unit
90       */
91      @SuppressWarnings("unchecked")
92      protected U getUnit(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
93      {
94          SerializationUnits unitType = SerializationUnits.getUnitType(buffer[pointer.getAndIncrement(1)]);
95          DisplayType displayType = DisplayType.getDisplayType(unitType, 0 + buffer[pointer.getAndIncrement(1)]);
96          return (U) displayType.getDjunitsType();
97      }
98  
99  }