1 package org.djutils.serialization.serializers;
2
3 import org.djunits.unit.Unit;
4 import org.djunits.value.vdouble.scalar.base.DoubleScalar;
5 import org.djutils.serialization.EndianUtil;
6 import org.djutils.serialization.FieldTypes;
7 import org.djutils.serialization.SerializationException;
8
9
10
11
12
13
14
15
16
17
18
19 public class DoubleScalarSerializer<U extends Unit<U>, S extends DoubleScalar<U, S>>
20 extends ObjectWithUnitSerializer<U, S>
21 {
22
23 public DoubleScalarSerializer()
24 {
25 super(FieldTypes.DOUBLE_64_UNIT, "Djunits_DoubleScalar");
26 }
27
28
29 @Override
30 public int size(final S ads) throws SerializationException
31 {
32 return 2 + 8;
33 }
34
35
36 @Override
37 public void serialize(final S ads, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
38 throws SerializationException
39 {
40 encodeUnit(ads.getDisplayUnit(), buffer, pointer, endianUtil);
41 double v = ads.getSI();
42 endianUtil.encodeDouble(v, buffer, pointer.getAndIncrement(8));
43 }
44
45
46 @Override
47 public S deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil) throws SerializationException
48 {
49 U unit = getUnit(buffer, pointer, endianUtil);
50 S afd = DoubleScalar.instantiateAnonymous(endianUtil.decodeDouble(buffer, pointer.getAndIncrement(8)),
51 unit.getStandardUnit());
52 afd.setDisplayUnit(unit);
53 return afd;
54 }
55 }