All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sfm.csv.CsvMapper Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 1.10.3
Show newest version
package org.sfm.csv;

import org.sfm.map.MappingException;
import org.sfm.utils.RowHandler;

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 org.sfm.csv.CsvReader} to an object of the specified type T.
 * 

* There are 2 ways to instantiate a CsvMapper *

* Using {@link org.sfm.csv.CsvMapperFactory}

* * CsvMapper mapper = CsvMapperFactory
*         .newInstance()
*         .newMapper(Myclass.class);
* try (FileReader reader : new FileReader(file)) {
*     mapper.stream(reader).forEach(System.out::println);
* } *
*

* Or Using the {@link org.sfm.csv.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 org.sfm.csv.CsvReader} to gain more control on the csv parsing.
* You can instantiate a CsvReader using the {@link org.sfm.csv.CsvParser} DSL
*
* * try (FileReader reader : new FileReader(file)) {
*     CsvReader csvReader = CsvParser.quote('\'').separator(';').skip(2).limit(5).reader(reader);
*     mapper.stream(csvReader).forEach(System.out::println);
* } *
* * * @param the type of the object the it will map to * @see org.sfm.csv.CsvParser * @see org.sfm.csv.CsvMapperFactory */ public interface CsvMapper { /** * 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 RowHandler}.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 = mapper.forEach(reader, new ListHandler<T>()).getList(); * *
* * @param reader the reader * @param handle the callback instance * @param the row handler type * @return the callback instance * @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 RowHandler}.handler(T t) call back. * * * @param reader the reader * @param handle the callback instance * @param the row handler type * @return the callback instance * @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 RowHandler}.handler(T t) call back. * * * @param reader the reader * @param handle the callback instance * @param skip the number of row to skip * @param the row handler type * @return the callback instance * @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 RowHandler}.handler(T t) call back. * * * @param reader the reader * @param handle the callback instance * @param skip the number of row to skip * @param limit the number of row to process * @param the row handler type * @return the callback instance * @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 RowHandler}.handler(T t) call back. * * * @param reader the reader * @param handle the callback instance * @param limit the number of row to process * @param the row handler type * @return the callback instance * @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; @Deprecated Iterator iterate(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; @Deprecated Iterator iterate(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; @Deprecated Iterator iterate(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 }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy