Skip to content

Commit 22482c3

Browse files
authored
Merge pull request #7 from spacious-team/develop
Релиз 2022.4
2 parents f1ebd4a + 4613076 commit 22482c3

File tree

6 files changed

+73
-124
lines changed

6 files changed

+73
-124
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
<groupId>org.spacious-team</groupId>
2626
<artifactId>table-wrapper-excel-impl</artifactId>
27-
<version>2022.2</version>
27+
<version>2022.4</version>
2828
<packaging>jar</packaging>
2929

3030
<name>Table Wrapper API Excel Implementation</name>
@@ -71,7 +71,7 @@
7171
<dependency>
7272
<groupId>com.github.spacious-team</groupId>
7373
<artifactId>table-wrapper-api</artifactId>
74-
<version>2022.2</version>
74+
<version>2022.4</version>
7575
</dependency>
7676
<dependency>
7777
<groupId>org.apache.poi</groupId>

src/main/java/org/spacious_team/table_wrapper/excel/ExcelCellDataAccessObject.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,7 @@ public Cell getCell(ExcelTableRow row, Integer cellIndex) {
3535

3636
@Override
3737
public Object getValue(Cell cell) {
38-
return switch (cell.getCellType()) {
39-
case STRING -> cell.getStringCellValue();
40-
case NUMERIC -> cell.getNumericCellValue(); // return double
41-
case BLANK -> null;
42-
case BOOLEAN -> cell.getBooleanCellValue();
43-
case FORMULA -> getCachedFormulaValue(cell);
44-
case ERROR -> throw new RuntimeException("Ячейка содержит ошибку вычисления формулы: " +
45-
FormulaError.forInt(cell.getErrorCellValue()));
46-
case _NONE -> null;
47-
};
38+
return ExcelTableHelper.getValue(cell);
4839
}
4940

5041
@Override
@@ -66,15 +57,4 @@ public Instant getInstantValue(Cell cell) {
6657
public LocalDateTime getLocalDateTimeValue(Cell cell) {
6758
return cell.getLocalDateTimeCellValue();
6859
}
69-
70-
private static Object getCachedFormulaValue(Cell cell) {
71-
return switch (cell.getCachedFormulaResultType()) {
72-
case BOOLEAN -> cell.getBooleanCellValue();
73-
case NUMERIC -> cell.getNumericCellValue();
74-
case STRING -> cell.getRichStringCellValue();
75-
case ERROR -> throw new RuntimeException("Ячейка не содержит кешированный результат формулы: " +
76-
FormulaError.forInt(cell.getErrorCellValue()));
77-
default -> null; //never should occur
78-
};
79-
}
8060
}

src/main/java/org/spacious_team/table_wrapper/excel/ExcelSheet.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.spacious_team.table_wrapper.api.AbstractReportPage;
2727
import org.spacious_team.table_wrapper.api.TableCellAddress;
2828

29-
import java.util.function.BiPredicate;
29+
import java.util.function.Predicate;
3030

3131
@RequiredArgsConstructor
3232
public class ExcelSheet extends AbstractReportPage<ExcelTableRow> {
@@ -35,9 +35,15 @@ public class ExcelSheet extends AbstractReportPage<ExcelTableRow> {
3535
private final Sheet sheet;
3636

3737
@Override
38-
public TableCellAddress find(Object value, int startRow, int endRow, int startColumn, int endColumn,
39-
BiPredicate<String, Object> stringPredicate) {
40-
return ExcelTableHelper.find(sheet, value, startRow, endRow, startColumn, endColumn, stringPredicate);
38+
public TableCellAddress find(Object value, int startRow, int endRow, int startColumn, int endColumn) {
39+
return ExcelTableHelper.find(sheet, value, startRow, endRow, startColumn, endColumn);
40+
}
41+
42+
@Override
43+
public TableCellAddress find(int startRow, int endRow, int startColumn, int endColumn,
44+
Predicate<Object> cellValuePredicate) {
45+
return ExcelTableHelper.find(sheet, startRow, endRow, startColumn, endColumn,
46+
(cell) -> cellValuePredicate.test(ExcelTableHelper.getValue(cell)));
4147
}
4248

4349
@Override

src/main/java/org/spacious_team/table_wrapper/excel/ExcelTableFactory.java

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.spacious_team.table_wrapper.api.AbstractTableFactory;
2222
import org.spacious_team.table_wrapper.api.ReportPage;
2323
import org.spacious_team.table_wrapper.api.Table;
24+
import org.spacious_team.table_wrapper.api.TableCellRange;
2425
import org.spacious_team.table_wrapper.api.TableColumnDescription;
2526

2627
public class ExcelTableFactory extends AbstractTableFactory<ExcelSheet> {
@@ -29,56 +30,16 @@ public ExcelTableFactory() {
2930
super(ExcelSheet.class);
3031
}
3132

32-
public Table create(ReportPage reportPage,
33-
String tableName,
34-
String lastRowString,
35-
Class<? extends TableColumnDescription> headerDescription,
36-
int headersRowCount) {
37-
return new ExcelTable(cast(reportPage),
38-
tableName,
39-
reportPage.getTableCellRange(tableName, headersRowCount, lastRowString),
40-
headerDescription,
41-
headersRowCount);
42-
}
43-
4433
@Override
4534
public Table create(ReportPage reportPage,
4635
String tableName,
36+
TableCellRange tableRange,
4737
Class<? extends TableColumnDescription> headerDescription,
4838
int headersRowCount) {
49-
return new ExcelTable(cast(reportPage),
39+
return new ExcelTable(
40+
cast(reportPage),
5041
tableName,
51-
reportPage.getTableCellRange(tableName, headersRowCount),
52-
headerDescription,
53-
headersRowCount);
54-
}
55-
56-
@Override
57-
public Table createNameless(ReportPage reportPage,
58-
String providedTableName,
59-
String firstRowString,
60-
String lastRowString,
61-
Class<? extends TableColumnDescription> headerDescription,
62-
int headersRowCount) {
63-
return new ExcelTable(cast(reportPage),
64-
providedTableName,
65-
reportPage.getTableCellRange(firstRowString, headersRowCount, lastRowString)
66-
.addRowsToTop(1),
67-
headerDescription,
68-
headersRowCount);
69-
}
70-
71-
72-
@Override
73-
public Table createNameless(ReportPage reportPage,
74-
String providedTableName,
75-
String firstRowString,
76-
Class<? extends TableColumnDescription> headerDescription,
77-
int headersRowCount) {
78-
return new ExcelTable(cast(reportPage),
79-
providedTableName,
80-
reportPage.getTableCellRange(firstRowString, headersRowCount)
81-
.addRowsToTop(1),
42+
tableRange,
8243
headerDescription,
8344
headersRowCount);
8445
}

src/main/java/org/spacious_team/table_wrapper/excel/ExcelTableHelper.java

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,56 @@
1919
package org.spacious_team.table_wrapper.excel;
2020

2121
import org.apache.poi.ss.usermodel.Cell;
22-
import org.apache.poi.ss.usermodel.CellType;
22+
import org.apache.poi.ss.usermodel.FormulaError;
2323
import org.apache.poi.ss.usermodel.Row;
2424
import org.apache.poi.ss.usermodel.Sheet;
2525
import org.apache.poi.ss.util.CellAddress;
2626
import org.spacious_team.table_wrapper.api.TableCellAddress;
2727

28-
import java.util.function.BiPredicate;
28+
import java.util.Objects;
2929
import java.util.function.Predicate;
3030

3131
import static org.spacious_team.table_wrapper.api.TableCellAddress.NOT_FOUND;
3232

3333
class ExcelTableHelper {
3434

3535
/**
36-
* @param value searching value
37-
* @param startRow search rows start from this
38-
* @param endRow search rows excluding this, can handle values greater than real rows count
36+
* @param value searching value
37+
* @param startRow search rows start from this
38+
* @param endRow search rows excluding this, can handle values greater than real rows count
3939
* @param startColumn search columns start from this
40-
* @param endColumn search columns excluding this, can handle values greater than real columns count
40+
* @param endColumn search columns excluding this, can handle values greater than real columns count
4141
* @return table cell address or {@link TableCellAddress#NOT_FOUND}
4242
*/
43-
static TableCellAddress find(Sheet sheet, Object value, int startRow, int endRow,
43+
static TableCellAddress find(Sheet sheet, Object value,
44+
int startRow, int endRow,
45+
int startColumn, int endColumn) {
46+
Object expected = (value instanceof Number) ?
47+
((Number) value).doubleValue() : // excel store Numbers as doubles
48+
value;
49+
return find(sheet, startRow, endRow, startColumn, endColumn, (cell) -> equals(cell, expected));
50+
}
51+
52+
/**
53+
* @param startRow search rows start from this
54+
* @param endRow search rows excluding this, can handle values greater than real rows count
55+
* @param startColumn search columns start from this
56+
* @param endColumn search columns excluding this, can handle values greater than real columns count
57+
* @return table cell address or {@link TableCellAddress#NOT_FOUND}
58+
*/
59+
static TableCellAddress find(Sheet sheet, int startRow, int endRow,
4460
int startColumn, int endColumn,
45-
BiPredicate<String, Object> stringPredicate) {
46-
if (sheet.getLastRowNum() == -1) {
47-
return NOT_FOUND;
48-
} else if (endRow > sheet.getLastRowNum()) {
49-
endRow = sheet.getLastRowNum() + 1; // endRow is exclusive
50-
}
51-
CellType type = getType(value);
52-
if (type == CellType.NUMERIC) {
53-
value = ((Number) value).doubleValue();
54-
}
55-
for(int rowNum = startRow; rowNum < endRow; rowNum++) {
61+
Predicate<Cell> predicate) {
62+
startRow = Math.max(0, startRow);
63+
endRow = Math.min(endRow, sheet.getLastRowNum() + 1); // endRow is exclusive
64+
for (int rowNum = startRow; rowNum < endRow; rowNum++) {
5665
Row row = sheet.getRow(rowNum);
5766
if (row == null) continue;
5867
for (Cell cell : row) {
5968
if (cell != null) {
6069
int column = cell.getColumnIndex();
61-
if (startColumn <= column && column < endColumn && cell.getCellType() == type) {
62-
if (compare(value, cell, stringPredicate)) {
70+
if (startColumn <= column && column < endColumn) {
71+
if (predicate.test(cell)) {
6372
CellAddress address = cell.getAddress();
6473
return new TableCellAddress(address.getRow(), address.getColumn());
6574
}
@@ -70,43 +79,36 @@ static TableCellAddress find(Sheet sheet, Object value, int startRow, int endRow
7079
return NOT_FOUND;
7180
}
7281

73-
private static TableCellAddress findByPredicate(Sheet sheet, int startRow, Predicate<Cell> predicate) {
74-
int endRow = sheet.getLastRowNum() + 1;
75-
for(int rowNum = startRow; rowNum < endRow; rowNum++) {
76-
Row row = sheet.getRow(rowNum);
77-
if (row == null) continue;
78-
for (Cell cell : row) {
79-
if (predicate.test(cell)) {
80-
CellAddress address = cell.getAddress();
81-
return new TableCellAddress(address.getRow(), address.getColumn());
82-
}
83-
}
84-
}
85-
return NOT_FOUND;
82+
static Object getValue(Cell cell) {
83+
return switch (cell.getCellType()) {
84+
case STRING -> cell.getStringCellValue();
85+
case NUMERIC -> cell.getNumericCellValue(); // returns double
86+
case BLANK -> null;
87+
case BOOLEAN -> cell.getBooleanCellValue();
88+
case FORMULA -> getCachedFormulaValue(cell);
89+
case ERROR -> throw new RuntimeException("Ячейка содержит ошибку вычисления формулы: " +
90+
FormulaError.forInt(cell.getErrorCellValue()));
91+
case _NONE -> null;
92+
};
8693
}
8794

88-
private static CellType getType(Object value) {
89-
CellType type;
90-
if (value instanceof String) {
91-
type = (((String) value).isEmpty()) ? CellType.BLANK : CellType.STRING;
92-
} else if (value instanceof Number) {
93-
type = CellType.NUMERIC;
94-
} else if (value instanceof Boolean) {
95-
type = CellType.BOOLEAN;
96-
} else if (value == null) {
97-
type = CellType.BLANK;
98-
} else {
99-
throw new IllegalArgumentException("Не могу сравнить значение '" + value + "' типа " + value.getClass().getName());
100-
}
101-
return type;
95+
private static Object getCachedFormulaValue(Cell cell) {
96+
return switch (cell.getCachedFormulaResultType()) {
97+
case BOOLEAN -> cell.getBooleanCellValue();
98+
case NUMERIC -> cell.getNumericCellValue();
99+
case STRING -> cell.getRichStringCellValue();
100+
case ERROR -> throw new RuntimeException("Ячейка не содержит кешированный результат формулы: " +
101+
FormulaError.forInt(cell.getErrorCellValue()));
102+
default -> null; // never should occur
103+
};
102104
}
103105

104-
private static boolean compare(Object value, Cell cell, BiPredicate<String, Object> stringPredicate) {
106+
private static boolean equals(Cell cell, Object expected) {
105107
return switch (cell.getCellType()) {
106-
case BLANK -> value == null || value.equals("");
107-
case STRING -> stringPredicate.test(cell.getStringCellValue(), value);
108-
case NUMERIC -> (value instanceof Number) && (cell.getNumericCellValue() - ((Number) value).doubleValue()) < 1e-6;
109-
case BOOLEAN -> value.equals(cell.getBooleanCellValue());
108+
case BLANK -> expected == null || expected.equals("");
109+
case STRING -> (expected instanceof CharSequence) && Objects.equals(cell.getStringCellValue(), expected.toString());
110+
case NUMERIC -> (expected instanceof Number) && Math.abs((cell.getNumericCellValue() - ((Number) expected).doubleValue())) < 1e-6;
111+
case BOOLEAN -> expected.equals(cell.getBooleanCellValue());
110112
default -> false;
111113
};
112114
}

src/main/java/org/spacious_team/table_wrapper/excel/ExcelTableRow.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public int getLastCellNum() {
5959

6060
public boolean rowContains(Object value) {
6161
return ExcelTableHelper.find(row.getSheet(), value, row.getRowNum(), row.getRowNum() + 1,
62-
0, Integer.MAX_VALUE, String::equals) != NOT_FOUND;
62+
0, Integer.MAX_VALUE) != NOT_FOUND;
6363
}
6464

6565
@Override

0 commit comments

Comments
 (0)