org.simpleflatmapper.csv.CsvMapper Maven / Gradle / Ivy
Show all versions of sfm-csv Show documentation
package org.simpleflatmapper.csv;
import org.simpleflatmapper.lightningcsv.CsvReader;
import org.simpleflatmapper.map.MappingException;
import org.simpleflatmapper.map.SetRowMapper;
import org.simpleflatmapper.util.CheckedConsumer;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
//IFJAVA8_START
import java.util.stream.Stream;
//IFJAVA8_END
/**
* A CsvMapper will map from a {@link CsvReader} to an object of the specified type T.
*
* There are 2 ways to instantiate a CsvMapper
*
* Using {@link CsvMapperFactory}
*
* CsvMapper jdbcMapper = CsvMapperFactory
* .newInstance()
* .newMapper(MyClass.class);
* try (FileReader reader : new FileReader(file)) {
* jdbcMapper.stream(reader).forEach(System.out::println);
* }
*
*
* Or Using the {@link CsvParser} DSL
*
* try (FileReader reader : new FileReader(file)) {
* CsvParser.mapTo(MyClass.class).stream(reader).forEach(System.out::println);
* }
*
*
* The CsvMapper can read from an {@link java.io.Reader} or
* a {@link CsvReader} to gain more control on the csv parsing.
* You can instantiate a CsvReader using the {@link CsvParser} DSL
*
*
* try (FileReader reader : new FileReader(file)) {
* CsvReader csvReader = CsvParser.quote('\'').separator(';').skip(2).limit(5).reader(reader);
* jdbcMapper.stream(csvReader).forEach(System.out::println);
* }
*
*
*
* @param the type of the object the it will map to
* @see CsvParser
* @see CsvMapperFactory
*/
public interface CsvMapper extends SetRowMapper {
/**
* Will map each row of the content of reader to an object of type T and will pass that object to the handle via the {@link CheckedConsumer}.handler(T t) call back.
*
* The method will return the handler passed as an argument so you can easily chain the calls like
*
* List<T> list = jdbcMapper.forEach(reader, new ListHandler<T>()).getList();
*
*
*
* @param reader the reader
* @param handle the callback newInstance
* @param the row handler type
* @return the callback newInstance
* @throws IOException if an io error occurs
* @throws MappingException if an mapping error occurs
*/
> H forEach(Reader reader, H handle) throws IOException, MappingException;
/**
* Will map each row of the content of reader to an object of type T and will pass that object to the handle via the {@link CheckedConsumer}.handler(T t) call back.
*
*
* @param reader the reader
* @param handle the callback newInstance
* @param the row handler type
* @return the callback newInstance
* @throws IOException if an io error occurs
* @throws MappingException if an mapping error occurs
*/
> H forEach(CsvReader reader, H handle) throws IOException, MappingException;
/**
* Will map each row of the content of reader, starting at rowStart, to an object of type T and will pass that object to the handle via the {@link CheckedConsumer}.handler(T t) call back.
*
*
* @param reader the reader
* @param handle the callback newInstance
* @param skip the number of row to skip
* @param the row handler type
* @return the callback newInstance
* @throws IOException if an io error occurs
* @throws MappingException if an mapping error occurs
*/
> H forEach(Reader reader, H handle, int skip) throws IOException, MappingException;
/**
* Will map each row of the content of reader, starting at rowStart and ending before rowEnd, to an object of type T and will pass that object to the handle via the {@link CheckedConsumer}.handler(T t) call back.
*
*
* @param reader the reader
* @param handle the callback newInstance
* @param skip the number of row to skip
* @param limit the number of row to process
* @param the row handler type
* @return the callback newInstance
* @throws IOException if an io error occurs
* @throws MappingException if an mapping error occurs
*/
> H forEach(Reader reader, H handle, int skip, int limit) throws IOException, MappingException;
/**
* Will map each row of the content of reader, starting at rowStart and ending before rowEnd, to an object of type T and will pass that object to the handle via the {@link CheckedConsumer}.handler(T t) call back.
*
*
* @param reader the reader
* @param handle the callback newInstance
* @param limit the number of row to process
* @param the row handler type
* @return the callback newInstance
* @throws IOException if an io error occurs
* @throws MappingException if an mapping error occurs
*/
> H forEach(CsvReader reader, H handle, int limit) throws IOException, MappingException;
/**
* Will return an iterator on the reader that will return a mapped object for each row.
*
* @param reader the reader
* @return an iterator on the file
* @throws IOException if an io error occurs
*/
Iterator iterator(Reader reader) throws IOException;
/**
* Will return an iterator on the reader that will return a mapped object for each row.
*
* @param reader the reader
* @return an iterator on the file
* @throws IOException if an io error occurs
*/
Iterator iterator(CsvReader reader) throws IOException;
/**
* Will return an iterator on the reader that will return a mapped object for each row.
*
* @param reader the reader
* @param skip the number of row to skip
* @return an iterator on the file
* @throws IOException if an io error occurs
*/
Iterator iterator(Reader reader, int skip) throws IOException;
/**
* Will return a Stream of T
*
* @param reader the reader
* @return stream of T
* @throws IOException if an io error occurs
*/
//IFJAVA8_START
Stream stream(Reader reader) throws IOException;
//IFJAVA8_END
/**
* Will return a Stream of T
*
* @param reader the reader
* @return stream of T
* @throws IOException if an io error occurs
*/
//IFJAVA8_START
Stream stream(CsvReader reader) throws IOException;
//IFJAVA8_END
/**
* Will return a Stream of T.
*
* @param reader the reader
* @param skip the number of row to skip
* @return stream of T
* @throws IOException if an io error occurs
*/
//IFJAVA8_START
Stream stream(Reader reader, int skip) throws IOException;
//IFJAVA8_END
}