View Javadoc
1   package org.djutils.data;
2   
3   import org.djutils.base.Identifiable;
4   import org.djutils.immutablecollections.ImmutableList;
5   
6   /**
7    * Table with data stored in structured records.
8    * <p>
9    * Copyright (c) 2020-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
11   * </p>
12   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
13   * @author <a href="https://www.tudelft.nl/pknoppers">Peter Knoppers</a>
14   * @author <a href="https://www.transport.citg.tudelft.nl">Wouter Schakel</a>
15   */
16  public interface DataTable extends Iterable<DataRecord>, Identifiable
17  {
18  
19      /**
20       * Returns the description.
21       * @return description
22       */
23      String getDescription();
24  
25      /**
26       * Returns the list of columns.
27       * @return list of columns
28       */
29      ImmutableList<DataColumn<?>> getColumns();
30  
31      /**
32       * Returns the number of columns.
33       * @return number of columns
34       */
35      default int getNumberOfColumns()
36      {
37          return getColumns().size();
38      }
39  
40      /**
41       * Returns whether the table is empty.
42       * @return whether the table is empty
43       */
44      boolean isEmpty();
45  
46      /**
47       * Return the column ids as a String[].
48       * @return String[]; the column ids
49       */
50      default String[] getColumnIds()
51      {
52          String[] headers = new String[getNumberOfColumns()];
53          int index = 0;
54          for (DataColumn<?> column : getColumns())
55          {
56              headers[index++] = column.getId();
57          }
58          return headers;
59      }
60  
61      /**
62       * Return the column descriptions as a String[].
63       * @return String[] the column headers
64       */
65      default String[] getColumnDescriptions()
66      {
67          String[] descriptions = new String[getNumberOfColumns()];
68          int index = 0;
69          for (DataColumn<?> column : getColumns())
70          {
71              descriptions[index++] = column.getDescription();
72          }
73          return descriptions;
74      }
75  
76      /**
77       * Return the column data types as a Class&lt;?&gt;[].
78       * @return Class&lt;?&gt;[] the column data types
79       */
80      default Class<?>[] getColumnDataTypes()
81      {
82          Class<?>[] dataTypes = new Class[getNumberOfColumns()];
83          int index = 0;
84          for (DataColumn<?> column : getColumns())
85          {
86              dataTypes[index++] = column.getValueType();
87          }
88          return dataTypes;
89      }
90  
91      /**
92       * Return the column data types as a String[]. Each data type is presented as the full class name or the primitive name. In
93       * case of an array, the result is preceded by an "[" for each dimension. After one or more "[" symbols, the class name is
94       * preceded by an "L" for a non-primitive class or interface, and by "I" for integer, "Z" for boolean, "B" for byte, "C" for
95       * char, "D" for double, "F" for float, "J" for long and "S" for short. So for a column with a double, "double" is returned.
96       * For a column with a "Double", "java.lang.Double" is returned, for an int[][], "[[I" is returned, and for a Long[],
97       * "[Ljava.lang.Long" is returned.
98       * @return String[] the column data types as an array of Strings
99       */
100     default String[] getColumnDataTypeStrings()
101     {
102         String[] dataTypes = new String[getNumberOfColumns()];
103         int index = 0;
104         for (DataColumn<?> column : getColumns())
105         {
106             dataTypes[index++] = column.getValueType().getName();
107         }
108         return dataTypes;
109     }
110 
111 }