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

org.supercsv.ext.builder.impl.SqlDateCellProcessorBuilder Maven / Gradle / Ivy

Go to download

CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。

There is a newer version: 2.3
Show newest version
package org.supercsv.ext.builder.impl;

import java.lang.annotation.Annotation;
import java.sql.Date;
import java.text.DateFormat;
import java.util.Optional;

import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.cellprocessor.ift.DateCellProcessor;
import org.supercsv.cellprocessor.ift.StringCellProcessor;
import org.supercsv.ext.annotation.CsvDateConverter;
import org.supercsv.ext.builder.CellProcessorBuilder;
import org.supercsv.ext.cellprocessor.FormatLocaleDate;
import org.supercsv.ext.cellprocessor.ParseLocaleSqlDate;

/**
 * {@link Date}型の{@link CellProcessorBuilder}クラス。
 * 
 * @version 1.2
 * @author T.TSUCHIE
 *
 */
 
public class SqlDateCellProcessorBuilder extends AbstractDateCellProcessorBuilder {
    
    @Override
    public String getDefaultPattern() {
        return "yyyy-MM-dd";
    }
    
    @Override
    public CellProcessor buildOutputCellProcessor(final Class type, final Annotation[] annos,
            final CellProcessor processor, final boolean ignoreValidationProcessor) {
        
        final Optional converterAnno = getDateConverterAnnotation(annos);
        final DateFormat formatter = createDateFormatter(converterAnno);
        
        final Optional min = getMin(converterAnno).map(s -> parseValue(type, annos, s).get());
        final Optional max = getMax(converterAnno).map(s -> parseValue(type, annos, s).get());
        
        CellProcessor cp = processor;
        cp = (cp == null ? new FormatLocaleDate(formatter) : new FormatLocaleDate(formatter, (StringCellProcessor) cp));
        
        if(!ignoreValidationProcessor) {
            cp = prependRangeProcessor(type, annos, cp, min, max);;
        }
        return cp;
    }
    
    @Override
    public CellProcessor buildInputCellProcessor(final Class type, final Annotation[] annos, 
            final CellProcessor processor) {
        
        final Optional converterAnno = getDateConverterAnnotation(annos);
        final DateFormat formatter = createDateFormatter(converterAnno);
        
        final Optional min = getMin(converterAnno).map(s -> parseValue(type, annos, s).get());
        final Optional max = getMax(converterAnno).map(s -> parseValue(type, annos, s).get());
        
        CellProcessor cp = processor;
        cp = prependRangeProcessor(type, annos, cp, min, max);
        
        cp = (cp == null ? new ParseLocaleSqlDate(formatter) : new ParseLocaleSqlDate(formatter, (DateCellProcessor)cp));
        
        return cp;
        
    }
    
    @Override
    public Optional parseValue(final Class type, final Annotation[] annos, final String strValue) {
        
        return parseDate(annos, strValue).map(d -> new Date(d.getTime()));
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy