VOTableData.java
11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* This file is a part of EpnTAPClient.
* This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
* See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
* Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
*
* This program is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details. You should have received a copy of
* the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package eu.omp.irap.vespa.votable.votabledata;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import eu.omp.irap.vespa.votable.votable.VOTableException.CanNotParseDataException;
/**
* @author N. Jourdane
*/
public class VOTableData {
/** The name of the columns. `columnNames` length is equals to `data` length. */
private String[] columnsNames;
/**
* A list of arrays, representing data stored in the VOTable. Each element is a VOTable row,
* where arrays elements are in the same order as `columnNames`.
*/
private List<Object[]> data;
/** The title of the data set. */
private String title;
/**
* Constructor of VOTableData.
*
* @param title The title of the data set.
*/
public VOTableData(String title) {
this.title = title;
columnsNames = new String[0];
data = new ArrayList<>();
}
/**
* Constructor of VOTableData.
*
* @param columnsNames An array of columns names.
* @param title The title of the data set.
*/
public VOTableData(String title, String[] columnsNames) {
this.title = title;
this.columnsNames = columnsNames;
data = new ArrayList<>();
}
/**
* Constructor of VOTableData.
*
* @param title The title of the data set.
* @param columnsName An array of columns names.
* @param data A list of rows where each row is an array of value sorted at the same order as
* the column names returned by {@link #getColumnsName()}.
*/
public VOTableData(String title, String[] columnsName, List<Object[]> data) {
this.title = title;
columnsNames = columnsName;
this.data = data;
}
/**
* Append a VOTable data to the current VOTable data.
*
* @param dataToAppend The voTable data to append.
*/
public void append(VOTableData dataToAppend) {
data.addAll(dataToAppend.getData());
}
/**
* Get the cell value at the specified row and column.
*
* @param rowIndex The index of the row to select.
* @param columnIndex The index of the column to select.
* @return The value of the returned cell.
*/
public Object getCell(int rowIndex, int columnIndex) {
return data.get(rowIndex)[columnIndex];
}
/**
* Get the cell value at the specified row and column.
*
* @param rowIndex The index of the row to select.
* @param columnName The name of the column to select.
* @return The value of the returned cell.
* @throws CanNotParseDataException The column name was not found in the list.
*/
public Object getCell(int rowIndex, String columnName) throws CanNotParseDataException {
return data.get(rowIndex)[getColumnIndex(columnName)];
}
/**
* Get a column by its index.
*
* @param columnIndex The index of the column to get.
* @return A list of objects corresponding to all the rows values at the specified column.
*/
public List<Object> getColumn(int columnIndex) {
List<Object> column = new ArrayList<>();
for (int rowId = 0; rowId < data.size(); rowId++) {
column.add(data.get(rowId)[columnIndex]);
}
return column;
}
/**
* Get a column by its name.
*
* @param columnName The name of the column to get.
* @return A list of objects corresponding to all the rows values at the specified column.
* @throws CanNotParseDataException The column name was not found in the list.
*/
public List<Object> getColumn(String columnName) throws CanNotParseDataException {
return getColumn(getColumnIndex(columnName));
}
/**
* @param columnName The name of the column to get.
* @return The index of the first columns who match with columnName
* @throws CanNotParseDataException The column name was not found in the list.
* @throws IllegalArgumentException Column name not found in the table.
*/
public int getColumnIndex(String columnName) throws CanNotParseDataException {
for (int colId = 0; colId < columnsNames.length; colId++) {
if (columnName.equals(columnsNames[colId])) {
return colId;
}
}
throw new CanNotParseDataException(
"The column " + columnName + " was not found in the table.");
}
/**
* @return An array of all the columns names of the data set.
*/
public String[] getColumnsName() {
return columnsNames;
}
/**
* @return The data of the dataset, which is a list of rows where each row is an array of value
* sorted at the same order as the column names returned by {@link #getColumnsName()}.
*/
public List<Object[]> getData() {
return data;
}
/**
* @return A 2D array corresponding to the data, which is an array of rows where each row is an
* array of value sorted at the same order as the column names returned by
* {@link #getColumnsName()}.
*/
public Object[][] getDataArray() {
return data.toArray(new Object[data.size()][]);
}
/**
* @return the number of columns in the data set.
*/
public int getNbColumns() {
return columnsNames.length;
}
/**
* @return The number of rows in the data set.
*/
public int getNbRows() {
return data == null ? 0 : data.size();
}
/**
* @param rowIndex The index of the row to get.
* @return The Table row at the specified index.
*/
public Object[] getRow(int rowIndex) {
return data.get(rowIndex);
}
/**
* Search a row in the data, whose the specified value match at the specified column.
*
* @param columnIndex The index of a unique column to identify a row.
* @param value The value of the cell at the specified column name.
* @return An array of objects corresponding to the row who match with the filter.
* @throws IndexOutOfBoundsException If the value is not found at the specified column.
*/
public Object[] getRowByValue(int columnIndex, Object value) {
for (Object[] row : data) {
if (value.equals(row[columnIndex])) {
return row;
}
}
throw new IndexOutOfBoundsException(
"The value " + value + " is not found on the table at the column " + columnIndex);
}
/**
* Search a row in the data, whose the specified value match at the specified column. Ie,
* getRowMapByValue("foo", "bar") will returns the row where the value at the column named "foo"
* is equals to the value "bar". The column name must be unique, and the all values in the data
* set at the specified column must be also uniques.
*
* @param columnName The name of a unique column to identify a row.
* @param value The value of the cell at the specified column name.
* @return An array of objects corresponding to the row who match with the filter.
* @throws CanNotParseDataException The column name was not found in the list.
*/
public Object[] getRowByValue(String columnName, Object value) throws CanNotParseDataException {
return getRowByValue(getColumnIndex(columnName), value);
}
/**
* Search a row in the data, whose the specified value match at the specified column. Ie,
* getRowMapByValue("foo", "bar") will returns the row index where the value at the column named
* "foo" is equals to the value "bar". The column name must be unique, and the all values in the
* data set at the specified column must be also uniques.
*
* @param columnName The name of a unique column to identify a row.
* @param value The value of the cell at the specified column name.
* @return The index of the row who match with the filter.
* @throws CanNotParseDataException The column name was not found in the list.
*/
public int getRowIndex(String columnName, Object value) throws CanNotParseDataException {
int columnId = getColumnIndex(columnName);
for (int rowId = 0; rowId < data.size(); rowId++) {
if (value.equals(data.get(rowId)[columnId])) {
return rowId;
}
}
throw new IndexOutOfBoundsException(
"The value " + value + " is not found on the table at the column " + columnName);
}
/**
* Get a row at the specified index.
*
* @param rowIndex The index of the row to get.
* @return A map representing the row, as <column name, cell value>.
*/
public Map<String, Object> getRowMap(int rowIndex) {
Map<String, Object> row = new HashMap<>();
for (int i = 0; i < columnsNames.length; i++) {
row.put(columnsNames[i], data.get(rowIndex)[i]);
}
return row;
}
/**
* Search a row in the data, whose the specified value match at the specified column. Ie,
* getRowMapByValue("foo", "bar") will returns the row where the value at the column named "foo"
* is equals to the value "bar". The column name must be unique, and the all values in the data
* set at the specified column must be also uniques.
*
* @param columnName The name of a unique column to identify a row.
* @param value The value of the cell at the specified column name.
* @return A map representing the row, as <column name, cell value> who match with the filter.
* @throws CanNotParseDataException The column name was not found in the list.
*/
public Map<String, Object> getRowMapByValue(String columnName, Object value)
throws CanNotParseDataException {
return getRowMap(getRowIndex(columnName, value));
}
/**
* @return the title of the data set
*/
public String getTitle() {
return title;
}
/**
* Tell if a column contains the specified value.
*
* @param columnName The name of the concerned column.
* @param value The value to looks for.
* @return true if the specified column contains the specified value.
* @throws CanNotParseDataException The column name was not found in the list.
*/
public boolean isColumnContainingValue(String columnName, Object value)
throws CanNotParseDataException {
int rowId = getColumnIndex(columnName);
for (Object[] row : data) {
if (value.equals(row[rowId])) {
return true;
}
}
return false;
}
/**
* Tell if the columns of the data set contains the specified column.
*
* @param columnName the name of the column to search.
* @return true if the columns of the data set contains the specified column.
*/
public boolean isContainingColumnName(String columnName) {
for (String element : columnsNames) {
if (columnName.equals(element)) {
return true;
}
}
return false;
}
/**
* Set the data of the data set.
*
* @param data A list of rows where each row is an array of value sorted at the same order as
* the column names returned by {@link #getColumnsName()}.
*/
public void setData(List<Object[]> data) {
this.data = data;
}
}