org.djutils.serialization.serializers.DoubleVectorArraySerializer 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.unit.scale.IdentityScale;
import org.djunits.value.ValueRuntimeException;
import org.djunits.value.storage.StorageType;
import org.djunits.value.vdouble.scalar.base.DoubleScalar;
import org.djunits.value.vdouble.vector.base.DoubleVector;
import org.djunits.value.vdouble.vector.data.DoubleVectorData;
import org.djutils.exceptions.Throw;
import org.djutils.serialization.EndianUtil;
import org.djutils.serialization.FieldTypes;
import org.djutils.serialization.SerializationException;
/**
* (De)serializes an array of (same length) DJUNITS DoubleVectors.
*
* 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
* @param the vector type
*/
public class DoubleVectorArraySerializer, S extends DoubleScalar,
V extends DoubleVector> extends ObjectWithUnitSerializer
{
/** */
public DoubleVectorArraySerializer()
{
super(FieldTypes.DOUBLE_64_UNIT_COLUMN_ARRAY, "Djunits_vector_array");
}
@Override
public int size(final V[] adva) throws SerializationException
{
int result = 4 + 4;
int width = adva.length;
int height = adva[0].size();
for (int i = 0; i < width; i++)
{
V adv = adva[i];
Throw.when(adv.size() != height, SerializationException.class,
"All AbstractDoubleVectors in array must have same size");
result += 2;
}
result += height * width * 8;
return result;
}
@Override
public void serialize(final V[] adva, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
throws SerializationException
{
int width = adva.length;
int height = adva[0].size();
endianUtil.encodeInt(height, buffer, pointer.getAndIncrement(4));
endianUtil.encodeInt(adva.length, buffer, pointer.getAndIncrement(4));
for (int i = 0; i < width; i++)
{
V adv = adva[i];
Throw.when(adv.size() != height, SerializationException.class,
"All AbstractDoubleVectors in array must have same size");
encodeUnit(adv.getDisplayUnit(), buffer, pointer, endianUtil);
}
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
try
{
endianUtil.encodeDouble(adva[col].getSI(row), buffer, pointer.getAndIncrement(8));
}
catch (ValueRuntimeException e)
{
throw new SerializationException(e);
}
}
}
}
@Override
public V[] deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
throws SerializationException
{
int height = endianUtil.decodeInt(buffer, pointer.getAndIncrement(4));
int width = endianUtil.decodeInt(buffer, pointer.getAndIncrement(4));
@SuppressWarnings("unchecked")
V[] result = (V[]) new DoubleVector[width];
Unit extends Unit>>[] units = new Unit>[width];
for (int col = 0; col < width; col++)
{
units[col] = getUnit(buffer, pointer, endianUtil);
}
double[][] values = new double[width][height];
for (int row = 0; row < height; row++)
{
for (int col = 0; col < width; col++)
{
values[col][row] = endianUtil.decodeDouble(buffer, pointer.getAndIncrement(8));
}
}
for (int col = 0; col < width; col++)
{
try
{
DoubleVectorData fvd = DoubleVectorData.instantiate(values[col], IdentityScale.SCALE, StorageType.DENSE);
result[col] = DoubleVectorSerializer.instantiateAnonymous(fvd, units[col]);
}
catch (ValueRuntimeException e)
{
throw new SerializationException(e);
}
}
return result;
}
}