Commit 5f904693399ca3cf4c611e9cc3488b23e119be21

Authored by Nathanael Jourdane
1 parent 95cf2731
Exists in master

Move model methods to VOTableData and add method getCell().

src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableData.java
... ... @@ -17,7 +17,9 @@
17 17 package eu.omp.irap.vespa.epntapclient.votable.data;
18 18  
19 19 import java.util.ArrayList;
  20 +import java.util.HashMap;
20 21 import java.util.List;
  22 +import java.util.Map;
21 23  
22 24 /**
23 25 * @author N. Jourdane
... ... @@ -58,4 +60,61 @@ public class VOTableData {
58 60 public void addRow(Object[] row) {
59 61 data.add(row);
60 62 }
  63 +
  64 + /**
  65 + * @param column The name of the column to get.
  66 + * @return An array of Table fields name.
  67 + * @throws IllegalArgumentException Column name not found in the table.
  68 + */
  69 + public int getColumnIndex(String column) {
  70 + int i = 0;
  71 + while (!column.equals(columnsName[i])) {
  72 + i++;
  73 + if (i > columnsName.length) {
  74 + throw new IllegalArgumentException("Column " + column + " not found in the table.");
  75 + }
  76 + }
  77 + return i;
  78 + }
  79 +
  80 + /**
  81 + * @param rowIndex The index of the row to get.
  82 + * @return The Table row at the specified index.
  83 + */
  84 + public Object[] getRowByIndex(int rowIndex) {
  85 + return data.get(rowIndex);
  86 + }
  87 +
  88 + public Object getCell(int rowIndex, String columnName) {
  89 + return data.get(rowIndex)[getColumnIndex(columnName)];
  90 + }
  91 +
  92 + /**
  93 + * @param columnIndex A UNIQUE column (as SQL sense) to identify a row.
  94 + * @param value The value at `columnName`, in order to get the full row.
  95 + * @return A Table row, identified by a unique `value` in the `columnName` column.
  96 + * @throws IndexOutOfBoundsException If the value is not found at the specified column.
  97 + */
  98 + public Object[] getRowByValue(int columnIndex, Object value) {
  99 + for (Object[] row : data) {
  100 + if (value.equals(row[columnIndex])) {
  101 + return row;
  102 + }
  103 + }
  104 + throw new IndexOutOfBoundsException(
  105 + "The value " + value + " is not found on the table at the column " + columnIndex);
  106 + }
  107 +
  108 + /**
  109 + * @param rowIndex The index of the row to get.
  110 + * @return A dictionary representing the row, where each key is a column name.
  111 + */
  112 + public Map<String, Object> getRowMapByIndex(int rowIndex) {
  113 + Map<String, Object> row = new HashMap<>();
  114 + for (int i = 0; i < columnsName.length; i++) {
  115 + row.put(columnsName[i], data.get(rowIndex)[i]);
  116 + }
  117 + return row;
  118 + }
  119 +
61 120 }
... ...
src/main/java/eu/omp/irap/vespa/epntapclient/votable/data/VOTableDataCtrl.java
... ... @@ -19,15 +19,14 @@ package eu.omp.irap.vespa.epntapclient.votable.data;
19 19 import java.io.IOException;
20 20 import java.nio.ByteBuffer;
21 21 import java.util.ArrayList;
22   -import java.util.HashMap;
23 22 import java.util.List;
24   -import java.util.Map;
25 23 import java.util.logging.Logger;
26 24  
27 25 import javax.xml.bind.DatatypeConverter;
28 26  
29 27 import com.google.gson.Gson;
30 28  
  29 +import eu.omp.irap.vespa.epntapclient.votable.model.Data;
