Class ArrayOrMatrixWithUnitSerializer<U extends Unit<U>,T>
java.lang.Object
org.djutils.serialization.serializers.BasicSerializer<T>
org.djutils.serialization.serializers.ArrayOrMatrixWithUnitSerializer<U,T>
- Type Parameters:
U
- the unit typeT
- The object type to (de)serialize
- All Implemented Interfaces:
Serializer<T>
- Direct Known Subclasses:
DoubleMatrixSerializer
,DoubleVectorSerializer
,FloatMatrixSerializer
,FloatVectorSerializer
public abstract class ArrayOrMatrixWithUnitSerializer<U extends Unit<U>,T> extends BasicSerializer<T>
Serializer for Djunits arrays and matrices.
Copyright (c) 2019-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.
- Version:
- $Revision$, $LastChangedDate$, by $Author$,
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Constructor Summary
Constructors Constructor Description ArrayOrMatrixWithUnitSerializer(byte type, String dataClassName, int numberOfDimensions)
Construct a new serializer for Djunits arrays or matrices. -
Method Summary
Modifier and Type Method Description protected void
encodeUnit(U unit, byte[] message, Pointer pointer, EndianUtil endianUtil)
Code a unit, including MoneyUnits.int
getNumberOfDimensions()
Return the number of dimensions of the stored data.protected U
getUnit(byte[] buffer, Pointer pointer, EndianUtil endianUtil)
Retrieve and decode a DJUNITS unit.void
serializeWithPrefix(T object, byte[] buffer, Pointer pointer, EndianUtil endianUtil)
Serialize an object of type T including the prefix byte(s).int
sizeWithPrefix(T object)
Compute the number of bytes needed to serialize an object of type T (including the byte(s) that indicate that an object of type T is next in the data stream).Methods inherited from class org.djutils.serialization.serializers.BasicSerializer
dataClassName, fieldType, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.djutils.serialization.serializers.Serializer
deSerialize, serialize, size
-
Constructor Details
-
ArrayOrMatrixWithUnitSerializer
Construct a new serializer for Djunits arrays or matrices.- Parameters:
type
- byte; the field type (returned by thefieldType
method)dataClassName
- String; returned by the dataClassName methodnumberOfDimensions
- int; should be 1 for array serializer and 2 for matrix serializer
-
-
Method Details
-
sizeWithPrefix
Description copied from interface:Serializer
Compute the number of bytes needed to serialize an object of type T (including the byte(s) that indicate that an object of type T is next in the data stream).- Parameters:
object
- T; Instance of the object- Returns:
- int; the number of bytes needed to serialize an object of type T
- Throws:
SerializationException
- when theobject
cannot be serialized
-
serializeWithPrefix
public final void serializeWithPrefix(T object, byte[] buffer, Pointer pointer, EndianUtil endianUtil) throws SerializationExceptionDescription copied from interface:Serializer
Serialize an object of type T including the prefix byte(s).- Parameters:
object
- T; the object to serializebuffer
- byte[]; buffer for the serialized Tpointer
- Pointer; position in buffer where the first byte of the serialized T will be storedendianUtil
- EndianUtil; selects bigEndian or littleEndian encoding- Throws:
SerializationException
- when a matrix has size zero or is jagged
-
getNumberOfDimensions
public final int getNumberOfDimensions()Description copied from interface:Serializer
Return the number of dimensions of the stored data.- Returns:
- int; 0 for plain data, 1 for array, 2 for matrix
-
encodeUnit
Code a unit, including MoneyUnits.- Parameters:
unit
- the unit to code in the byte arraymessage
- the byte arraypointer
- the start pointer in the byte arrayendianUtil
- EndianUtil; encoder to use for multi-byte values
-
getUnit
Retrieve and decode a DJUNITS unit.- Parameters:
buffer
- byte[]; the encoded datapointer
- Pointer; position in the encoded data where the unit is to be decoded fromendianUtil
- EndianUtil; decoder for multi-byte values- Returns:
- Unit
-