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