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.Unit;
7 import org.djutils.exceptions.Throw;
8
9 /**
10 * Float-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 */
17 public class FloatSparseValue<Q extends Quantity<Q>> implements Serializable
18 {
19 /** */
20 private static final long serialVersionUID = 20191018L;
21
22 /** the row in the matrix. */
23 private final int row;
24
25 /** the column in the matrix. */
26 private final int column;
27
28 /** the value of the data point in the matrix. */
29 private final float si;
30
31 /**
32 * Create a data point for a sparse matrix.
33 * @param row the row of the sparse data point in the matrix
34 * @param column the column of the sparse data point in the matrix
35 * @param value the value in the given unit of the data point in the matrix
36 */
37 public FloatSparseValue(final int row, final int column, final Q value)
38 {
39 this(row, column, checkNull(value).floatValue());
40 }
41
42 /**
43 * Check for null pointer in constructor.
44 * @param value the scalar to check
45 * @return the untouched scalar value
46 * @param <Q> the quantity type
47
48 */
49 private static <Q extends Quantity<Q>> Q checkNull(final Q value)
50 {
51 Throw.whenNull(value, "value may not be null");
52 return value;
53 }
54
55 /**
56 * Create a data point for a sparse matrix.
57 * @param row the row of the sparse data point in the matrix
58 * @param column the column of the sparse data point in the matrix
59 * @param valueInUnit the value in the given unit of the data point in the matrix
60 * @param unit the unit of the value
61 */
62 public FloatSparseValue(final int row, final int column, final float valueInUnit, final Unit<?, Q> unit)
63 {
64 this(row, column, (float) unit.toBaseValue(valueInUnit));
65 }
66
67 /**
68 * Create a data point for a sparse matrix.
69 * @param row the row of the sparse data point in the matrix
70 * @param column the column of the sparse data point in the matrix
71 * @param si the SI value of the data point in the matrix
72 */
73 public FloatSparseValue(final int row, final int column, final float si)
74 {
75 Throw.when(row < 0, IllegalArgumentException.class, "row may not be negative");
76 Throw.when(column < 0, IllegalArgumentException.class, "column may not be negative");
77 this.row = row;
78 this.column = column;
79 this.si = si;
80 }
81
82 /**
83 * @return the row in the matrix
84 */
85 public final int getRow()
86 {
87 return this.row;
88 }
89
90 /**
91 * @return the column in the matrix
92 */
93 public final int getColumn()
94 {
95 return this.column;
96 }
97
98 /**
99 * @return the SI value of the data point in the matrix
100 */
101 public final float si()
102 {
103 return this.si;
104 }
105
106 @Override
107 public String toString()
108 {
109 return "FloatSparseValue [row=" + this.row + ", column=" + this.column + ", valueSI=" + this.si + "]";
110 }
111
112 }