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      /** {@inheritDoc} */
40      @Override
41      public Iterator<Row> iterator()
42      {
43          return this.rows.iterator();
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public boolean isEmpty()
49      {
50          return this.rows.isEmpty();
51      }
52  
53      /**
54       * Adds a row to the table.
55       * @param data Map&lt;String, Object&gt;; data with values given per column
56       * @throws IllegalArgumentException when the size or data types in the data map do not comply to the columns
57       */
58      public void addRow(final Map<Column<?>, Object> data)
59      {
60          Throw.whenNull(data, "Data may not be null.");
61          Throw.when(data.size() != getNumberOfColumns(), IllegalArgumentException.class,
62                  "Number of data columns doesn't match number of table columns.");
63          Object[] dataObjects = new Object[getNumberOfColumns()];
64          for (int index = 0; index < getNumberOfColumns(); index++)
65          {
66              Column<?> column = getColumn(index);
67              Throw.when(!data.containsKey(column), IllegalArgumentException.class, "Missing data for column %s", column.getId());
68              Object value = data.get(column);
69              checkValueType(column, value);
70              dataObjects[index] = value;
71          }
72          this.rows.add(new Row(this, dataObjects));
73      }
74  
75      /**
76       * Adds a row to the table.
77       * @param data Map&lt;String, Object&gt;; data with values given per column id
78       * @throws IllegalArgumentException when the size or data types in the data map do not comply to the columns
79       */
80      public void addRowByColumnIds(final Map<String, Object> data)
81      {
82          Throw.whenNull(data, "Data may not be null.");
83          Throw.when(data.size() != getNumberOfColumns(), IllegalArgumentException.class,
84                  "Number of data columns doesn't match number of table columns.");
85          Object[] dataObjects = new Object[getNumberOfColumns()];
86          for (int index = 0; index < getNumberOfColumns(); index++)
87          {
88              Column<?> column = getColumn(index);
89              Throw.when(!data.containsKey(column.getId()), IllegalArgumentException.class, "Missing data for column %s",
90                      column.getId());
91              Object value = data.get(column.getId());
92              checkValueType(column, value);
93              dataObjects[index] = value;
94          }
95          this.rows.add(new Row(this, dataObjects));
96      }
97  
98      /**
99       * 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
100      * columns.
101      * @param data Object[]; row data
102      * @throws IllegalArgumentException when the size, order or data types in the {@code Object[]} do not comply to the columns
103      */
104     public void addRow(final Object[] data)
105     {
106         Throw.whenNull(data, "Data may not be null.");
107         Throw.when(data.length != getNumberOfColumns(), IllegalArgumentException.class,
108                 "Number of data columns doesn't match number of table columns.");
109         for (int index = 0; index < getNumberOfColumns(); index++)
110         {
111             checkValueType(getColumn(index), data[index]);
112         }
113         Object[] dataObjects = new Object[getNumberOfColumns()]; // safe copy
114         System.arraycopy(data, 0, dataObjects, 0, getNumberOfColumns());
115         this.rows.add(new Row(this, dataObjects));
116     }
117 
118     /**
119      * Checks whether the type of a value is suitable for a column.
120      * @param column Column&lt;?&gt;; column.
121      * @param value Object; value.
122      */
123     private void checkValueType(final Column<?> column, final Object value)
124     {
125         if (null != value)
126         {
127             Class<?> valueType = value.getClass();
128             Throw.when(!column.getValueType().isAssignableFrom(valueType), IllegalArgumentException.class,
129                     "Data value for column %s is not of type %s, but of type %s.", column.getId(), column.getValueType(),
130                     valueType);
131         }
132     }
133 
134 }