From 5f904693399ca3cf4c611e9cc3488b23e119be21 Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Thu, 21 Apr 2016 19:19:45 +0200 Subject: [PATCH] Move model methods to VOTableData and add method getCell(). --- src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java | 103 ++++++++++++++++++++++++++++++------------------------------------------------------------------------- 2 files changed, 89 insertions(+), 73 deletions(-) diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java index 52b7181..a929474 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java @@ -17,7 +17,9 @@ package eu.omp.irap.vespa.epntapclient.votable.data; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @author N. Jourdane @@ -58,4 +60,61 @@ public class VOTableData { public void addRow(Object[] row) { data.add(row); } + + /** + * @param column The name of the column to get. + * @return An array of Table fields name. + * @throws IllegalArgumentException Column name not found in the table. + */ + public int getColumnIndex(String column) { + int i = 0; + while (!column.equals(columnsName[i])) { + i++; + if (i > columnsName.length) { + throw new IllegalArgumentException("Column " + column + " not found in the table."); + } + } + return i; + } + + /** + * @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); + } + + public Object getCell(int rowIndex, String columnName) { + return data.get(rowIndex)[getColumnIndex(columnName)]; + } + + /** + * @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 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); + } + + /** + * @param rowIndex The index of the row to get. + * @return A dictionary representing the row, where each key is a column name. + */ + public Map getRowMapByIndex(int rowIndex) { + Map row = new HashMap<>(); + for (int i = 0; i < columnsName.length; i++) { + row.put(columnsName[i], data.get(rowIndex)[i]); + } + return row; + } + } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java index 029d0c4..14b542f 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java @@ -19,15 +19,14 @@ package eu.omp.irap.vespa.epntapclient.votable.data; import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.logging.Logger; import javax.xml.bind.DatatypeConverter; import com.google.gson.Gson; +import eu.omp.irap.vespa.epntapclient.votable.model.Data; import eu.omp.irap.vespa.epntapclient.votable.model.DataType; import eu.omp.irap.vespa.epntapclient.votable.model.Field; import eu.omp.irap.vespa.epntapclient.votable.model.Stream; @@ -50,6 +49,10 @@ public class VOTableDataCtrl { private int nbColumns; + private Data dataNode; + + private List fieldsNodes; + /** The index of the current position in the bytes array. */ private int cursor; @@ -64,96 +67,42 @@ public class VOTableDataCtrl { */ public VOTableDataCtrl(Table table) throws IOException { cursor = 0; + dataNode = table.getDATA(); - List fields = new ArrayList<>(); + fieldsNodes = new ArrayList<>(); for (Object obj : table.getFIELDOrPARAMOrGROUP()) { if (obj.getClass() == Field.class) { - fields.add((Field) obj); + fieldsNodes.add((Field) obj); } } - String[] columnsName = new String[fields.size()]; + + String[] columnsName = new String[fieldsNodes.size()]; nbColumns = columnsName.length; - for (int i = 0; i < fields.size(); i++) { - columnsName[i] = fields.get(i).getName(); + for (int i = 0; i < fieldsNodes.size(); i++) { + columnsName[i] = fieldsNodes.get(i).getName(); } data = new VOTableData(columnsName); VOTableDataCtrl.logger.info("Columns name: " + new Gson().toJson(columnsName)); + } + public VOTableData parseData() { List rows = new ArrayList<>(); - if (table.getDATA().getBINARY() != null) { - parseBinaryStream(table.getDATA().getBINARY().getSTREAM(), fields); - } else if (table.getDATA().getBINARY2() != null) { - VOTableDataCtrl.parseBinary2Stream(table.getDATA().getBINARY2().getSTREAM(), fields); - } else if (table.getDATA().getTABLEDATA() != null) { - VOTableDataCtrl.parseTableDataStream(table.getDATA().getTABLEDATA(), fields); - } else if (table.getDATA().getFITS() != null) { - VOTableDataCtrl.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields); + if (dataNode.getBINARY() != null) { + parseBinaryStream(dataNode.getBINARY().getSTREAM(), fieldsNodes); + } else if (dataNode.getBINARY2() != null) { + VOTableDataCtrl.parseBinary2Stream(dataNode.getBINARY2().getSTREAM(), fieldsNodes); + } else if (dataNode.getTABLEDATA() != null) { + VOTableDataCtrl.parseTableDataStream(dataNode.getTABLEDATA(), fieldsNodes); + } else if (dataNode.getFITS() != null) { + VOTableDataCtrl.parseFITSStream(dataNode.getFITS().getSTREAM(), fieldsNodes); } Debug.printObject("voTableData", rows); - } - - /** - * @return the data stored in the Table. - */ - public VOTableData getData() { return data; } /** - * @param column The name of the column to get. - * @return An array of Table fields name. - * @throws IllegalArgumentException Column name not found in the table. - */ - public int getColumnIndex(String column) { - int i = 0; - while (!column.equals(data.getColumnsName()[i])) { - i++; - if (i > nbColumns) { - throw new IllegalArgumentException("Column " + column + " not found in the table."); - } - } - return i; - } - - /** - * @param rowIndex The index of the row to get. - * @return The Table row at the specified index. - */ - public Object[] getRowByIndex(int rowIndex) { - return data.getData().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 < nbColumns; i++) { - row.put(data.getColumnsName()[i], data.getData().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 IndexOutOfBoundsException If the value is not found at the specified column. - */ - public Object[] getRowByValue(int columnIndex, Object value) { - for (Object[] row : data.getData()) { - if (value.equals(row[columnIndex])) { - return row; - } - } - throw new IndexOutOfBoundsException( - "The value " + value + " is not found on the table at the column " + columnIndex); - } - - /** * get the data on its BINARY form. * * @param voStream the data Stream in the VOTable Table. @@ -271,4 +220,12 @@ public class VOTableDataCtrl { private static void parseFITSStream(Stream stream, List fields) { VOTableDataCtrl.logger.info("Parsing data in FITS stream..."); } + + /** + * @return the data stored in the Table. + */ + public VOTableData getData() { + return data; + } + } -- libgit2 0.21.2