All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.djutils.serialization.serializers.DoubleScalarSerializer Maven / Gradle / Ivy

There is a newer version: 2.2.2
Show newest version
package org.djutils.serialization.serializers;

import org.djunits.unit.Unit;
import org.djunits.value.vdouble.scalar.base.DoubleScalar;
import org.djunits.value.vdouble.scalar.base.DoubleScalarInterface;
import org.djutils.serialization.EndianUtil;
import org.djutils.serialization.FieldTypes;
import org.djutils.serialization.SerializationException;

/**
 * (De)serializes a DJUNITS DoubleScalar.
 * 

* Copyright (c) 2019-2023 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 DoubleScalarSerializer, S extends DoubleScalarInterface> extends ObjectWithUnitSerializer { /** */ public DoubleScalarSerializer() { super(FieldTypes.DOUBLE_64_UNIT, "Djunits_DoubleScalar"); } /** {@inheritDoc} */ @Override public int size(final S ads) throws SerializationException { return 2 + 8; } /** {@inheritDoc} */ @Override public void serialize(final S ads, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil) throws SerializationException { encodeUnit(ads.getDisplayUnit(), buffer, pointer, endianUtil); double v = ads.getSI(); endianUtil.encodeDouble(v, buffer, pointer.getAndIncrement(8)); } /** {@inheritDoc} */ @Override public S deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil) throws SerializationException { U unit = getUnit(buffer, pointer, endianUtil); S afd = DoubleScalar.instantiateAnonymous(endianUtil.decodeDouble(buffer, pointer.getAndIncrement(8)), unit.getStandardUnit()); afd.setDisplayUnit(unit); return afd; } }