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

io.annot8.common.data.bounds.CellBounds 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.api.exceptions.Annot8RuntimeException;
import io.annot8.common.data.content.Row;
import io.annot8.common.data.content.Table;
import io.annot8.common.data.content.TableContent;
import jakarta.json.bind.annotation.JsonbCreator;
import jakarta.json.bind.annotation.JsonbProperty;
import java.util.Optional;

public class CellBounds extends AbstractCellBounds {

  private final int row;
  private final int column;

  @JsonbCreator
  public CellBounds(@JsonbProperty("row") int row, @JsonbProperty("column") int column) {
    this.row = row;
    this.column = column;
  }

  @Override
  public , R> Optional getData(C content, Class requiredClass) {
    if (isValid(content)) {
      Table table = (Table) content.getData();
      Optional optional = table.getRow(row);
      if (optional.isEmpty()) {
        throw new Annot8RuntimeException("Failed to find row");
      }
      Optional value = optional.get().getValueAt(column);
      return value.map(requiredClass::cast);
    }
    return Optional.empty();
  }

  @Override
  public > boolean isValid(C content) {
    return super.isValid(content) && isCellReferenceValid((TableContent) content, row, column);
  }

  public int getRow() {
    return row;
  }

  public int getColumn() {
    return column;
  }
}