com.github.mygreen.supercsv.builder.standard.AbstractNumberProcessorBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of super-csv-annotation Show documentation
Show all versions of super-csv-annotation Show documentation
CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。
package com.github.mygreen.supercsv.builder.standard;
import java.math.MathContext;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Currency;
import java.util.Locale;
import java.util.Optional;
import org.supercsv.cellprocessor.ift.CellProcessor;
import com.github.mygreen.supercsv.annotation.constraint.CsvNumberMax;
import com.github.mygreen.supercsv.annotation.constraint.CsvNumberMin;
import com.github.mygreen.supercsv.annotation.constraint.CsvNumberRange;
import com.github.mygreen.supercsv.annotation.format.CsvNumberFormat;
import com.github.mygreen.supercsv.builder.AbstractProcessorBuilder;
import com.github.mygreen.supercsv.builder.Configuration;
import com.github.mygreen.supercsv.builder.FieldAccessor;
import com.github.mygreen.supercsv.cellprocessor.constraint.NumberMaxFactory;
import com.github.mygreen.supercsv.cellprocessor.constraint.NumberMinFactory;
import com.github.mygreen.supercsv.cellprocessor.constraint.NumberRangeFactory;
import com.github.mygreen.supercsv.cellprocessor.format.NumberFormatWrapper;
import com.github.mygreen.supercsv.cellprocessor.format.SimpleNumberFormatter;
import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
import com.github.mygreen.supercsv.util.Utils;
/**
* 数値型に対する{@link CellProcessor}を組み立てるクラス。
* 各種タイプごとに実装を行う。
*
* @version 2.0
* @author T.TSUCHIE
*
*/
public abstract class AbstractNumberProcessorBuilder> extends AbstractProcessorBuilder {
public AbstractNumberProcessorBuilder() {
super();
}
@Override
protected void init() {
super.init();
// 制約のアノテーションの追加
registerForConstraint(CsvNumberRange.class, new NumberRangeFactory<>());
registerForConstraint(CsvNumberMin.class, new NumberMinFactory<>());
registerForConstraint(CsvNumberMax.class, new NumberMaxFactory<>());
}
@Override
protected TextFormatter getDefaultFormatter(final FieldAccessor field, final Configuration config) {
final Optional> formatter = createFormatter(field, config);
if(formatter.isPresent()) {
return formatter.get();
} else {
return createSimpleFormatter(field, config);
}
}
/**
* 数値のフォーマッタを作成する。
* アノテーション{@link CsvNumberFormat}の値を元に作成します。
* @param field フィールド情報
* @param config システム設定
* @return アノテーション{@link CsvNumberFormat}が付与されていない場合は、空を返す。
*/
@SuppressWarnings("unchecked")
protected Optional> createFormatter(final FieldAccessor field, final Configuration config) {
final Optional formatAnno = field.getAnnotation(CsvNumberFormat.class);
if(!formatAnno.isPresent()) {
return Optional.empty();
}
final String pattern = formatAnno.get().pattern();
if(pattern.isEmpty()) {
return Optional.empty();
}
final boolean lenient = formatAnno.get().lenient();
final Locale locale = Utils.getLocale(formatAnno.get().locale());
final Optional currency = formatAnno.get().currency().isEmpty() ? Optional.empty()
: Optional.of(Currency.getInstance(formatAnno.get().currency()));
final RoundingMode roundingMode = formatAnno.get().rounding();
final DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
final DecimalFormat formatter = new DecimalFormat(pattern, symbols);
formatter.setParseBigDecimal(true);
formatter.setRoundingMode(roundingMode);
currency.ifPresent(c -> formatter.setCurrency(c));
final NumberFormatWrapper wrapper = new NumberFormatWrapper<>(formatter, (Class)field.getType(), lenient);
wrapper.setValidationMessage(formatAnno.get().message());
return Optional.of(wrapper);
}
/**
* アノテーション{@link CsvNumberFormat}の指定がないときなどの書式のない数値のフォーマッタの作成。
* {@link #createFormatter(FieldAccessor, Configuration)}で結果が空の時に使用する。
*
* @param field フィールド情報
* @param config システム設定
* @return
*/
@SuppressWarnings("unchecked")
protected SimpleNumberFormatter createSimpleFormatter(final FieldAccessor field, final Configuration config) {
final Optional formatAnno = field.getAnnotation(CsvNumberFormat.class);
if(!formatAnno.isPresent()) {
return new SimpleNumberFormatter<>((Class)field.getType(), false);
}
final boolean lenient = formatAnno.get().lenient();
final RoundingMode roundingMode = formatAnno.get().rounding();
final int precision = formatAnno.get().precision();
final SimpleNumberFormatter formatter;
if(precision >= 0) {
formatter = new SimpleNumberFormatter<>((Class)field.getType(), lenient, new MathContext(precision, roundingMode));
} else {
formatter = new SimpleNumberFormatter<>((Class)field.getType(), lenient);
}
formatter.setValidationMessage(formatAnno.get().message());
return formatter;
}
}