diff --git a/.gitignore b/.gitignore index 6d1c1c1..70d1c01 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ target/ *.iml *.ipr !.idea/runConfigurations +!.idea/codeStyles !.idea/copyright ### NetBeans ### diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1e15c02 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index e29a5bd..5fba261 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ org.spacious-team table-wrapper-api - 2020.12 + 2020.12.1 jar Table Wrapper API diff --git a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java index 53533bf..c477c50 100644 --- a/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java +++ b/src/main/java/org/spacious_team/table_wrapper/api/AbstractTable.java @@ -25,12 +25,18 @@ import java.math.BigDecimal; import java.nio.file.Path; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.BiPredicate; -import static java.util.Collections.emptyList; - @Slf4j @ToString(of = {"tableName"}) public abstract class AbstractTable implements Table { @@ -87,17 +93,24 @@ private Map getColumnIndices(ReportPage reportPage, TableC * Extracts exactly one object from excel row */ public List getData(Path file, BiFunction rowExtractor) { - return getDataCollection(file, (table, row) -> - Optional.ofNullable(rowExtractor.apply(table, row)) - .map(Collections::singletonList) - .orElse(emptyList())); + return getDataCollection(file, (row, data) -> { + T result = rowExtractor.apply(this, row); + if (result != null) { + data.add(result); + } + }); } /** * Extracts objects from excel table without duplicate objects handling (duplicated row are both will be returned) */ public List getDataCollection(Path file, BiFunction> rowExtractor) { - return getDataCollection(file, rowExtractor, Object::equals, Arrays::asList); + return getDataCollection(file, (row, data) -> { + Collection result = rowExtractor.apply(this, row); + if (result != null) { + data.addAll(result); + } + }); } /** @@ -106,18 +119,25 @@ public List getDataCollection(Path file, BiFunction List getDataCollection(Path file, BiFunction> rowExtractor, BiPredicate equalityChecker, BiFunction> mergeDuplicates) { + return getDataCollection(file, (row, data) -> { + Collection result = rowExtractor.apply(this, row); + if (result != null) { + for (T r : result) { + addWithEqualityChecker(r, data, equalityChecker, mergeDuplicates); + } + } + }); + } + + private List getDataCollection(Path file, BiConsumer> rowHandler) { List data = new ArrayList<>(); for (TableRow row : this) { if (row != null) { try { - Collection result = rowExtractor.apply(this, row); - if (result != null) { - for (T r : result) { - addWithEqualityChecker(r, data, equalityChecker, mergeDuplicates); - } - } + rowHandler.accept(row, data); } catch (Exception e) { - log.warn("Не могу распарсить таблицу '{}' в файле {}, строка {}", tableName, file.getFileName(), row.getRowNum() + 1, e); + log.warn("Не могу распарсить таблицу '{}' в файле {}, строка {}", + tableName, file.getFileName(), row.getRowNum() + 1, e); } } } @@ -127,7 +147,7 @@ public List getDataCollection(Path file, BiFunction void addWithEqualityChecker(T element, Collection collection, BiPredicate equalityChecker, - BiFunction> mergeDuplicates) { + BiFunction> duplicatesMerger) { T equalsObject = null; for (T e : collection) { if (equalityChecker.test(e, element)) { @@ -137,7 +157,7 @@ public static void addWithEqualityChecker(T element, } if (equalsObject != null) { collection.remove(equalsObject); - collection.addAll(mergeDuplicates.apply(equalsObject, element)); + collection.addAll(duplicatesMerger.apply(equalsObject, element)); } else { collection.add(element); }