31 30 import eu.omp.irap.vespa.epntapclient.votable.model.DataType;
32 31 import eu.omp.irap.vespa.epntapclient.votable.model.Field;
33 32 import eu.omp.irap.vespa.epntapclient.votable.model.Stream;
... ... @@ -50,6 +49,10 @@ public class VOTableDataCtrl {
50 49  
51 50 private int nbColumns;
52 51  
  52 + private Data dataNode;
  53 +
  54 + private List<Field> fieldsNodes;
  55 +
53 56 /** The index of the current position in the bytes array. */
54 57 private int cursor;
55 58  
... ... @@ -64,96 +67,42 @@ public class VOTableDataCtrl {
64 67 */
65 68 public VOTableDataCtrl(Table table) throws IOException {
66 69 cursor = 0;
  70 + dataNode = table.getDATA();
67 71  
68   - List<Field> fields = new ArrayList<>();
  72 + fieldsNodes = new ArrayList<>();
69 73 for (Object obj : table.getFIELDOrPARAMOrGROUP()) {
70 74 if (obj.getClass() == Field.class) {
71   - fields.add((Field) obj);
  75 + fieldsNodes.add((Field) obj);
72 76 }
73 77 }
74   - String[] columnsName = new String[fields.size()];
  78 +
  79 + String[] columnsName = new String[fieldsNodes.size()];
75 80 nbColumns = columnsName.length;
76   - for (int i = 0; i < fields.size(); i++) {
77   - columnsName[i] = fields.get(i).getName();
  81 + for (int i = 0; i < fieldsNodes.size(); i++) {
  82 + columnsName[i] = fieldsNodes.get(i).getName();
78 83 }
79 84 data = new VOTableData(columnsName);
80 85 VOTableDataCtrl.logger.info("Columns name: " + new Gson().toJson(columnsName));
  86 + }
81 87  
  88 + public VOTableData parseData() {
82 89 List<Object[]> rows = new ArrayList<>();
83 90  
84   - if (table.getDATA().getBINARY() != null) {
85   - parseBinaryStream(table.getDATA().getBINARY().getSTREAM(), fields);
86   - } else if (table.getDATA().getBINARY2() != null) {
87   - VOTableDataCtrl.parseBinary2Stream(table.getDATA().getBINARY2().getSTREAM(), fields);
88   - } else if (table.getDATA().getTABLEDATA() != null) {
89   - VOTableDataCtrl.parseTableDataStream(table.getDATA().getTABLEDATA(), fields);
90   - } else if (table.getDATA().getFITS() != null) {
91   - VOTableDataCtrl.parseFITSStream(table.getDATA().getFITS().getSTREAM(), fields);
  91 + if (dataNode.getBINARY() != null) {
  92 + parseBinaryStream(dataNode.getBINARY().getSTREAM(), fieldsNodes);
  93 + } else if (dataNode.getBINARY2() != null) {
  94 + VOTableDataCtrl.parseBinary2Stream(dataNode.getBINARY2().getSTREAM(), fieldsNodes);
  95 + } else if (dataNode.getTABLEDATA() != null) {
  96 + VOTableDataCtrl.parseTableDataStream(dataNode.getTABLEDATA(), fieldsNodes);
  97 + } else if (dataNode.getFITS() != null) {
  98 + VOTableDataCtrl.parseFITSStream(dataNode.getFITS().getSTREAM(), fieldsNodes);
92 99 }
93 100  
94 101 Debug.printObject("voTableData", rows);
95   - }
96   -
97   - /**
98   - * @return the data stored in the Table.
99   - */
100   - public VOTableData getData() {
101 102 return data;
102 103 }
103 104  
104 105 /**
105   - * @param column The name of the column to get.
106   - * @return An array of Table fields name.
107   - * @throws IllegalArgumentException Column name not found in the table.
108   - */
109   - public int getColumnIndex(String column) {
110   - int i = 0;
111   - while (!column.equals(data.getColumnsName()[i])) {
112   - i++;
113   - if (i > nbColumns) {
114   - throw new IllegalArgumentException("Column " + column + " not found in the table.");
115   - }
116   - }
117   - return i;
118   - }
119   -
120   - /**
121   - * @param rowIndex The index of the row to get.
122   - * @return The Table row at the specified index.
123   - */
124   - public Object[] getRowByIndex(int rowIndex) {
125   - return data.getData().get(rowIndex);
126   - }
127   -
128   - /**
129   - * @param rowIndex The index of the row to get.
130   - * @return A dictionary representing the row, where each key is a column name.
131   - */
132   - public Map<String, Object> getDicRowByIndex(int rowIndex) {
133   - Map<String, Object> row = new HashMap<>();
134   - for (int i = 0; i < nbColumns; i++) {
135   - row.put(data.getColumnsName()[i], data.getData().get(rowIndex)[i]);
136   - }
137   - return row;
138   - }
139   -
140   - /**
141   - * @param columnIndex A UNIQUE column (as SQL sense) to identify a row.
142   - * @param value The value at `columnName`, in order to get the full row.
143   - * @return A Table row, identified by a unique `value` in the `columnName` column.
144   - * @throws IndexOutOfBoundsException If the value is not found at the specified column.
145   - */
146   - public Object[] getRowByValue(int columnIndex, Object value) {
147   - for (Object[] row : data.getData()) {
148   - if (value.equals(row[columnIndex])) {
149   - return row;
150   - }
151   - }
152   - throw new IndexOutOfBoundsException(
153   - "The value " + value + " is not found on the table at the column " + columnIndex);
154   - }
155   -
156   - /**
157 106 * get the data on its BINARY form.
158 107 *
159 108 * @param voStream the data Stream in the VOTable Table.
... ... @@ -271,4 +220,12 @@ public class VOTableDataCtrl {
271 220 private static void parseFITSStream(Stream stream, List<Field> fields) {
272 221 VOTableDataCtrl.logger.info("Parsing data in FITS stream...");
273 222 }
  223 +
  224 + /**
  225 + * @return the data stored in the Table.
  226 + */
  227 + public VOTableData getData() {
  228 + return data;
  229 + }
  230 +
274 231 }
... ...