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
14
15
16
17
18
19
20
21
22 public class ListTable extends Table
23 {
24
25
26 private List<Row> rows = Collections.synchronizedList(new ArrayList<>());
27
28
29
30
31
32
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
53
54
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
75
76
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
98
99
100
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()];
112 System.arraycopy(data, 0, dataObjects, 0, getNumberOfColumns());
113 this.rows.add(new Row(this, dataObjects));
114 }
115
116
117
118
119
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 }