View Javadoc
1   package org.djutils.data;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.Iterator;
7   import java.util.List;
8   import java.util.Map;
9   
10  import org.djutils.exceptions.Throw;
11  
12  /**
13   * Table implementation that stores {@code Record}s in a {@code List}.
14   * <p>
15   * Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
17   * </p>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
20   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
21   */
22  public class ListTable extends Table
23  {
24  
25      /** Records. */
26      private List<Row> rows = Collections.synchronizedList(new ArrayList<>());
27  
28      /**
29       * Constructor.
30       * @param id String; id
31       * @param description String; description
32       * @param columns Collection&lt;Column&lt;?&gt;&gt;; columns
33       */
34      public ListTable(final String id, final String description, final Collection<Column<?>> columns)
35      {
36          super(id, description, columns);
37      }
38  
39      @Override
40      public Iterator<Row> iterator()
41      {
42          return this.rows.iterator();
43      }
44  
45      @Override
46      public boolean isEmpty()
47      {
48          return this.rows.isEmpty();
49      }
50  
51      /**
52       * Adds a row to the table.
53       * @param data Map&lt;String, Object&gt;; data with values given per column
54       * @throws IllegalArgumentException when the size or data types in the data map do not comply to the columns
55       */
56      public void addRow(final Map<Column<?>, Object> data)
57      {
58          Throw.whenNull(data, "Data may not be null.");
59          Throw.when(data.size() != getNumberOfColumns(), IllegalArgumentException.class,
60                  "Number of data columns doesn't match number of table columns.");
61          Object[] dataObjects = new Object[getNumberOfColumns()];
62          for (int index = 0; index < getNumberOfColumns(); index++)
63          {
64              Column<?> column = getColumn(index);
65              Throw.when(!data.containsKey(column), IllegalArgumentException.class, "Missing data for column %s", column.getId());
66              Object value = data.get(column);
67              checkValueType(column, value);
68              dataObjects[index] = value;
69          }
70          this.rows.add(new Row(this, dataObjects));
71      }
72  
73      /**
74       * Adds a row to the table.
75       * @param data Map&lt;String, Object&gt;; data with values given per column id
76       * @throws IllegalArgumentException when the size or data types in the data map do not comply to the columns
77       */
78      public void addRowByColumnIds(final Map<String, Object> data)
79      {
80          Throw.whenNull(data, "Data may not be null.");
81          Throw.when(data.size() != getNumberOfColumns(), IllegalArgumentException.class,
82                  "Number of data columns doesn't match number of table columns.");
83          Object[] dataObjects = new Object[getNumberOfColumns()];
84          for (int index = 0; index < getNumberOfColumns(); index++)
85          {
86              Column<?> column = getColumn(index);
87              Throw.when(!data.containsKey(column.getId()), IllegalArgumentException.class, "Missing data for column %s",
88                      column.getId());
89              Object value = data.get(column.getId());
90              checkValueType(column, value);
91              dataObjects[index] = value;
92          }
93          this.rows.add(new Row(this, dataObjects));
94      }
95  
96      /**
97       * Adds a row to the table. The order in which the elements in the array are offered should be the same as the order of the
98       * columns.
99       * @param data Object[]; row data
100      * @throws IllegalArgumentException when the size, order or data types in the {@code Object[]} do not comply to the columns
101      */
102     public void addRow(final Object[] data)
103     {
104         Throw.whenNull(data, "Data may not be null.");
105         Throw.when(data.length != getNumberOfColumns(), IllegalArgumentException.class,
106                 "Number of data columns doesn't match number of table columns.");
107         for (int index = 0; index < getNumberOfColumns(); index++)
108         {
109             checkValueType(getColumn(index), data[index]);
110         }
111         Object[] dataObjects = new Object[getNumberOfColumns()]; // safe copy
112         System.arraycopy(data, 0, dataObjects, 0, getNumberOfColumns());
113         this.rows.add(new Row(this, dataObjects));
114     }
115 
116     /**
117      * Checks whether the type of a value is suitable for a column.
118      * @param column Column&lt;?&gt;; column.
119      * @param value Object; value.
120      */
121     private void checkValueType(final Column<?> column, final Object value)
122     {
123         if (null != value)
124         {
125             Class<?> valueType = value.getClass();
126             Throw.when(!column.getValueType().isAssignableFrom(valueType), IllegalArgumentException.class,
127                     "Data value for column %s is not of type %s, but of type %s.", column.getId(), column.getValueType(),
128                     valueType);
129         }
130     }
131 
132 }