import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.util.HashMap; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; public class UpdatePageTableValues { private HashMap rows = new HashMap<>(); /** * default constructor that takes any amount of elements and finds the table rows of them. * * @param elements any amount of elements, these dont have to be of type TR since they get filtered anyways */ public UpdatePageTableValues(Elements elements) { // // get all table rows // var tableRows = elements .parallelStream() .filter(element -> element.tagName().equals("tr") && element.attr("id").contains("_")) .collect(Collectors.toList()); parseTableRows(tableRows); } /** * returns the row number provided a row element * * @param element row element that is used to get row number from * @return row number or null if none */ private static Optional getGetColumnNumOfTdElement(Element element) { // only parse td tags if(!element.tagName().equals("td")) return Optional.of(null); var subStrs = element.attr("id").split("_"); // We should have at least 3 sub strings // // bc58d56c-767a-47d6-80c3-021fd8079417 (uuid of the update) // C2 (column number) // R3 (row number) if(subStrs.length >= 3) { try { return Optional.of(Long.parseLong(subStrs[1].replace("C", ""))); } catch (Exception e) { e.printStackTrace(); } } // return null otherwise return Optional.of(null); } /** * gers row number of table row element * @param element * @return table row number */ private static Optional getRowNumOfTrElement(Element element) { if(!element.tagName().equals("tr")) return Optional.of(null); var subStrs = element.attr("id").split("_"); // We should have at least 2 sub strings // // bc58d56c-767a-47d6-80c3-021fd8079417 (uuid of the update) // R3 (row number) if (subStrs.length >= 2) { try { return Optional.of(Long.parseLong(subStrs[1].replace("R", ""))); } catch (Exception e) { e.printStackTrace(); } } // return null otherwise return Optional.of(null); } /** * fill rows hashmap with rows of data * @param tableRows raw html table row elements */ private void parseTableRows(List tableRows) { tableRows.parallelStream().forEach(tr -> { // // try catching for .split // try { rows.put(getRowNumOfTrElement(tr).get(), new UpdatePageRow( tr.child(1).text(), tr.child(2).text(), tr.child(3).text(), tr.child(4).text(), tr.child(5).text(), tr.child(6).text(), tr.child(7).attr("id").split("_")[0] )); } catch (Exception e) { e.printStackTrace(); } }); } /** * get all the driver uuid's * @return list of driver uuid's */ public List getDriverUuids() { return rows .entrySet() .parallelStream() .map(v -> v.getValue().uuid) .collect(Collectors.toList()); } }