Skip to content

Commit

Permalink
Merge pull request #6 from spacious-team/develop
Browse files Browse the repository at this point in the history
Релиз 2020.12.1
  • Loading branch information
vananiev authored Nov 29, 2020
2 parents 047107c + cf8bb63 commit 54086df
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ target/
*.iml
*.ipr
!.idea/runConfigurations
!.idea/codeStyles
!.idea/copyright

### NetBeans ###
Expand Down
8 changes: 8 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<groupId>org.spacious-team</groupId>
<artifactId>table-wrapper-api</artifactId>
<version>2020.12</version>
<version>2020.12.1</version>
<packaging>jar</packaging>

<name>Table Wrapper API</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -87,17 +93,24 @@ private Map<TableColumn, Integer> getColumnIndices(ReportPage reportPage, TableC
* Extracts exactly one object from excel row
*/
public <T> List<T> getData(Path file, BiFunction<? super Table, TableRow, T> 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 <T> List<T> getDataCollection(Path file, BiFunction<? super Table, TableRow, Collection<T>> rowExtractor) {
return getDataCollection(file, rowExtractor, Object::equals, Arrays::asList);
return getDataCollection(file, (row, data) -> {
Collection<T> result = rowExtractor.apply(this, row);
if (result != null) {
data.addAll(result);
}
});
}

/**
Expand All @@ -106,18 +119,25 @@ public <T> List<T> getDataCollection(Path file, BiFunction<? super Table, TableR
public <T> List<T> getDataCollection(Path file, BiFunction<? super Table, TableRow, Collection<T>> rowExtractor,
BiPredicate<T, T> equalityChecker,
BiFunction<T, T, Collection<T>> mergeDuplicates) {
return getDataCollection(file, (row, data) -> {
Collection<T> result = rowExtractor.apply(this, row);
if (result != null) {
for (T r : result) {
addWithEqualityChecker(r, data, equalityChecker, mergeDuplicates);
}
}
});
}

private <T> List<T> getDataCollection(Path file, BiConsumer<TableRow, Collection<T>> rowHandler) {
List<T> data = new ArrayList<>();
for (TableRow row : this) {
if (row != null) {
try {
Collection<T> 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);
}
}
}
Expand All @@ -127,7 +147,7 @@ public <T> List<T> getDataCollection(Path file, BiFunction<? super Table, TableR
public static <T> void addWithEqualityChecker(T element,
Collection<T> collection,
BiPredicate<T, T> equalityChecker,
BiFunction<T, T, Collection<T>> mergeDuplicates) {
BiFunction<T, T, Collection<T>> duplicatesMerger) {
T equalsObject = null;
for (T e : collection) {
if (equalityChecker.test(e, element)) {
Expand All @@ -137,7 +157,7 @@ public static <T> 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);
}
Expand Down

0 comments on commit 54086df

Please sign in to comment.