View Javadoc
1   package org.djutils.serialization.serializers;
2   
3   import org.djutils.serialization.EndianUtil;
4   
5   /**
6    * Serializer for arrays or matrices.
7    * @param <T> type; with [] or [][]
8    * @param <E> type without [] or [][]
9    */
10  public abstract class ArrayOrMatrixSerializer<T extends Object, E extends Object> extends BasicSerializer<T>
11  {
12      /** Size of one element of the encoded data. */
13      private final int elementSize;
14  
15      /** Number of dimension; 1 for array, 2 for matrix. */
16      private final int numberOfDimensions;
17  
18      /**
19       * Construct a new ArrayOrMatrixSerializere.
20       * @param type byte; the field type (returned by the <code>fieldType</code> method)
21       * @param elementSize int; the number of bytes needed to encode one additional array, or matrix element
22       * @param dataClassName String; returned by the dataClassName method
23       * @param numberOfDimensions int; should be 1 for array serializer and 2 for matrix serializer
24       */
25      ArrayOrMatrixSerializer(final byte type, final int elementSize, final String dataClassName, final int numberOfDimensions)
26      {
27          super(type, dataClassName);
28          this.elementSize = elementSize;
29          this.numberOfDimensions = numberOfDimensions;
30      }
31  
32      /**
33       * Return the number of bytes needed to encode one additional element.
34       * @return int; the number of bytes needed to encode one additional element
35       */
36      public final int getElementSize()
37      {
38          return this.elementSize;
39      }
40  
41      /**
42       * Return the number of dimensions of the stored data.
43       * @return int; 1 for array, 2 for matrix
44       */
45      @Override
46      public final int getNumberOfDimensions()
47      {
48          return this.numberOfDimensions;
49      }
50  
51      /**
52       * Serializer for one array or matrix element (without type prefix) must be implemented in implementing sub classes.
53       * @param object E; the object to serialize
54       * @param buffer byte[]; the byte buffer for the serialized object
55       * @param offset int; index in byte buffer where first serialized byte must be stored
56       * @param endianUtil EndianUtil; selects bigEndian or littleEndian encoding
57       */
58      public abstract void serializeElement(E object, byte[] buffer, int offset, EndianUtil endianUtil);
59  
60      /**
61       * Deserializer for one array or matrix element (without type prefix) must be implemented in implementing sub classes.
62       * @param buffer byte[]; the byte buffer from which the object is to be deserialized
63       * @param offset int; index in byte buffer where first byte of the object is stored
64       * @param endianUtil EndianUtil; selects bigEndian or littleEndian encoding
65       * @return E; the deserialized object
66       */
67      public abstract E deSerializeElement(byte[] buffer, int offset, EndianUtil endianUtil);
68  
69  }