org.nerd4j.csv.writer.CSVWriterMetadata 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.writer;
import org.nerd4j.csv.exception.CSVConfigurationException;
import org.nerd4j.csv.field.CSVFieldMetadata;
import org.nerd4j.csv.formatter.CSVFormatter;
import org.nerd4j.csv.formatter.CSVFormatterFactory;
import org.nerd4j.csv.formatter.CSVFormatterMetadata;
import org.nerd4j.csv.writer.binding.ModelToCSVBinder;
import org.nerd4j.csv.writer.binding.ModelToCSVBinderFactory;
/**
* Represents the meta-data used to tell a {@link CSVWriterFactoryImpl}
* how to build and configure the related {@link CSVWriter}s.
*
* @param type of the field mapping descriptor.
*
* @author Nerd4j Team
*/
public final class CSVWriterMetadata
{
/**
* Tells the {@link CSVWriter} to write the
* given header into the first row of the CSV destination.
*
* The default value for this flag is true
.
*
*/
private final boolean writeHeader;
/**
* Each index in the array corresponds to a column
* in the CSV and provides configurations about
* how to handle the related fields.
*
* This array cannot contain null
values.
*
*/
private final CSVFieldMetadata,String>[] fieldConfigurations;
/** The factory able to create {@link CSVFormatter}s. */
private final CSVFormatterFactory formatterFactory;
/** The factory able to create {@link ModelBinder}s. */
private final ModelToCSVBinderFactory modelBinderFactory;
/**
* Constructor with parameters.
*
* @param formatterMetadata the configuration of the formatter to use.
* @param modelBinderFactory the factory able to create {@link ModelToCSVBinder}s.
* @param fieldConfigurations the configurations related to the single fields.
* @param writeHeader tells if the header should be written.
*/
public CSVWriterMetadata( final CSVFormatterMetadata formatterMetadata,
final ModelToCSVBinderFactory modelBinderFactory,
final CSVFieldMetadata,String>[] fieldConfigurations,
final boolean writeHeader )
{
super();
if( fieldConfigurations == null || fieldConfigurations.length < 1 )
throw new CSVConfigurationException( "The field configurations are mandatory. Check the configuration" );
this.writeHeader = writeHeader;
this.modelBinderFactory = modelBinderFactory;
this.fieldConfigurations = fieldConfigurations;
this.formatterFactory = new CSVFormatterFactory( formatterMetadata );
this.checkConsistency();
}
/* ******************* */
/* GETTERS & SETTERS */
/* ******************* */
public boolean isWriteHeader()
{
return writeHeader;
}
public CSVFieldMetadata,String>[] getFieldConfigurations()
{
return fieldConfigurations;
}
public ModelToCSVBinderFactory getModelBinderFactory()
{
return modelBinderFactory;
}
public CSVFormatterFactory getFormatterFactory()
{
return formatterFactory;
}
/* ***************** */
/* PRIVATE METHODS */
/* ***************** */
/**
* Checks the consistency of the provided field configurations.
*
*/
private void checkConsistency()
{
if( modelBinderFactory == null )
throw new CSVConfigurationException( "The CSV Model Builder Factory is mandatory" );
if( formatterFactory == null )
throw new CSVConfigurationException( "The CSV Formatter Configuration is mandatory" );
for( CSVFieldMetadata,String> conf : fieldConfigurations )
if( conf == null )
throw new CSVConfigurationException( "The field configuration array cannot contain null entries. Check the configuration" );
if( writeHeader )
for( int i = 0; i < fieldConfigurations.length; ++i )
if( fieldConfigurations[i].getColumnName() == null || fieldConfigurations[i].getColumnName().isEmpty() )
throw new CSVConfigurationException( "The flag 'writeHeader' is true but there is no header for column " + i + ". Check the configuration" );
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy