Package io.github.dornol.excelkit.core
Class AbstractReadHandler<T>
java.lang.Object
io.github.dornol.excelkit.core.TempResourceContainer
io.github.dornol.excelkit.core.AbstractReadHandler<T>
- Type Parameters:
T- The target row data type to map each row into
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
CsvReadHandler,ExcelReadHandler
Abstract base class for file read handlers (Excel, CSV).
Provides common functionality including:
- Constructor parameter validation
- Temporary file initialization from an InputStream
- Optional Bean Validation support
- Since:
- 2025-07-19
-
Field Summary
FieldsModifier and TypeFieldDescriptionSupplier for creating new row instances (setter mode).Function for mapping row data to instances (mapping mode).protected final @Nullable jakarta.validation.ValidatorOptional bean validator for row validation. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractReadHandler(InputStream inputStream, Function<RowData, T> rowMapper, @Nullable jakarta.validation.Validator validator, String extension) Constructs a read handler in mapping mode for immutable object construction.protectedAbstractReadHandler(InputStream inputStream, Supplier<T> instanceSupplier, @Nullable jakarta.validation.Validator validator, String extension) Constructs a read handler in setter mode by validating inputs and initializing a temporary file. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanmapColumn(ReadColumn<T> column, T instance, CellData cellData, int columnIndex, List<String> headerNames, List<String> messages) Maps a single column value to the instance, with required-field validation.protected booleanmapColumn(BiConsumer<T, CellData> setter, T instance, CellData cellData, int columnIndex, List<String> headerNames, List<String> messages) Maps a single column value to the instance, handling exceptions.protected ReadResult<T> mapWithRowMapper(RowData rowData) Maps a row using the row mapper function (mapping mode).abstract voidread(Consumer<ReadResult<T>> consumer) Reads the file and invokes the given consumer for each row result.abstract Stream<ReadResult<T>> Reads the file and returns a lazy stream of row results.voidreadStrict(Consumer<T> consumer) Reads the file and invokes the given consumer only for successfully parsed rows.protected int[]resolveColumnIndices(int columnCount, IntFunction<String> headerNameFn, IntUnaryOperator columnIndexFn, List<String> headerNames, String errorPrefix) Resolves column indices based on headerName, columnIndex, or positional order.protected static voidvalidateColumns(List<?> columns) Validates that the columns list is non-null and non-empty.protected static voidvalidateHeaderRowIndex(int headerRowIndex) Validates that headerRowIndex is non-negative.protected booleanvalidateIfNeeded(T instance, List<String> messages) Validates the given instance using Bean Validation (if a validator is configured).Methods inherited from class io.github.dornol.excelkit.core.TempResourceContainer
close, getTempDir, getTempFile, setTempDir, setTempFile
-
Field Details
-
instanceSupplier
Supplier for creating new row instances (setter mode). -
rowMapper
Function for mapping row data to instances (mapping mode). -
validator
protected final @Nullable jakarta.validation.Validator validatorOptional bean validator for row validation.
-
-
Constructor Details
-
AbstractReadHandler
protected AbstractReadHandler(InputStream inputStream, Supplier<T> instanceSupplier, @Nullable jakarta.validation.Validator validator, String extension) Constructs a read handler in setter mode by validating inputs and initializing a temporary file.- Parameters:
inputStream- The input stream of the uploaded fileinstanceSupplier- A supplier to instantiate new row objectsvalidator- Optional bean validator for validating mapped instancesextension- File extension for the temporary file (e.g., ".xlsx", ".csv")
-
AbstractReadHandler
protected AbstractReadHandler(InputStream inputStream, Function<RowData, T> rowMapper, @Nullable jakarta.validation.Validator validator, String extension) Constructs a read handler in mapping mode for immutable object construction.- Parameters:
inputStream- The input stream of the uploaded filerowMapper- A function that creates an instance from aRowDatavalidator- Optional bean validator for validating mapped instancesextension- File extension for the temporary file (e.g., ".xlsx", ".csv")
-
-
Method Details
-
validateHeaderRowIndex
protected static void validateHeaderRowIndex(int headerRowIndex) Validates that headerRowIndex is non-negative.- Parameters:
headerRowIndex- the header row index to validate
-
validateColumns
Validates that the columns list is non-null and non-empty.- Parameters:
columns- the column list to validate
-
read
Reads the file and invokes the given consumer for each row result.- Parameters:
consumer- Callback to receive parsed and validated row results
-
readStrict
Reads the file and invokes the given consumer only for successfully parsed rows. If any row fails validation or mapping, aReadAbortExceptionis thrown immediately.- Parameters:
consumer- Callback to receive successfully parsed row data- Throws:
ReadAbortException- if any row fails validation or mapping
-
readAsStream
Reads the file and returns a lazy stream of row results.Important: The returned stream holds file and thread resources. Always use try-with-resources to ensure proper cleanup:
try (Stream<ReadResult<T>> stream = handler.readAsStream()) { stream.forEach(result -> ...); }- Returns:
- A stream of parsed and validated row results
-
validateIfNeeded
Validates the given instance using Bean Validation (if a validator is configured).- Parameters:
instance- The object to validatemessages- A mutable list to collect violation messages- Returns:
trueif valid or no validator is configured,falseif violations exist
-
resolveColumnIndices
protected int[] resolveColumnIndices(int columnCount, IntFunction<String> headerNameFn, IntUnaryOperator columnIndexFn, List<String> headerNames, String errorPrefix) Resolves column indices based on headerName, columnIndex, or positional order.- Parameters:
columnCount- number of columns to resolveheaderNameFn- function to get headerName for column i (may return null)columnIndexFn- function to get explicit columnIndex for column i (-1 if not set)headerNames- the header names from the fileerrorPrefix- prefix for error messages (e.g., "sheet" or "CSV")- Returns:
- resolved index array
-
mapColumn
protected boolean mapColumn(BiConsumer<T, CellData> setter, T instance, CellData cellData, int columnIndex, List<String> headerNames, List<String> messages) Maps a single column value to the instance, handling exceptions.- Parameters:
setter- The setter to applyinstance- The target objectcellData- The cell data to setcolumnIndex- The column index (for error reporting)headerNames- The header names (for error reporting)messages- A mutable list to collect error messages- Returns:
trueif mapping succeeded,falseif an exception occurred
-
mapColumn
protected boolean mapColumn(ReadColumn<T> column, T instance, CellData cellData, int columnIndex, List<String> headerNames, List<String> messages) Maps a single column value to the instance, with required-field validation.- Parameters:
column- The column definition (includes required flag)instance- The target objectcellData- The cell data to setcolumnIndex- The column index (for error reporting)headerNames- The header names (for error reporting)messages- A mutable list to collect error messages- Returns:
trueif mapping succeeded,falseif an error occurred
-
mapWithRowMapper
Maps a row using the row mapper function (mapping mode). Catches exceptions from the mapper and wraps them in a failedReadResult.- Parameters:
rowData- The row data to map- Returns:
- A
ReadResultcontaining the mapped instance or error messages
-