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