From cac8cfc896541e7f7ecd17474b358d03747b91fb Mon Sep 17 00:00:00 2001 From: Zikani Nyirenda Mwase Date: Mon, 2 Sep 2019 19:24:58 +0200 Subject: [PATCH] Add Java Stream API support Add support for Streams to the Reader and EntityHandler classes as suggested in issue #11 --- .../com/creditdatamw/zerocell/Reader.java | 27 ++++++++++++++++++- .../handler/EntityExcelSheetHandler.java | 18 +++++++++---- .../zerocell/handler/EntityHandler.java | 5 ++++ .../com/creditdatamw/zerocell/TestReader.java | 12 +++++++++ 4 files changed, 56 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/com/creditdatamw/zerocell/Reader.java b/core/src/main/java/com/creditdatamw/zerocell/Reader.java index b94560e..44d41d8 100644 --- a/core/src/main/java/com/creditdatamw/zerocell/Reader.java +++ b/core/src/main/java/com/creditdatamw/zerocell/Reader.java @@ -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 @@ -87,8 +88,14 @@ public ReaderBuilder setRowNumber(int rowNumber) { return this; } + /** + * Processes the file + * + * @param + * @return EntityHandler instance + */ @SuppressWarnings("unchecked") - public List list() { + private EntityHandler processFile() { EntityHandler entityHandler; if (!Objects.isNull(sheetName) && !Objects.isNull(columnMapping)) { entityHandler = new EntityHandler(clazz, sheetName, columnMapping, skipHeaderRow, skipFirstNRows, maxRowNumber); @@ -100,7 +107,25 @@ public List 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 list() { + EntityHandler entityHandler = processFile(); return entityHandler.readAsList(); } + + /** + * Reads the Entities the Excel file and returns a Stream + * @return Stream of entities + */ + public Stream stream() { + EntityHandler entityHandler = processFile(); + return entityHandler.readAsStream(); + } } } diff --git a/core/src/main/java/com/creditdatamw/zerocell/handler/EntityExcelSheetHandler.java b/core/src/main/java/com/creditdatamw/zerocell/handler/EntityExcelSheetHandler.java index a5304ad..0e173db 100644 --- a/core/src/main/java/com/creditdatamw/zerocell/handler/EntityExcelSheetHandler.java +++ b/core/src/main/java/com/creditdatamw/zerocell/handler/EntityExcelSheetHandler.java @@ -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; @@ -24,7 +26,7 @@ final class EntityExcelSheetHandler implements ZeroCellReader { private final ColumnInfo rowNumberColumn; private final Map columns; - private final List entities; + private Stream.Builder streamBuilder; private final Converter NOOP_CONVERTER = new NoopConverter(); private final Map converters; private boolean isHeaderRow = false; @@ -37,7 +39,7 @@ final class EntityExcelSheetHandler implements ZeroCellReader { this.rowNumberColumn = rowNumberColumn; this.columns = columns; this.converters = cacheConverters(); - this.entities = new ArrayList<>(); + this.streamBuilder = Stream.builder(); } private Map cacheConverters() { @@ -67,14 +69,19 @@ public List 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 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 @@ -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; } } diff --git a/core/src/main/java/com/creditdatamw/zerocell/handler/EntityHandler.java b/core/src/main/java/com/creditdatamw/zerocell/handler/EntityHandler.java index c608de1..9be2270 100644 --- a/core/src/main/java/com/creditdatamw/zerocell/handler/EntityHandler.java +++ b/core/src/main/java/com/creditdatamw/zerocell/handler/EntityHandler.java @@ -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; @@ -116,6 +117,10 @@ public List readAsList() { return list; } + public Stream readAsStream() { + return this.entitySheetHandler.getEntityStream(); + } + public void process(File file) throws ZeroCellException { ReaderUtil.process(file, sheetName, this.entitySheetHandler); } diff --git a/core/src/test/java/com/creditdatamw/zerocell/TestReader.java b/core/src/test/java/com/creditdatamw/zerocell/TestReader.java index c9b9855..95c31ca 100644 --- a/core/src/test/java/com/creditdatamw/zerocell/TestReader.java +++ b/core/src/test/java/com/creditdatamw/zerocell/TestReader.java @@ -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.*; @@ -42,6 +43,17 @@ public void testShouldExtractPeopleFromFile() { assertEquals(1, zikani.getRowNumber()); assertEquals("Zikani", zikani.getFirstName()); } + @Test + public void testShouldReturnStream() { + Optional 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() {