package org.djutils.data; import org.djutils.base.Identifiable; import org.djutils.immutablecollections.ImmutableList; /** * Table with data stored in structured records. *

* Copyright (c) 2020-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See DJUTILS License. *

* @author Alexander Verbraeck * @author Peter Knoppers */ public interface DataTable extends Iterable, Identifiable { /** * Returns the description. * @return description */ String getDescription(); /** * Returns the list of columns. * @return list of columns */ ImmutableList> getColumns(); /** * Returns the number of columns. * @return number of columns */ default int getNumberOfColumns() { return getColumns().size(); } /** * Returns whether the table is empty. * @return whether the table is empty */ boolean isEmpty(); /** * Return the column ids as a String[]. * @return String[]; the column ids */ default String[] getColumnIds() { String[] headers = new String[getNumberOfColumns()]; int index = 0; for (DataColumn column : getColumns()) { headers[index++] = column.getId(); } return headers; } /** * Return the column descriptions as a String[]. * @return String[] the column headers */ default String[] getColumnDescriptions() { String[] descriptions = new String[getNumberOfColumns()]; int index = 0; for (DataColumn column : getColumns()) { descriptions[index++] = column.getDescription(); } return descriptions; } /** * Return the column data types as a Class<?>[]. * @return Class<?>[] the column data types */ default Class[] getColumnDataTypes() { Class[] dataTypes = new Class[getNumberOfColumns()]; int index = 0; for (DataColumn column : getColumns()) { dataTypes[index++] = column.getValueType(); } return dataTypes; } /** * Return the column data types as a String[]. Each data type is presented as the full class name or the primitive name. In * case of an array, the result is preceded by an "[" for each dimension. After one or more "[" symbols, the class name is * preceded by an "L" for a non-primitive class or interface, and by "I" for integer, "Z" for boolean, "B" for byte, "C" for * char, "D" for double, "F" for float, "J" for long and "S" for short. So for a column with a double, "double" is returned. * For a column with a "Double", "java.lang.Double" is returned, for an int[][], "[[I" is returned, and for a Long[], * "[Ljava.lang.Long" is returned. * @return String[] the column data types as an array of Strings */ default String[] getColumnDataTypeStrings() { String[] dataTypes = new String[getNumberOfColumns()]; int index = 0; for (DataColumn column : getColumns()) { dataTypes[index++] = column.getValueType().getName(); } return dataTypes; } }