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      /** {@inheritDoc} */
77      @Override
78      public int hashCode()
79      {
80          final int prime = 31;
81          int result = 1;
82          result = prime * result + Arrays.deepHashCode(this.values);
83          return result;
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public boolean equals(final Object obj)
89      {
90          if (this == obj)
91          {
92              return true;
93          }
94          if (obj == null)
95          {
96              return false;
97          }
98          if (getClass() != obj.getClass())
99          {
100             return false;
101         }
102         Row other = (Row) obj;
103         return Arrays.deepEquals(this.values, other.values);
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public String toString()
109     {
110         return "Row " + Arrays.toString(this.values);
111     }
112 
113 }