Skip to content

Commit

Permalink
🔖 release 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Feb 6, 2018
1 parent e23a39c commit 91c3a1a
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Add maven dependency
<dependency>
<groupId>io.github.biezhi</groupId>
<artifactId>excel-plus</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
</dependency>
```

Expand Down
9 changes: 8 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>io.github.biezhi</groupId>
<artifactId>excel-plus</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
</dependency>
```

Expand Down Expand Up @@ -226,6 +226,13 @@ excelPlus.export(Exporter.create(cardSecrets).byTemplate("tpl.xls")).writeAsFile

# 版本更新

<b>v0.0.2</b>

- 1. 添加验证行记录各项指标
- 2. 区分验证成功和失败行
- 3. 可配置是否添加到验证成功行
- 4. 取消 `columnName` 选项为必选

<b>v0.0.1</b>

- 发布第一个版本
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.biezhi</groupId>
<artifactId>excel-plus</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<name>excel-plus</name>
<url>https://biezhi.github.io/excel-plus</url>
<description>excel read and write framework</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @return excel header column title
*/
String columnName();
String columnName() default "";

/**
* When a field is a Date or a Time type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface Converter<T> {
* @param value
* @return
*/
T read(String value);
default T read(String value) {
return null;
}

}
30 changes: 15 additions & 15 deletions src/main/java/io/github/biezhi/excel/plus/reader/ExcelReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ExcelReader<T> {
private Class<T> type;
private Predicate<T> filter;
private Function<T, ValidRow> validFunction;
private int startRowIndex = 1;
private int startRowIndex = 1;

public ExcelReader(Workbook workbook, Class<T> type) {
this.workbook = workbook;
Expand All @@ -38,28 +38,28 @@ public ExcelReader(Workbook workbook, Class<T> type) {
public ExcelResult<T> asResult() {
ExcelResult<T> excelResult = new ExcelResult<>();

Stream<Pair<Integer, T>> stream = this.asStream();
List<Pair<Integer, T>> collect = this.asStream().collect(Collectors.toList());
Stream<T> listStream = collect.stream().map(Pair::getV);

if (null != this.validFunction) {
if (null != this.filter) {
listStream = listStream.filter(this.filter);
}

List<T> rows = listStream.collect(Collectors.toList());
excelResult.rows(rows);

stream = stream.filter(pair -> {
if (null != this.validFunction) {
collect.forEach(pair -> {
Integer rowNum = pair.getK();
T item = pair.getV();
ValidRow validRow = validFunction.apply(item);
if (!validRow.valid()) {
validRow.rowNum(rowNum);
excelResult.addError(validRow);
return false;
validRow.rowNum(rowNum);
if (validRow.isValid() && validRow.allowAdd()) {
excelResult.addSuccessRow(item);
}
return true;
excelResult.addValidRow(validRow);
});
}

Stream<T> listStream = stream.map(Pair::getV);
if (null != this.filter) {
listStream = listStream.filter(this.filter);
}
excelResult.rows(listStream.collect(Collectors.toList()));
return excelResult;
}

Expand Down
49 changes: 42 additions & 7 deletions src/main/java/io/github/biezhi/excel/plus/reader/ExcelResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Excel valid result
Expand All @@ -11,27 +12,61 @@
*/
public class ExcelResult<T> {

private List<ValidRow> errors = new ArrayList<>();
private List<T> rows;
private List<ValidRow> validRows = new ArrayList<>();
private List<T> successRows = new ArrayList<>();
private List<T> rows = new ArrayList<>();
private int totalRows;
private Boolean isValid = true;

public boolean isValid() {
return errors.size() == 0;
public void addValidRow(ValidRow validRow) {
validRows.add(validRow);
if (validRow.isValidFail()) {
isValid = false;
}
}

public void addError(ValidRow validRow) {
errors.add(validRow);
public void addSuccessRow(T item) {
successRows.add(item);
}

public void rows(List<T> rows) {
this.rows = rows;
this.totalRows = rows.size();
}

public List<T> rows() {
return rows;
}

public List<T> successRows() {
return successRows;
}

public String popMsg() {
List<ValidRow> errors = errors();
if (null != errors && errors.size() > 0) {
return errors.get(0).msg();
}
return null;
}

public List<ValidRow> errors() {
return errors;
return validRows.stream()
.filter(ValidRow::isValidFail)
.filter(ValidRow::hasMsg)
.collect(Collectors.toList());
}

public int totalRows() {
return totalRows;
}

public int counter(String key) {
return validRows.stream().map(v -> v.counter(key)).reduce(0, (x, y) -> x + y);
}

public boolean isValid() {
return isValid;
}

}
61 changes: 57 additions & 4 deletions src/main/java/io/github/biezhi/excel/plus/reader/ValidRow.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.biezhi.excel.plus.reader;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Verify that the row data is passed and the row number
* and error messages are stored internally.
Expand All @@ -11,25 +14,45 @@ public class ValidRow {

private String msg;
private boolean valid;
private boolean allowAdd;
private int rowNum;
private Map<String, Integer> counter = new ConcurrentHashMap<>();

public ValidRow(boolean valid, String msg) {
public ValidRow(boolean valid, boolean allowAdd, String msg) {
this.valid = valid;
this.allowAdd = allowAdd;
this.msg = msg;
}

public static ValidRow ok() {
return new ValidRow(true, null);
return new ValidRow(true, true, null);
}

public static ValidRow fail(String msg) {
return new ValidRow(false, msg);
return new ValidRow(false, false, msg);
}

boolean valid() {
boolean isValid() {
return valid;
}

boolean isValidFail() {
return !valid;
}

boolean hasMsg() {
return null != msg;
}

boolean allowAdd() {
return this.allowAdd;
}

public ValidRow allowAdd(boolean allowAdd) {
this.allowAdd = allowAdd;
return this;
}

public String msg() {
return this.msg;
}
Expand All @@ -42,6 +65,36 @@ public int rowNum() {
return this.rowNum;
}

public ValidRow addCounter(String key) {
return this.addCounter(key, 1);
}

public ValidRow addCounter(String key, int count) {
Integer currentCount = counter.get(key);
if (null == currentCount) {
currentCount = count;
}
counter.put(key, currentCount);
return this;
}

public ValidRow subtract(String key) {
return this.subtract(key, 1);
}

public ValidRow subtract(String key, int count) {
Integer currentCount = counter.get(key);
if (null != currentCount) {
currentCount = currentCount - count;
counter.put(key, currentCount);
}
return this;
}

public int counter(String key) {
return counter.getOrDefault(key, 0);
}

@Override
public String toString() {
if (valid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public static void writeToField(Object item, int col, String value) {
}

private static Object asObject(Field field, String value) {
if(null == value || "".equals(value)){
return null;
}
ExcelField excelField = field.getAnnotation(ExcelField.class);
if (null != excelField && !excelField.convertType().equals(EmptyConverter.class)) {
Converter converter = newInstance(excelField.convertType());
Expand Down
1 change: 1 addition & 0 deletions src/test/java/io/github/biezhi/excel/plus/Examples.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void testReadFilter() throws ExcelException {
@Test
public void testReadValid() throws ExcelException {
ExcelResult<CardSecret> excelResult = excelPlus.read(new File("卡密列表.xls"), CardSecret.class)
.startRow(2)
.valid(cardSecret -> {
BigDecimal amount = cardSecret.getAmount();
if (amount.doubleValue() < 20) {
Expand Down

0 comments on commit 91c3a1a

Please sign in to comment.