Commit aeee6b4078fb701c86c89e224b5a28c4bf7fcc98
1 parent
865564b8
Exists in
master
Add methods to DataParser to get rows.
Showing
1 changed file
with
70 additions
and
3 deletions
Show diff stats
src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableDataParser.java
1 | -/** | |
1 | +/* | |
2 | 2 | * This file is a part of EpnTAPClient. |
3 | 3 | * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer. |
4 | 4 | * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861 |
... | ... | @@ -13,11 +13,14 @@ |
13 | 13 | * the GNU General Public License along with this program. If not, see |
14 | 14 | * <http://www.gnu.org/licenses/>. |
15 | 15 | */ |
16 | + | |
16 | 17 | package eu.omp.irap.vespa.epntapclient.votable.controller; |
17 | 18 | |
18 | 19 | import java.util.ArrayList; |
19 | 20 | import java.util.Base64; |
21 | +import java.util.HashMap; | |
20 | 22 | import java.util.List; |
23 | +import java.util.Map; | |
21 | 24 | import java.util.Vector; |
22 | 25 | |
23 | 26 | import com.google.gson.Gson; |
... | ... | @@ -89,20 +92,84 @@ public class VOTableDataParser { |
89 | 92 | } |
90 | 93 | |
91 | 94 | /** |
92 | - * @return the data stored in the VOTable. | |
95 | + * @return the data stored in the Table. | |
93 | 96 | */ |
94 | 97 | public List<Object[]> getDataArray() { |
95 | 98 | return data; |
96 | 99 | } |
97 | 100 | |
98 | 101 | /** |
99 | - * @return An array of VOTable fields name. | |
102 | + * @return An array of Table fields name. | |
100 | 103 | */ |
101 | 104 | public String[] getColumnsName() { |
102 | 105 | return columnsName; |
103 | 106 | } |
104 | 107 | |
105 | 108 | /** |
109 | + * @param columnName The name of the column to get. | |
110 | + * @return An array of Table fields name. | |
111 | + * @throws Exception If the column name is not found. | |
112 | + */ | |
113 | + public int getColumnIndex(String columnName) throws Exception { | |
114 | + int i = 0; | |
115 | + while (!columnName.equals(columnsName[i])) { | |
116 | + i++; | |
117 | + if (i > columnsName.length) | |
118 | + throw new Exception("Column name " + columnName + "not found in the table."); | |
119 | + } | |
120 | + return i; | |
121 | + } | |
122 | + | |
123 | + /** | |
124 | + * @return The number of rows in the Table. | |
125 | + */ | |
126 | + public int getNbRows() { | |
127 | + return data.size(); | |
128 | + } | |
129 | + | |
130 | + /** | |
131 | + * @return The number of columns in the Table. | |
132 | + */ | |
133 | + public int getNbColumns() { | |
134 | + return columnsName.length; | |
135 | + } | |
136 | + | |
137 | + /** | |
138 | + * @param rowIndex The index of the row to get. | |
139 | + * @return The Table row at the specified index. | |
140 | + */ | |
141 | + public Object[] getRowByIndex(int rowIndex) { | |
142 | + return data.get(rowIndex); | |
143 | + } | |
144 | + | |
145 | + /** | |
146 | + * @param rowIndex The index of the row to get. | |
147 | + * @return A dictionary representing the row, where each key is a column name. | |
148 | + */ | |
149 | + public Map<String, Object> getDicRowByIndex(int rowIndex) { | |
150 | + Map<String, Object> row = new HashMap<>(); | |
151 | + for (int i = 0; i < columnsName.length; i++) { | |
152 | + row.put(columnsName[i], data.get(rowIndex)[i]); | |
153 | + } | |
154 | + return row; | |
155 | + } | |
156 | + | |
157 | + /** | |
158 | + * @param columnIndex A UNIQUE column (as SQL sense) to identify a row. | |
159 | + * @param value The value at `columnName`, in order to get the full row. | |
160 | + * @return A Table row, identified by a unique `value` in the `columnName` column. | |
161 | + * @throws Exception If the value is not found at the specified column. | |
162 | + */ | |
163 | + public Object[] getRowByValue(int columnIndex, Object value) throws Exception { | |
164 | + for (Object[] row : data) { | |
165 | + if (value.equals(row[columnIndex])) | |
166 | + return row; | |
167 | + } | |
168 | + throw new Exception( | |
169 | + "The value " + value + " is not found on the table at the column " + columnIndex); | |
170 | + } | |
171 | + | |
172 | + /** | |
106 | 173 | * get the data on its BINARY form. |
107 | 174 | * |
108 | 175 | * @param stream the data Stream in the VOTable Table. | ... | ... |