Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Java Stream API support #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion core/src/main/java/com/creditdatamw/zerocell/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

/**
* Main API for ZeroCell
Expand Down Expand Up @@ -87,8 +88,14 @@ public ReaderBuilder<T> setRowNumber(int rowNumber) {
return this;
}

/**
* Processes the file
*
* @param <T>
* @return EntityHandler instance
*/
@SuppressWarnings("unchecked")
public <T> List<T> list() {
private <T> EntityHandler<T> processFile() {
EntityHandler<T> entityHandler;
if (!Objects.isNull(sheetName) && !Objects.isNull(columnMapping)) {
entityHandler = new EntityHandler(clazz, sheetName, columnMapping, skipHeaderRow, skipFirstNRows, maxRowNumber);
Expand All @@ -100,7 +107,25 @@ public <T> List<T> list() {
entityHandler = new EntityHandler(clazz, skipHeaderRow, skipFirstNRows, maxRowNumber);
}
entityHandler.process(file);
return entityHandler;
}

/**
* Reads the Entities from the Excel file and returns a list
* @return List of entities
*/
public List<T> list() {
EntityHandler<T> entityHandler = processFile();
return entityHandler.readAsList();
}

/**
* Reads the Entities the Excel file and returns a Stream
* @return Stream of entities
*/
public Stream<T> stream() {
EntityHandler<T> entityHandler = processFile();
return entityHandler.readAsStream();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.io.File;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.creditdatamw.zerocell.converter.ConverterUtils.convertValueToType;

Expand All @@ -24,7 +26,7 @@ final class EntityExcelSheetHandler<T> implements ZeroCellReader {

private final ColumnInfo rowNumberColumn;
private final Map<Integer, ColumnInfo> columns;
private final List<T> entities;
private Stream.Builder<T> streamBuilder;
private final Converter NOOP_CONVERTER = new NoopConverter();
private final Map<Integer, Converter> converters;
private boolean isHeaderRow = false;
Expand All @@ -37,7 +39,7 @@ final class EntityExcelSheetHandler<T> implements ZeroCellReader {
this.rowNumberColumn = rowNumberColumn;
this.columns = columns;
this.converters = cacheConverters();
this.entities = new ArrayList<>();
this.streamBuilder = Stream.builder();
}

private Map<Integer, Converter> cacheConverters() {
Expand Down Expand Up @@ -67,14 +69,19 @@ public List<T> read(File file, String sheet) {
* We don't need to process the file here since that's
* handled in {@link ReaderUtil} which MUST be used when using this class
*/
return Collections.unmodifiableList(this.entities);
return getEntityStream().collect(Collectors.toList());
}

public Stream<T> getEntityStream() {
return this.streamBuilder.build();
}

void clear() {
this.currentRow = -1;
this.currentCol = -1;
this.cur = null;
this.entities.clear();
this.streamBuilder = null;
this.streamBuilder = Stream.builder();
}

@Override
Expand Down Expand Up @@ -114,7 +121,8 @@ public void endRow(int i) {
}

if (!Objects.isNull(cur)) {
this.entities.add(cur);
// this.entities.add(cur);
streamBuilder.add(cur);
cur = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

import static com.creditdatamw.zerocell.column.ColumnMapping.parseColumnMappingFromAnnotations;

Expand Down Expand Up @@ -116,6 +117,10 @@ public List<T> readAsList() {
return list;
}

public Stream<T> readAsStream() {
return this.entitySheetHandler.getEntityStream();
}

public void process(File file) throws ZeroCellException {
ReaderUtil.process(file, sheetName, this.entitySheetHandler);
}
Expand Down
12 changes: 12 additions & 0 deletions core/src/test/java/com/creditdatamw/zerocell/TestReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.sql.Date;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -42,6 +43,17 @@ public void testShouldExtractPeopleFromFile() {
assertEquals(1, zikani.getRowNumber());
assertEquals("Zikani", zikani.getFirstName());
}
@Test
public void testShouldReturnStream() {
Optional<Person> zikani = Reader.of(Person.class)
.from(new File("src/test/resources/test_people.xlsx"))
.sheet("uploads")
.stream()
.findFirst();
assertTrue(zikani.isPresent());
assertEquals(1, zikani.get().getRowNumber());
assertEquals("Zikani", zikani.get().getFirstName());
}

@Test
public void testShouldSkipFirst3RowsFromFile() {
Expand Down