View Javadoc
1   package org.djutils.data;
2   
3   import java.util.Arrays;
4   
5   /**
6    * Row in a table.
7    * <p>
8    * Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
9    * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
10   * </p>
11   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
12   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
13   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
14   */
15  public class Row
16  {
17  
18      /** Table. */
19      private final Table table;
20      
21      /** Values. */
22      private final Object[] values;
23  
24      /**
25       * Constructor.
26       * @param table Table; table.
27       * @param values Object[]; values.
28       */
29      public Row(final Table table, final Object[] values)
30      {
31          this.table = table;
32          this.values = values;
33      }
34  
35      /**
36       * Returns the column value in this row. For performance, use {@code getValue(int columnNumber)}.
37       * @param column Column&lt;T&gt;; column.
38       * @param <T> value type.
39       * @return T; the column value in this row.
40       */
41      @SuppressWarnings("unchecked")
42      public <T> T getValue(final Column<T> column)
43      {
44          return (T) this.values[this.table.getColumnNumber(column)];
45      }
46  
47      /**
48       * Returns the column value in this row. For performance, use {@code getValue(int columnNumber)}.
49       * @param id String; column id.
50       * @return the column value in this row.
51       */
52      public Object getValue(final String id)
53      {
54          return this.values[this.table.getColumnNumber(id)];
55      }
56      
57      /**
58       * Returns the column value in this row.
59       * @param columnNumber int; column number.
60       * @return Object; the column value in this row.
61       */
62      public Object getValue(final int columnNumber)
63      {
64          return this.values[columnNumber];
65      }
66      
67      /**
68       * Returns the column values of this record in the natural order of the columns.
69       * @return Object[]; the column value in this record
70       */
71      public Object[] getValues()
72      {
73          return this.values;
74      }
75      
76      @Override
77      public int hashCode()
78      {
79          final int prime = 31;
80          int result = 1;
81          result = prime * result + Arrays.deepHashCode(this.values);
82          return result;
83      }
84  
85      @Override
86      public boolean equals(final Object obj)
87      {
88          if (this == obj)
89          {
90              return true;
91          }
92          if (obj == null)
93          {
94              return false;
95          }
96          if (getClass() != obj.getClass())
97          {
98              return false;
99          }
100         Row other = (Row) obj;
101         return Arrays.deepEquals(this.values, other.values);
102     }
103 
104     @Override
105     public String toString()
106     {
107         return "Row " + Arrays.toString(this.values);
108     }
109 
110 }