org.djutils.serialization.serializers.FloatScalarSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of djutils-serialization Show documentation
Show all versions of djutils-serialization Show documentation
DJUTILS serialization of data structures
package org.djutils.serialization.serializers;
import org.djunits.unit.Unit;
import org.djunits.value.vfloat.scalar.base.FloatScalar;
import org.djutils.serialization.EndianUtil;
import org.djutils.serialization.FieldTypes;
import org.djutils.serialization.SerializationException;
/**
* (De)serializes a DJUNITS FloatScalar.
*
* Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See DJUNITS License.
*
* @author Alexander Verbraeck
* @param the unit type
* @param the scalar type
*/
public class FloatScalarSerializer, S extends FloatScalar>
extends ObjectWithUnitSerializer
{
/** */
public FloatScalarSerializer()
{
super(FieldTypes.FLOAT_32_UNIT, "Djunits_FloatScalar");
}
@Override
public int size(final S afs) throws SerializationException
{
return 2 + 4;
}
@Override
public void serialize(final S afs, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
throws SerializationException
{
encodeUnit(afs.getDisplayUnit(), buffer, pointer, endianUtil);
float v = afs.getSI();
endianUtil.encodeFloat(v, buffer, pointer.getAndIncrement(4));
}
@Override
public S deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil) throws SerializationException
{
U unit = getUnit(buffer, pointer, endianUtil);
S afs = FloatScalar.instantiateAnonymous(endianUtil.decodeFloat(buffer, pointer.getAndIncrement(4)),
unit.getStandardUnit());
afs.setDisplayUnit(unit);
return afs;
}
}