1   package org.djutils.serialization.serializers;
2   
3   import java.lang.reflect.Constructor;
4   import java.lang.reflect.InvocationTargetException;
5   import java.util.HashMap;
6   import java.util.Map;
7   
8   import org.djunits.unit.SIUnit;
9   import org.djunits.unit.Unit;
10  import org.djunits.unit.scale.IdentityScale;
11  import org.djunits.unit.util.UnitRuntimeException;
12  import org.djunits.value.ValueRuntimeException;
13  import org.djunits.value.storage.StorageType;
14  import org.djunits.value.vdouble.matrix.SIMatrix;
15  import org.djunits.value.vdouble.matrix.base.DoubleMatrix;
16  import org.djunits.value.vdouble.matrix.data.DoubleMatrixData;
17  import org.djunits.value.vdouble.scalar.base.DoubleScalar;
18  import org.djunits.value.vdouble.vector.base.DoubleVector;
19  import org.djutils.serialization.Endianness;
20  import org.djutils.serialization.FieldTypes;
21  import org.djutils.serialization.SerializationException;
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  public class DoubleMatrixSerializer<U extends Unit<U>, S extends DoubleScalar<U, S>, V extends DoubleVector<U, S, V>,
36          M extends DoubleMatrix<U, S, V, M>> extends ArrayOrMatrixWithUnitSerializer<U, M>
37  {
38      
39      private static final Map<Unit<?>, Constructor<? extends DoubleMatrix<?, ?, ?, ?>>> CACHE = new HashMap<>();
40  
41      
42      public DoubleMatrixSerializer()
43      {
44          super(FieldTypes.DOUBLE_64_UNIT_MATRIX, "Djunits_DoubleMatrix", 2);
45      }
46  
47      @Override
48      public int size(final M adm)
49      {
50          return 4 + 4 + 2 + 8 * adm.rows() * adm.cols();
51      }
52  
53      @Override
54      public int getElementSize()
55      {
56          return 8;
57      }
58  
59      @Override
60      public void serialize(final M adm, final byte[] buffer, final Pointer pointer, final Endianness endianness)
61              throws SerializationException
62      {
63          endianness.encodeInt(adm.rows(), buffer, pointer.getAndIncrement(4));
64          endianness.encodeInt(adm.cols(), buffer, pointer.getAndIncrement(4));
65          encodeUnit(adm.getDisplayUnit(), buffer, pointer, endianness);
66          for (int i = 0; i < adm.rows(); i++)
67          {
68              for (int j = 0; j < adm.cols(); j++)
69              {
70                  endianness.encodeDouble(adm.get(i, j).getSI(), buffer, pointer.getAndIncrement(8));
71              }
72          }
73      }
74  
75      @Override
76      public M deSerialize(final byte[] buffer, final Pointer pointer, final Endianness endianness) throws SerializationException
77      {
78          try
79          {
80              int height = endianness.decodeInt(buffer, pointer.getAndIncrement(4));
81              int width = endianness.decodeInt(buffer, pointer.getAndIncrement(4));
82              Unit<? extends Unit<?>> unit = getUnit(buffer, pointer, endianness);
83              double[][] array = new double[height][width];
84              for (int i = 0; i < height; i++)
85              {
86                  for (int j = 0; j < width; j++)
87                  {
88                      array[i][j] = endianness.decodeDouble(buffer, pointer.getAndIncrement(8));
89                  }
90              }
91              DoubleMatrixData fvd = DoubleMatrixData.instantiate(array, IdentityScale.SCALE, StorageType.DENSE);
92              return instantiateAnonymous(fvd, unit);
93          }
94          catch (ValueRuntimeException exception)
95          {
96              throw new SerializationException(exception);
97          }
98      }
99  
100     
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112     @SuppressWarnings("unchecked")
113     public static <U extends Unit<U>, S extends DoubleScalar<U, S>, V extends DoubleVector<U, S, V>,
114             M extends DoubleMatrix<U, S, V, M>> M instantiateAnonymous(final DoubleMatrixData data, final Unit<?> unit)
115     {
116         try
117         {
118             Constructor<? extends DoubleMatrix<?, ?, ?, ?>> matrixConstructor = CACHE.get(unit);
119             if (matrixConstructor == null)
120             {
121                 if (!unit.getClass().getSimpleName().endsWith("Unit"))
122                 {
123                     throw new ClassNotFoundException("Unit " + unit.getClass().getSimpleName()
124                             + " name does noet end with 'Unit'. Cannot find corresponding scalar");
125                 }
126                 Class<? extends DoubleMatrix<?, ?, ?, ?>> matrixClass;
127                 if (unit instanceof SIUnit)
128                 {
129                     matrixClass = SIMatrix.class;
130                 }
131                 else
132                 {
133                     matrixClass = (Class<DoubleMatrix<?, ?, ?, ?>>) Class.forName("org.djunits.value.vdouble.matrix."
134                             + unit.getClass().getSimpleName().replace("Unit", "") + "Matrix");
135                 }
136                 matrixConstructor = matrixClass.getDeclaredConstructor(DoubleMatrixData.class, unit.getClass());
137                 CACHE.put(unit, matrixConstructor);
138             }
139             return (M) matrixConstructor.newInstance(data, unit);
140         }
141         catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
142                 | IllegalAccessException | IllegalArgumentException | InvocationTargetException exception)
143         {
144             throw new UnitRuntimeException(
145                     "Cannot instantiate DoubleMatrix of unit " + unit.toString() + ". Reason: " + exception.getMessage());
146         }
147     }
148 
149 }