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