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

com.poiji.bind.Poiji Maven / Gradle / Ivy

package com.poiji.bind;

import com.poiji.exception.IllegalCastException;
import com.poiji.exception.InvalidExcelFileExtension;
import com.poiji.exception.PoijiExcelType;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.apache.poi.ss.usermodel.Sheet;

import static com.poiji.util.PoijiConstants.XLSX_EXTENSION;
import static com.poiji.util.PoijiConstants.XLS_EXTENSION;

/**
 * The entry point of the mapping process.
 * 

* Example: *

 * {@literal List} employees = Poiji.fromExcel(new File("employees.xls"), Employee.class);
 * employees.size();
 * // 3
 * Employee firstEmployee = employees.get(0);
 * // Employee{employeeId=123923, name='Joe', surname='Doe', age=30, single=true, birthday='4/9/1987'}
 * 
*

* Created by hakan on 16/01/2017. */ public final class Poiji { private Poiji() { } /** * converts excel properties into an object * * @param file excel file ending with .xlsx. * @param type type of the root object. * @param type of the root object. * @return the newly created objects * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcelProperties(File, Class, PoijiOptions) */ public static T fromExcelProperties(final File file, final Class type) { return Poiji.fromExcelProperties().withSource(file).withJavaType(type).get(); } /** * converts excel properties into an object * * @param inputStream excel file stream * @param excelType type of the excel file, xlsx only! * @param type type of the root object. * @param type of the root object. * @return the newly created object * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcelProperties(InputStream, PoijiExcelType, Class, PoijiOptions) */ public static T fromExcelProperties(final InputStream inputStream, PoijiExcelType excelType, final Class type) { return Poiji.fromExcelProperties().withSource(inputStream, excelType).withJavaType(type).get(); } /** * converts excel properties into an object * * @param file excel file ending with .xlsx. * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. In this case, only the password has an effect * @return the newly created object * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcelProperties(File, Class) */ public static T fromExcelProperties(final File file, final Class type, final PoijiOptions options) { return Poiji.fromExcelProperties().withSource(file).withJavaType(type).withOptions(options).get(); } /** * converts excel properties into an object * * @param inputStream excel file stream * @param excelType type of the excel file, xlsx only! * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. In this case, only the password has an effect * @return the newly created object * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcelProperties(InputStream, PoijiExcelType, Class) */ public static T fromExcelProperties(final InputStream inputStream, PoijiExcelType excelType, Class type, PoijiOptions options) { return Poiji.fromExcelProperties().withSource(inputStream, excelType).withJavaType(type).withOptions(options).get(); } /** * converts excel rows into a list of objects * * @param file * excel file ending with .xls or .xlsx. * @param type * type of the root object. * @param * type of the root object. * @return * the newly created a list of objects * * @throws PoijiException * if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension * if the specified excel file extension is invalid. * @throws IllegalCastException * if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * * @see Poiji#fromExcel(File, Class, PoijiOptions) */ public static List fromExcel(final File file, final Class type) { return Poiji.fromExcel().withSource(file).withJavaType(type).toList(); } /** * converts excel rows into a list of objects * * @param file excel file ending with .xls or .xlsx. * @param type type of the root object. * @param type of the root object. * @param consumer output retrieves records * @throws PoijiException if an internal exception occurs during the mapping * process. * @throws InvalidExcelFileExtension if the specified excel file extension * is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * * @see Poiji#fromExcel(File, Class, PoijiOptions) */ public static void fromExcel(final File file, final Class type, final Consumer consumer) { Poiji.fromExcel().withSource(file).withJavaType(type).withConsumer(consumer).toConsume(); } /** * converts excel rows into a stream of objects * * @param file excel file ending with .xls or .xlsx. * @param type type of the root object. * @param type of the root object. * @throws PoijiException if an internal exception occurs during the mapping * process. * @throws InvalidExcelFileExtension if the specified excel file extension * is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * * @see Poiji#fromExcel(File, Class, PoijiOptions) */ public static Stream fromExcelToStream(final File file, final Class type) { return Poiji.fromExcel().withSource(file).withJavaType(type).toStream(); } /** * converts excel rows into a list of objects * * @param inputStream excel file stream * @param excelType type of the excel file, xls or xlsx * @param type type of the root object. * @param type of the root object. * @return the newly created a list of objects * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class, PoijiOptions) */ public static List fromExcel( final InputStream inputStream, PoijiExcelType excelType, final Class type ) { return Poiji.fromExcel().withSource(inputStream, excelType).withJavaType(type).toList(); } /** * converts excel rows into a list of objects * * @param inputStream excel file stream * @param excelType type of the excel file, xls or xlsx * @param type type of the root object. * @param type of the root object. * @param consumer represents an operation that accepts the type argument * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class, PoijiOptions) */ public static void fromExcel( final InputStream inputStream, PoijiExcelType excelType, final Class type, final Consumer consumer ) { Poiji.fromExcel().withSource(inputStream, excelType).withJavaType(type).withConsumer(consumer).toConsume(); } /** * converts excel rows into a stream of objects * * @param inputStream excel file stream * @param excelType type of the excel file, xls or xlsx * @param type type of the root object. * @param type of the root object. * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class, PoijiOptions) */ public static Stream fromExcelToStream(final InputStream inputStream, PoijiExcelType excelType, final Class type) { return Poiji.fromExcel().withSource(inputStream, excelType).withJavaType(type).toStream(); } /** * converts excel rows into a list of objects * * @param file excel file ending with .xls or .xlsx. * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @return the newly created a list of objects * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class) */ public static List fromExcel(final File file, final Class type, final PoijiOptions options) { return Poiji.fromExcel().withSource(file).withJavaType(type).withOptions(options).toList(); } /** * converts excel rows into a list of objects * * @param file excel file ending with .xls or .xlsx. * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @param consumer represents an operation that accepts the type argument * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class) */ public static void fromExcel( final File file, final Class type, final PoijiOptions options, final Consumer consumer ) { Poiji.fromExcel().withSource(file).withJavaType(type).withOptions(options).withConsumer(consumer).toConsume(); } /** * converts excel rows into a stream of objects * * @param file excel file ending with .xls or .xlsx. * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class) */ public static Stream fromExcelToStream(final File file, final Class type, final PoijiOptions options) { return Poiji.fromExcel().withSource(file).withJavaType(type).withOptions(options).toStream(); } /** * converts excel rows into a list of objects * * @param inputStream excel file stream * @param excelType type of the excel file, xls or xlsx * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @return the newly created a list of objects * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class) */ public static List fromExcel( final InputStream inputStream, final PoijiExcelType excelType, final Class type, final PoijiOptions options ) { return Poiji.fromExcel().withSource(inputStream, excelType).withJavaType(type).withOptions(options).toList(); } /** * converts excel rows into a list of objects * * @param inputStream excel file stream * @param excelType type of the excel file, xls or xlsx * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @param consumer represents an operation that accepts the type argument * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java * language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class) */ public static void fromExcel(final InputStream inputStream, final PoijiExcelType excelType, final Class type, final PoijiOptions options, final Consumer consumer ) { Poiji.fromExcel().withSource(inputStream, excelType).withJavaType(type).withOptions(options).withConsumer(consumer).toConsume(); } /** * converts excel rows into a stream of objects * * @param inputStream excel file stream * @param excelType type of the excel file, xls or xlsx * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @throws PoijiException if an internal exception occurs during the mapping process. * @throws InvalidExcelFileExtension if the specified excel file extension is invalid. * @throws IllegalCastException if this Field object is enforcing Java * language access control and the underlying field is either inaccessible or final. * @see Poiji#fromExcel(File, Class) */ public static Stream fromExcelToStream( final InputStream inputStream, final PoijiExcelType excelType, final Class type, final PoijiOptions options ) { return Poiji.fromExcel().withSource(inputStream, excelType).withJavaType(type).withOptions(options).toStream(); } /** * converts excel rows into a list of objects * * @param sheet excel sheet its workbook must be either an instance of {@code HSSFWorkbook} or {@code XSSFWorkbook}. * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @throws PoijiException if an internal exception occurs during the mapping process. * @see Poiji#fromExcel(Sheet, Class, PoijiOptions, Consumer) * @see Poiji#fromExcel(Sheet, Class) */ public static List fromExcel(final Sheet sheet, final Class type, final PoijiOptions options) { return Poiji.fromExcel().withSource(sheet).withJavaType(type).withOptions(options).toList(); } /** * converts excel rows into a list of objects * * @param sheet excel sheet its workbook must be either an instance of {@code HSSFWorkbook} or {@code XSSFWorkbook}. * @param type type of the root object. * @param type of the root object. * @throws PoijiException if an internal exception occurs during the mapping process. * @see Poiji#fromExcel(Sheet, Class, PoijiOptions) * @see Poiji#fromExcel(Sheet, Class, PoijiOptions, Consumer) */ public static List fromExcel(final Sheet sheet, final Class type) { return Poiji.fromExcel().withSource(sheet).withJavaType(type).toList(); } /** * converts excel rows into a stream of objects * * @param sheet excel sheet its workbook must be either an instance of {@code HSSFWorkbook} or {@code XSSFWorkbook}. * @param type type of the root object. * @param type of the root object. * @throws PoijiException if an internal exception occurs during the mapping process. * @see Poiji#fromExcel(Sheet, Class, PoijiOptions) * @see Poiji#fromExcel(Sheet, Class, PoijiOptions, Consumer) */ public static Stream fromExcelToStream(final Sheet sheet, final Class type) { return Poiji.fromExcel().withSource(sheet).withJavaType(type).toStream(); } /** * converts excel rows into a list of objects * * @param sheet excel sheet its workbook must be either an instance of {@code HSSFWorkbook} or {@code XSSFWorkbook}. * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @param consumer represents an operation that accepts the type argument. * @throws PoijiException if an internal exception occurs during the mapping process. * @see Poiji#fromExcel(Sheet, Class, PoijiOptions) * @see Poiji#fromExcel(Sheet, Class) */ public static void fromExcel( final Sheet sheet, final Class type, final PoijiOptions options, final Consumer consumer ) { Poiji.fromExcel().withSource(sheet).withJavaType(type).withOptions(options).withConsumer(consumer).toConsume(); } /** * converts excel rows into a stream of objects * * @param sheet excel sheet its workbook must be either an instance of {@code HSSFWorkbook} or {@code XSSFWorkbook}. * @param type type of the root object. * @param type of the root object. * @param options specifies to change the default behaviour of the poiji. * @throws PoijiException if an internal exception occurs during the mapping process. * @see Poiji#fromExcel(Sheet, Class, PoijiOptions) * @see Poiji#fromExcel(Sheet, Class) */ public static Stream fromExcelToStream( final Sheet sheet, final Class type, final PoijiOptions options ) { return Poiji.fromExcel().withSource(sheet).withJavaType(type).withOptions(options).toStream(); } public static void toExcel(final File file, final Class clazz, final Collection data) { Poiji.toExcel().withJavaType(clazz).withSource(data).withDestination(file).save(); } public static void toExcel( final File file, final Class clazz, final Collection data, final PoijiOptions options ) { Poiji.toExcel().withJavaType(clazz).withSource(data).withDestination(file).withOptions(options).save(); } public static void toExcel( final OutputStream outputStream, final PoijiExcelType excelType, final Class clazz, final Collection data ) { Poiji.toExcel().withJavaType(clazz).withSource(data).withDestination(outputStream, excelType).save(); } public static void toExcel( final OutputStream outputStream, final PoijiExcelType excelType, final Class clazz, final Collection data, final PoijiOptions options ) { Poiji.toExcel().withJavaType(clazz).withSource(data).withDestination(outputStream, excelType).withOptions(options).save(); } public static void toExcel(final File file, final Class clazz, final Stream data) { Poiji.toExcel().withJavaType(clazz).withSource(data).withDestination(file).save(); } public static void toExcel( final File file, final Class clazz, final Stream data, final PoijiOptions options ) { Poiji.toExcel().withJavaType(clazz).withSource(data).withDestination(file).withOptions(options).save(); } public static void toExcel( final OutputStream outputStream, final PoijiExcelType excelType, final Class clazz, final Stream data ) { Poiji.toExcel().withJavaType(clazz).withSource(data).withDestination(outputStream, excelType).save(); } public static void toExcel( final OutputStream outputStream, final PoijiExcelType excelType, final Class clazz, final Stream data, final PoijiOptions options ) { Poiji.toExcel().withJavaType(clazz).withSource(data).withDestination(outputStream, excelType).withOptions(options).save(); } public static ToExcel toExcel(){ return new ToExcel<>(); } public static FromExcel fromExcel(){ return new FromExcel<>(); } public static FromExcelProperties fromExcelProperties(){ return new FromExcelProperties<>(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy