Skip to content

Commit

Permalink
Merge pull request #97 from jigarWala/#32
Browse files Browse the repository at this point in the history
feature fix for #32, map only first n rows
  • Loading branch information
ozlerhakan committed Oct 19, 2019
2 parents 2ffcf59 + d288adb commit 2351a5a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/poiji/bind/mapping/HSSFUnmarshaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ abstract class HSSFUnmarshaller implements Unmarshaller {
protected final PoijiOptions options;
private final Casting casting;
private Map<String, Integer> titles;
private int limit;

HSSFUnmarshaller(PoijiOptions options) {
this.options = options;
this.limit = options.getLimit();
dataFormatter = new DataFormatter();
titles = new HashMap<>();
casting = options.getCasting();
Expand All @@ -57,6 +59,9 @@ public <T> void unmarshal(Class<T> type, Consumer<? super T> consumer) {

for (Row currentRow : sheet) {
if (!skip(currentRow, skip) && !isRowEmpty(currentRow)) {

if(currentRow.getRowNum() + 1 >= limit)
return;
T t = deserialize0(currentRow, type);
consumer.accept(t);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/poiji/bind/mapping/PoijiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.poiji.annotation.ExcelRow;
import com.poiji.config.Casting;
import com.poiji.exception.IllegalCastException;
import com.poiji.exception.LimitCrossedException;
import com.poiji.exception.PoijiInstantiationException;
import com.poiji.option.PoijiOptions;
import com.poiji.util.ReflectUtil;

Expand All @@ -31,6 +33,7 @@ final class PoijiHandler<T> implements SheetContentsHandler {
private T instance;
private Consumer<? super T> consumer;
private int internalCount;
private int limit;

private Class<T> type;
private PoijiOptions options;
Expand All @@ -46,6 +49,7 @@ final class PoijiHandler<T> implements SheetContentsHandler {
this.type = type;
this.options = options;
this.consumer = consumer;
this.limit = options.getLimit();

casting = options.getCasting();
titles = new HashMap<>();
Expand Down Expand Up @@ -190,7 +194,6 @@ public void cell(String cellReference, String formattedValue, XSSFComment commen

CellAddress cellAddress = new CellAddress(cellReference);
int row = cellAddress.getRow();

internalCount = row;
int column = cellAddress.getColumn();
int headers = options.getHeaderStart();
Expand All @@ -203,6 +206,9 @@ public void cell(String cellReference, String formattedValue, XSSFComment commen
return;
}

if (row +1 >= limit)
throw new LimitCrossedException("Limit crossed, Stop Iteration");

setFieldValue(formattedValue, type, column);
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/poiji/bind/mapping/XSSFUnmarshaller.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.poiji.bind.mapping;

import com.poiji.bind.Unmarshaller;
import com.poiji.exception.LimitCrossedException;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.apache.poi.ooxml.util.SAXHelper;
Expand Down Expand Up @@ -109,7 +110,10 @@ private <T> void processSheet(StylesTable styles,
= new XSSFSheetXMLHandler(styles, null, readOnlySharedStringsTable, poijiHandler, formatter, false);
reader.setContentHandler(contentHandler);
reader.parse(sheetSource);
} catch (SAXException | IOException e) {
} catch (LimitCrossedException e) {
IOUtils.closeQuietly(sheetInputStream);
// swallowing the exception for good :)
} catch (SAXException | IOException e) {
throw new PoijiException("Problem occurred while reading data", e);
}
}
Expand All @@ -129,4 +133,4 @@ protected <T> void listOfEncryptedItems(Class<T> type, Consumer<? super T> consu
protected abstract <T> void returnFromExcelFile(Class<T> type, Consumer<? super T> consumer);

protected abstract <T> void returnFromEncryptedFile(Class<T> type, Consumer<? super T> consumer);
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/poiji/exception/LimitCrossedException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.poiji.exception;

public class LimitCrossedException extends PoijiException {

public LimitCrossedException(String message) {
super(message);
}

}
31 changes: 29 additions & 2 deletions src/main/java/com/poiji/option/PoijiOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
public final class PoijiOptions {

private int skip;
private int limit;
private int sheetIndex;
private String password;
private String dateRegex;
Expand All @@ -38,7 +39,16 @@ private PoijiOptions setSkip(int skip) {
return this;
}

private PoijiOptions setDatePattern(String datePattern) {
public int getLimit() {
return limit;
}

public PoijiOptions setLimit(int limit) {
this.limit = limit;
return this;
}

private PoijiOptions setDatePattern(String datePattern) {
this.datePattern = datePattern;
return this;
}
Expand Down Expand Up @@ -157,6 +167,7 @@ public String getSheetName() {

public static class PoijiOptionsBuilder {

private int limit=Integer.MAX_VALUE;
private int sheetIndex;
private String password;
private String dateRegex;
Expand Down Expand Up @@ -245,7 +256,8 @@ public PoijiOptions build() {
.setDateRegex(dateRegex)
.setDateLenient(dateLenient)
.setHeaderStart(headerStart)
.setCasting(casting);
.setCasting(casting)
.setLimit(limit == Integer.MAX_VALUE ? limit : limit + headerStart + 1);
}

/**
Expand Down Expand Up @@ -287,6 +299,21 @@ public PoijiOptionsBuilder skip(int skip) {
return this;
}

/**
* limit a number of rows after the header & skipped rows row. The header & skipped rows are not counted.
*
* @param limit number
* @return this
*/

public PoijiOptionsBuilder limit(int limit) {
if(limit<0) {
throw new PoijiException("limit must be greater than or equal to 0");
}
this.limit = limit;
return this;
}

/**
* set password for encrypted excel file, Default is null
*
Expand Down
65 changes: 65 additions & 0 deletions src/test/java/com/poiji/deserialize/ReadUptoLimitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.poiji.deserialize;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.util.List;

import org.junit.Test;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.byid.ConfigPerson;
import com.poiji.deserialize.model.byid.Person;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import com.poiji.option.PoijiOptions.PoijiOptionsBuilder;

public class ReadUptoLimitTest {

@Test
public void testWithoutAnyLimit() {

List<ConfigPerson> actualEmployees = Poiji.fromExcel(new File("src/test/resources/employees.xlsx"),
ConfigPerson.class);

assertEquals(3, actualEmployees.size());

}

@Test
public void limitWithoutSkip() {

PoijiOptions options = PoijiOptionsBuilder.settings().limit(4).build();

List<Person> personListFromXSSF = Poiji.fromExcel(new File("src/test/resources/person.xlsx"), Person.class,
options);
assertEquals(3, personListFromXSSF.size());

List<Person> personListFromHSSF = Poiji.fromExcel(new File("src/test/resources/person.xls"), Person.class,
options);
assertEquals(3, personListFromHSSF.size());

}

@Test
public void limitWithSkip() {

PoijiOptions options = PoijiOptionsBuilder.settings().skip(2).limit(4).build();

List<Person> personListFromXSSF = Poiji.fromExcel(new File("src/test/resources/person.xlsx"), Person.class,
options);
assertEquals(1, personListFromXSSF.size());

List<Person> personListFromHSSF = Poiji.fromExcel(new File("src/test/resources/person.xls"), Person.class,
options);
assertEquals(1, personListFromHSSF.size());
}

@Test(expected = PoijiException.class)
public void negativeLimitOptionThrowsException() {

PoijiOptions options = PoijiOptionsBuilder.settings().limit(-1).build();
Poiji.fromExcel(new File("src/test/resources/person.xlsx"), Person.class, options, System.out::println);
}

}

0 comments on commit 2351a5a

Please sign in to comment.