View Javadoc
1   package com.github.mygreen.supercsv.cellprocessor.constraint;
2   
3   import java.util.Optional;
4   
5   import org.supercsv.cellprocessor.ift.CellProcessor;
6   
7   import com.github.mygreen.supercsv.annotation.constraint.CsvUniqueHashCode;
8   import com.github.mygreen.supercsv.builder.Configuration;
9   import com.github.mygreen.supercsv.builder.FieldAccessor;
10  import com.github.mygreen.supercsv.cellprocessor.ConstraintProcessorFactory;
11  import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
12  
13  /**
14   * アノテーション{@link CsvUniqueHashCode}をハンドリングして、CellProcessorの{@link UniqueHashCode}を作成する。
15   * 
16   * @since 2.0
17   * @author T.TSUCHIE
18   *
19   */
20  public class UniqueHashCodeFactory<T> implements ConstraintProcessorFactory<CsvUniqueHashCode> {
21      
22      @Override
23      public Optional<CellProcessor> create(final CsvUniqueHashCode anno, final Optional<CellProcessor> next,
24              final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
25          
26          @SuppressWarnings("unchecked")
27          final TextFormatter<T> typeFormatter = (TextFormatter<T>)formatter;
28          
29          final UniqueHashCode<T> processor = next.map(n -> new UniqueHashCode<T>(typeFormatter, n))
30                  .orElseGet(() -> new UniqueHashCode<T>(typeFormatter));
31          
32          processor.setValidationMessage(anno.message());
33          
34          return Optional.of(processor);
35      }
36      
37  }