View Javadoc
1   package org.djunits.value.vdouble.matrix.base;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.unit.Unit;
6   import org.djunits.value.ValueRuntimeException;
7   import org.djunits.value.util.ValueUtil;
8   import org.djunits.value.vdouble.scalar.base.DoubleScalar;
9   import org.djutils.exceptions.Throw;
10  
11  /**
12   * Data point for a matrix that can be used for constructing sparse matrices.
13   * <p>
14   * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
16   * </p>
17   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
18   * @param <U> the unit type of the value
19   * @param <S> the scalar type of the value
20   */
21  public class DoubleSparseValue<U extends Unit<U>, S extends DoubleScalar<U, S>> implements Serializable
22  {
23      /** */
24      private static final long serialVersionUID = 20191018L;
25  
26      /** the row in the matrix. */
27      private final int row;
28  
29      /** the column in the matrix. */
30      private final int column;
31  
32      /** the value of the data point in the matrix. */
33      private final double valueSI;
34  
35      /**
36       * Create a data point for a sparse matrix.
37       * @param row int; the row of the sparse data point in the matrix
38       * @param column int; the column of the sparse data point in the matrix
39       * @param value S; the value in the given unit of the data point in the matrix
40       */
41      public DoubleSparseValue(final int row, final int column, final S value)
42      {
43          this(row, column, checkNull(value).getSI());
44      }
45  
46      /**
47       * Check for null pointer in constructor.
48       * @param value S; the scalar to check
49       * @return S; the untouched scalar value
50       * @param <U> the unit type
51       * @param <S> the corresponding scalar type
52       */
53      private static <U extends Unit<U>, S extends DoubleScalar<U, S>> S checkNull(final S value)
54      {
55          Throw.whenNull(value, "value may not be null");
56          return value;
57      }
58  
59      /**
60       * Create a data point for a sparse matrix.
61       * @param row int; the row of the sparse data point in the matrix
62       * @param column int; the column of the sparse data point in the matrix
63       * @param valueInUnit double; the value in the given unit of the data point in the matrix
64       * @param unit U; the unit of the value
65       */
66      public DoubleSparseValue(final int row, final int column, final double valueInUnit, final U unit)
67      {
68          this(row, column, (float) ValueUtil.expressAsSIUnit(valueInUnit, unit));
69      }
70  
71      /**
72       * Create a data point for a sparse matrix.
73       * @param row int; the row of the sparse data point in the matrix
74       * @param column int; the column of the sparse data point in the matrix
75       * @param valueSI double; the SI value of the data point in the matrix
76       */
77      public DoubleSparseValue(final int row, final int column, final double valueSI)
78      {
79          Throw.when(row < 0, ValueRuntimeException.class, "row may not be negative");
80          Throw.when(column < 0, ValueRuntimeException.class, "column may not be negative");
81          this.row = row;
82          this.column = column;
83          this.valueSI = valueSI;
84      }
85  
86      /**
87       * @return the row in the matrix
88       */
89      public final int getRow()
90      {
91          return this.row;
92      }
93  
94      /**
95       * @return the column in the matrix
96       */
97      public final int getColumn()
98      {
99          return this.column;
100     }
101 
102     /**
103      * @return the SI value of the data point in the matrix
104      */
105     public final double getValueSI()
106     {
107         return this.valueSI;
108     }
109 
110     @Override
111     public String toString()
112     {
113         return "DoubleSparseValue [row=" + this.row + ", column=" + this.column + ", valueSI=" + this.valueSI + "]";
114     }
115 
116 }