diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java index afcf972..952376b 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java @@ -1,4 +1,4 @@ -/** +/* * 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 @@ -13,11 +13,14 @@ * the GNU General Public License along with this program. If not, see * . */ + package eu.omp.irap.vespa.epntapclient.votable.controller; import java.util.ArrayList; import java.util.Base64; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Vector; import com.google.gson.Gson; @@ -89,20 +92,84 @@ public class VOTableDataParser { } /** - * @return the data stored in the VOTable. + * @return the data stored in the Table. */ public List getDataArray() { return data; } /** - * @return An array of VOTable fields name. + * @return An array of Table fields name. */ public String[] getColumnsName() { return columnsName; } /** + * @param columnName The name of the column to get. + * @return An array of Table fields name. + * @throws Exception If the column name is not found. + */ + public int getColumnIndex(String columnName) throws Exception { + int i = 0; + while (!columnName.equals(columnsName[i])) { + i++; + if (i > columnsName.length) + throw new Exception("Column name " + columnName + "not found in the table."); + } + return i; + } + + /** + * @return The number of rows in the Table. + */ + public int getNbRows() { + return data.size(); + } + + /** + * @return The number of columns in the Table. + */ + public int getNbColumns() { + return columnsName.length; + } + + /** + * @param rowIndex The index of the row to get. + * @return The Table row at the specified index. + */ + public Object[] getRowByIndex(int rowIndex) { + return data.get(rowIndex); + } + + /** + * @param rowIndex The index of the row to get. + * @return A dictionary representing the row, where each key is a column name. + */ + public Map getDicRowByIndex(int rowIndex) { + Map row = new HashMap<>(); + for (int i = 0; i < columnsName.length; i++) { + row.put(columnsName[i], data.get(rowIndex)[i]); + } + return row; + } + + /** + * @param columnIndex A UNIQUE column (as SQL sense) to identify a row. + * @param value The value at `columnName`, in order to get the full row. + * @return A Table row, identified by a unique `value` in the `columnName` column. + * @throws Exception If the value is not found at the specified column. + */ + public Object[] getRowByValue(int columnIndex, Object value) throws Exception { + for (Object[] row : data) { + if (value.equals(row[columnIndex])) + return row; + } + throw new Exception( + "The value " + value + " is not found on the table at the column " + columnIndex); + } + + /** * get the data on its BINARY form. * * @param stream the data Stream in the VOTable Table. -- libgit2 0.21.2