Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
@ToString(of = {"tableName"})
public abstract class AbstractTable<R extends ReportPageRow> implements Table {

@Getter
protected final AbstractReportPage<R> reportPage;
protected final String tableName;
@Getter
Expand Down Expand Up @@ -223,13 +224,18 @@ public boolean hasNext() {
public TableRow next() {
R row;
do {
row = reportPage.getRow(tableRange.getFirstRow() + (i++));
row = getRow(tableRange.getFirstRow() + (i++));
} while (row == null && hasNext());
tableRow.setRow(row);
return tableRow;
}
}

@Override
public R getRow(int i) {
return reportPage.getRow(i);
}

@Override
public TableRow findRow(Object value) {
TableCellAddress address = reportPage.find(value);
Expand All @@ -245,7 +251,7 @@ public TableRow findRowByPrefix(String prefix) {
private MutableTableRow<R> getMutableTableRow(TableCellAddress address) {
if (tableRange.contains(address)) {
MutableTableRow<R> tableRow = new MutableTableRow<>(this, getCellDataAccessObject());
tableRow.setRow(reportPage.getRow(address.getRow()));
tableRow.setRow(getRow(address.getRow()));
return tableRow;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import java.util.regex.Pattern;

/**
* Implementation is the stateless singleton cell helper class
* @apiNote Impl may have parameters that affect how the value is parsed,
* for example DataTimeFormat that changes behavior of date time value parser.
*/
public interface CellDataAccessObject<C, R extends ReportPageRow> {

Expand All @@ -39,14 +40,14 @@ public interface CellDataAccessObject<C, R extends ReportPageRow> {
Object getValue(C cell);

/**
* @throws RuntimeException if can't extract int value
* @throws RuntimeException if method can't extract int value
*/
default int getIntValue(C cell) {
return (int) getLongValue(cell);
}

/**
* @throws RuntimeException if can't extract long value
* @throws RuntimeException if method can't extract long value
*/
default long getLongValue(C cell) {
Object value = getValue(cell);
Expand All @@ -60,7 +61,7 @@ default long getLongValue(C cell) {
}

/**
* @throws RuntimeException if can't extract Double value
* @throws RuntimeException if method can't extract Double value
*/
default double getDoubleValue(C cell) {
Object value = getValue(cell);
Expand All @@ -84,7 +85,7 @@ default double getDoubleValue(C cell) {
}

/**
* @throws RuntimeException if can't extract BigDecimal value
* @throws RuntimeException if method can't extract BigDecimal value
* @see <a href="https://stackoverflow.com/questions/6787142/bigdecimal-equals-versus-compareto">Stack overflow</a>
* for BigDecimal values equality
*/
Expand All @@ -96,19 +97,19 @@ default BigDecimal getBigDecimalValue(C cell) {
}

/**
* @throws RuntimeException if can't extract string value
* @throws RuntimeException if method can't extract string value
*/
default String getStringValue(C cell) {
return getValue(cell).toString();
}

/**
* @throws RuntimeException if can't extract instant value
* @throws RuntimeException if method can't extract instant value
*/
Instant getInstantValue(C cell);

/**
* @throws RuntimeException if can't extract local date time value
* @throws RuntimeException if method can't extract local date time value
*/
default LocalDateTime getLocalDateTimeValue(C cell) {
return getInstantValue(cell)
Expand All @@ -122,55 +123,55 @@ default Object getValue(R row, Integer cellIndex) {
}

/**
* @throws RuntimeException if can't extract int value
* @throws RuntimeException if method can't extract int value
*/
default int getIntValue(R row, Integer cellIndex) {
C cell = getCell(row, cellIndex);
return getIntValue(cell);
}

/**
* @throws RuntimeException if can't extract long value
* @throws RuntimeException if method can't extract long value
*/
default long getLongValue(R row, Integer cellIndex) {
C cell = getCell(row, cellIndex);
return getLongValue(cell);
}

/**
* @throws RuntimeException if can't extract Double value
* @throws RuntimeException if method can't extract Double value
*/
default double getDoubleValue(R row, Integer cellIndex) {
C cell = getCell(row, cellIndex);
return getDoubleValue(cell);
}

/**
* @throws RuntimeException if can't extract BigDecimal value
* @throws RuntimeException if method can't extract BigDecimal value
*/
default BigDecimal getBigDecimalValue(R row, Integer cellIndex) {
C cell = getCell(row, cellIndex);
return getBigDecimalValue(cell);
}

/**
* @throws RuntimeException if can't extract string value
* @throws RuntimeException if method can't extract string value
*/
default String getStringValue(R row, Integer cellIndex) {
C cell = getCell(row, cellIndex);
return getStringValue(cell);
}

/**
* @throws RuntimeException if can't extract instant value
* @throws RuntimeException if method can't extract instant value
*/
default Instant getInstantValue(R row, Integer cellIndex) {
C cell = getCell(row, cellIndex);
return getInstantValue(cell);
}

/**
* @throws RuntimeException if can't extract local date time value
* @throws RuntimeException if method can't extract local date time value
*/
default LocalDateTime getLocalDateTimeValue(R row, Integer cellIndex) {
C cell = getCell(row, cellIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@

/**
* Mutable implementation. Used by {@link AbstractTable#iterator()} and {@link AbstractTable#stream()} to eliminate
* heap pollution. Use {@link #clone()} for using variable outside iterator or stream.
* heap pollution. On each iteration {@link #row} field is updated. Call {@link #clone()} instead of using this object
* outside iterator or stream, that safer in outside code, because {@link #row} field holds value even if iterator
* will continue to work.
*/
@Data
class MutableTableRow<T extends ReportPageRow> implements TableRow {
Expand Down Expand Up @@ -78,51 +80,30 @@ public Object getCellValue(TableColumnDescription column) {
return dao.getValue(row, getCellIndex(column));
}

/**
* @throws RuntimeException if can't extract int value
*/
public int getIntCellValue(TableColumnDescription column) {
return dao.getIntValue(row, getCellIndex(column));
}

/**
* @throws RuntimeException if can't extract long value
*/
public long getLongCellValue(TableColumnDescription column) {
return dao.getLongValue(row, getCellIndex(column));
}

/**
* @throws RuntimeException if can't extract Double value
*/
public double getDoubleCellValue(TableColumnDescription column) {
return dao.getDoubleValue(row, getCellIndex(column));
}

/**
* @throws RuntimeException if can't extract BigDecimal value
*/
public BigDecimal getBigDecimalCellValue(TableColumnDescription column) {
return dao.getBigDecimalValue(row, getCellIndex(column));
}

/**
* @throws RuntimeException if can't extract string value
*/
public String getStringCellValue(TableColumnDescription column) {
return dao.getStringValue(row, getCellIndex(column));
}

/**
* @throws RuntimeException if can't extract instant value
*/
public Instant getInstantCellValue(TableColumnDescription column) {
return dao.getInstantValue(row, getCellIndex(column));
}

/**
* @throws RuntimeException if can't extract local date time value
*/
public LocalDateTime getLocalDateTimeCellValue(TableColumnDescription column) {
return dao.getLocalDateTimeValue(row, getCellIndex(column));
}
Expand All @@ -133,15 +114,15 @@ private Integer getCellIndex(TableColumnDescription column) {
}

/**
* Object is mutable.
* Clone it if it should be outside table rows loop block, {@link Table#iterator()} or {@link Table#stream()}
* This object is mutable.
* Clone it if it should be used outside table rows loop block ({@link Table#iterator()} or {@link Table#stream()}).
* Cloned object is safe use everywhere, this object should be used oly inside of one iteration
* of {@link Table#iterator()} or {@link Table#stream()}
*/
@SuppressWarnings("unchecked")
public MutableTableRow<T> clone() {
try {
MutableTableRow<T> tableRow = (MutableTableRow<T>) super.clone();
tableRow.setRow(row);
return tableRow;
return (MutableTableRow<T>) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Can't clone " + this.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ default Object getNextColumnValue(String firstColumnValuePrefix) {
/**
* @param i zero-based index
* @return row object or null is row does not exist
* @apiNote Method impl should return {@link CellDataAccessObject} aware {@link ReportPageRow} impl
*/
ReportPageRow getRow(int i);

Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/spacious_team/table_wrapper/api/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
@SuppressWarnings("unused")
public interface Table extends Iterable<TableRow> {

/**
* Report page this table belongs to
*/
ReportPage getReportPage();

/**
* Extracts exactly one object from excel row
*/
Expand Down Expand Up @@ -58,6 +63,13 @@ <T> List<T> getDataCollection(Object report, Function<TableRow, Collection<T>> r

Stream<TableRow> stream();

/**
* @param i zero-based index
* @return row object or null is row does not exist
* @apiNote Method impl should return {@link CellDataAccessObject} aware {@link ReportPageRow} impl
*/
ReportPageRow getRow(int i);

/**
* @return row containing cell with exact value or null if not found
*/
Expand All @@ -71,7 +83,7 @@ <T> List<T> getDataCollection(Object report, Function<TableRow, Collection<T>> r
Map<TableColumn, Integer> getHeaderDescription();

/**
* By default table iterates throw all rows, call method if last row is "total" row and it should be excluded
* By default, table iterates throw all rows, call method if last row is "total" row, and it should be excluded
*/
default Table excludeTotalRow() {
return subTable(0, -1);
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/org/spacious_team/table_wrapper/api/TableRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,43 @@ public interface TableRow extends ReportPageRow, Cloneable {

TableCell getCell(TableColumnDescription column);

/**
* Returns cell's native value
*/
Object getCellValue(TableColumnDescription column);

/**
* @throws RuntimeException if can't extract int value
* @throws RuntimeException if method can't extract int value
*/
int getIntCellValue(TableColumnDescription column);

/**
* @throws RuntimeException if can't extract long value
* @throws RuntimeException if method can't extract long value
*/
long getLongCellValue(TableColumnDescription column);

/**
* @throws RuntimeException if can't extract Double value
* @throws RuntimeException if method can't extract Double value
*/
double getDoubleCellValue(TableColumnDescription column);

/**
* @throws RuntimeException if can't extract BigDecimal value
* @throws RuntimeException if method can't extract BigDecimal value
*/
BigDecimal getBigDecimalCellValue(TableColumnDescription column);

/**
* @throws RuntimeException if can't extract string value
* @throws RuntimeException if method can't extract string value
*/
String getStringCellValue(TableColumnDescription column);

/**
* @throws RuntimeException if can't extract instant value
* @throws RuntimeException if method can't extract instant value
*/
Instant getInstantCellValue(TableColumnDescription column);

/**
* @throws RuntimeException if can't extract local date time value
* @throws RuntimeException if method can't extract local date time value
*/
LocalDateTime getLocalDateTimeCellValue(TableColumnDescription column);

Expand Down