org.nerd4j.csv.reader.binding.AbstractCSVToModelBinderFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nerd4j-csv Show documentation
Show all versions of nerd4j-csv Show documentation
CSV manipulation library.
/*
* #%L
* Nerd4j CSV
* %%
* Copyright (C) 2013 Nerd4j
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nerd4j.csv.reader.binding;
import java.lang.reflect.Array;
import org.nerd4j.csv.exception.CSVToModelBindingException;
import org.nerd4j.csv.field.CSVFieldMetadata;
import org.nerd4j.csv.reader.CSVReaderMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Abstract implementation of the {@link CSVToModelBinderFactory}
* that contains the code common to all the concrete implementations.
*
* @param type of the model returned by the builder.
* @param type of the column mapping descriptor.
*
* @author Nerd4j Team
*/
public abstract class AbstractCSVToModelBinderFactory implements CSVToModelBinderFactory
{
/** Internal logging system. */
private static final Logger logger = LoggerFactory.getLogger( AbstractCSVToModelBinderFactory.class );
/** Type of the object used to map the columns to the model. */
private final Class mappingType;
/**
* Constructor with parameters.
*
* @param mappingType type of the object used to map the columns to the model.
*/
public AbstractCSVToModelBinderFactory( Class mappingType )
throws CSVToModelBindingException
{
super();
if( mappingType == null )
throw new CSVToModelBindingException( "The column mapping type is mandatory" );
this.mappingType = mappingType;
}
/* ******************* */
/* INTERFACE METHODS */
/* ******************* */
/**
* {@inheritDoc}
*/
@Override
public CSVToModelBinder getCSVToModelBinder( final CSVReaderMetadata configuration,
final Integer[] columnMapping )
throws CSVToModelBindingException
{
if( configuration == null )
throw new CSVToModelBindingException( "The CSV reader configuration is mandatory" );
if( columnMapping == null )
throw new CSVToModelBindingException( "The CSV column mapping is mandatory" );
return createBinder( configuration, columnMapping );
}
/* ***************** */
/* PRIVATE METHODS */
/* ***************** */
/**
* Creates a new model binder using the provided configuration.
*
* @param configuration used to configure the column mapping for binding.
* @param columnMapping mapping of the CSV source columns.
*/
@SuppressWarnings("unchecked")
private CSVToModelBinder createBinder( final CSVReaderMetadata configuration,
final Integer[] columnMapping )
throws CSVToModelBindingException
{
try{
final CSVFieldMetadata,?>[] fieldConfs = configuration.getFieldConfigurations();
/* If the mapping is empty we return an empty binder. */
if( columnMapping == null || fieldConfs == null || fieldConfs.length < 1 )
{
final Mapping[] fieldMapping = (Mapping[]) Array.newInstance( mappingType, 0 );
return getBinder( fieldConfs, fieldMapping );
}
/* Otherwise we create the internal structure. */
final Mapping[] fieldMapping = (Mapping[]) Array.newInstance( mappingType, columnMapping.length );
for( int i = 0; i < columnMapping.length; ++i )
{
fieldMapping[i] = columnMapping[i] != null
? getMapping( fieldConfs[columnMapping[i]].getMappingDescriptor() )
: null;
}
return getBinder( fieldConfs, fieldMapping );
}catch( Exception ex )
{
logger.error( "Unexpected error during model binder creation.", ex );
throw new CSVToModelBindingException( ex );
}
}
/* ***************** */
/* EXTENSION HOOKS */
/* ***************** */
/**
* Given the mapping descriptor in {@link String} format
* returns the actual mapping object.
*
* @param mappingDescriptor the mapping descriptor.
* @return the actual mapping object.
*/
protected abstract Mapping getMapping( final String mappingDescriptor );
/**
* Given the original field configurations and the computed column mapping
* returns an instance of {@link CSVToModelBinder}.
*
* @param fieldConfs original fields configurations.
* @param fieldMapping computed column mapping.
* @return the actual {@link CSVToModelBinder}.
*/
protected abstract CSVToModelBinder getBinder( final CSVFieldMetadata,?>[] fieldConfs, final Mapping[] fieldMapping );
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy