Skip to content

Commit

Permalink
add consumer support
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlerhakan committed Aug 1, 2018
1 parent 83c4eb6 commit 1a080a8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
57 changes: 55 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ In your Maven/Gradle project, first add the corresponding dependency:
<dependency>
<groupId>com.github.ozlerhakan</groupId>
<artifactId>poiji</artifactId>
<version>1.13</version>
<version>1.14</version>
</dependency>
----

.gradle
[source,groovy]
----
dependencies {
compile 'com.github.ozlerhakan:poiji:1.13'
compile 'com.github.ozlerhakan:poiji:1.14'
}
----

Expand Down Expand Up @@ -284,6 +284,59 @@ Car car = cars.get(0);
// 4
----

=== Example 4

As of 1.14, Poiji supports Consumer Interface. As https://github.com/ozlerhakan/poiji/pull/39#issuecomment-409521808[@fmarazita] explained the usage, there are several benefits of having a Consumer:

1. Huge excel file ( without you have all in memory)
2. Run time processing/filtering data
3. DB batch insertion

For example, we have a Calculation entity class and want to insert each row while retrieving:

[source, java]
----
class Calculation {
@ExcelCell(0)
String Name
@ExcelCell(1)
int a
@ExcelCell(2)
int b
public int getA(){
return a;
}
public int getB(){
return b;
}
public int getNanem){
return name;
}
}
----

[source, java]
----
File fileCalculation = new File(example.xlsx);
PoijiOptions options = PoijiOptionsBuilder.settings(1).sheetIndex(1).build();
Poiji.fromExcel(fileCalculation, Calculation.class, options, this::dbInsertion);
private void dbInsertion(Calculation siCalculation) {
int value= siCalculation.getA() + siCalculation.getB();
String name = siCalculation.getName();
return insertDB(name , value);
}
----

=== Try with JShell

Since we have a new pedagogic tool, Java 9 REPL, you can try Poiji in JShell. Clone the repo and follow the steps below. JShell should open up a new jshell session once loading the startup scripts and the specified jars that must be in the classpath. You must first import and create related packages and classes before using Poiji. We are able to use directly Poiji and Employee classes because they are already imported from `jshell/snippets` with `try-with-jshell.sh`.
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/poiji/bind/Poiji.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import static com.poiji.util.PoijiConstants.XLSX_EXTENSION;
import static com.poiji.util.PoijiConstants.XLS_EXTENSION;
import java.util.ArrayList;
import java.util.function.Consumer;

/**
* The entry point of the mapping process.
Expand Down Expand Up @@ -115,7 +115,7 @@ public static synchronized <T> List<T> fromExcel(final InputStream inputStream,
* @param excelType type of the excel file, xls or xlsx
* @param type type of the root object.
* @param <T> type of the root object.
* @param consumer
* @param consumer represents an operation that accepts the type argument
* @throws PoijiException if an internal exception occurs during the mapping process.
* @throws InvalidExcelFileExtension if the specified excel file extension is invalid.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
Expand Down Expand Up @@ -157,7 +157,7 @@ public static synchronized <T> List<T> fromExcel(final File file, final Class<T>
* @param type type of the root object.
* @param <T> type of the root object.
* @param options specifies to change the default behaviour of the poiji.
* @param consumer output retrieves records
* @param consumer represents an operation that accepts the type argument
* @throws PoijiException if an internal exception occurs during the mapping process.
* @throws InvalidExcelFileExtension if the specified excel file extension is invalid.
* @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
Expand Down Expand Up @@ -200,7 +200,7 @@ public static synchronized <T> List<T> fromExcel(final InputStream inputStream,
* @param type type of the root object.
* @param <T> type of the root object.
* @param options specifies to change the default behaviour of the poiji.
* @param consumer output retrieves records
* @param consumer represents an operation that accepts the type argument
* @throws PoijiException if an internal exception occurs during the mapping process.
* @throws InvalidExcelFileExtension if the specified excel file extension is invalid.
* @throws IllegalCastException if this Field object is enforcing Java
Expand Down

0 comments on commit 1a080a8

Please sign in to comment.