All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.annot8.common.data.bounds.MultiCellBounds Maven / Gradle / Ivy

The newest version!
/* Annot8 (annot8.io) - Licensed under Apache-2.0. */
package io.annot8.common.data.bounds;

import io.annot8.api.data.Content;
import io.annot8.common.data.content.Row;
import io.annot8.common.data.content.TableContent;
import jakarta.json.bind.annotation.JsonbCreator;
import jakarta.json.bind.annotation.JsonbProperty;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Bounds to represent multiple cells within a table row
 *
 * 

This bounds implementation will only return instances of MultiCellData with getData */ public class MultiCellBounds extends AbstractCellBounds { private final int[] cells; private final int row; @JsonbCreator public MultiCellBounds(@JsonbProperty("row") int row, @JsonbProperty("cells") int[] cells) { this.row = row; this.cells = cells; } public int getRow() { return row; } public int[] getCells() { return cells; } @Override public , R> Optional getData(C content, Class requiredClass) { if (requiredClass.equals(MultiCellData.class)) { if (!isValid(content)) { return Optional.empty(); } TableContent tableContent = (TableContent) content; Optional rowOptional = tableContent.getData().getRow(this.row); if (rowOptional.isPresent()) { Row r = rowOptional.get(); Map values = new HashMap<>(); Map headers = new HashMap<>(); for (int i : cells) { Optional valueOptional = r.getValueAt(i); Optional headerOptional = r.getColumnName(i); if (valueOptional.isPresent()) { values.put(i, valueOptional.get()); headerOptional.ifPresent(s -> headers.put(s, i)); } } MultiCellData multiCellData = new MultiCellData(values, headers); return Optional.of(requiredClass.cast(multiCellData)); } } return Optional.empty(); } @Override public > boolean isValid(C content) { if (!super.isValid(content)) { return false; } for (int i : cells) { if (!isCellReferenceValid((TableContent) content, row, i)) { return false; } } return true; } }