View Javadoc
1   package com.github.mygreen.supercsv.cellprocessor.constraint;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.List;
6   import java.util.Optional;
7   
8   import org.supercsv.cellprocessor.ift.CellProcessor;
9   
10  import com.github.mygreen.supercsv.annotation.constraint.CsvWordRequire;
11  import com.github.mygreen.supercsv.builder.Configuration;
12  import com.github.mygreen.supercsv.builder.FieldAccessor;
13  import com.github.mygreen.supercsv.cellprocessor.ConstraintProcessorFactory;
14  import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
15  import com.github.mygreen.supercsv.exception.SuperCsvInvalidAnnotationException;
16  import com.github.mygreen.supercsv.localization.MessageBuilder;
17  
18  /**
19   * アノテーション{@link CsvWordRequire}をハンドリングして、CellProcessorの{@link WordRequire}を作成する。
20   * 
21   * @since 2.0
22   * @author T.TSUCHIE
23   *
24   */
25  public class WordRequireFactory implements ConstraintProcessorFactory<CsvWordRequire> {
26      
27      @Override
28      public Optional<CellProcessor> create(final CsvWordRequire anno, final Optional<CellProcessor> next,
29              final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
30          
31          final List<String> words = new ArrayList<>();
32          if(anno.value().length > 0) {
33              words.addAll(Arrays.asList(anno.value()));
34          }
35              
36          if(anno.provider().length > 0) {
37              final RequiredWordProvider./com/github/mygreen/supercsv/cellprocessor/constraint/RequiredWordProvider.html#RequiredWordProvider">RequiredWordProvider provider = (RequiredWordProvider) config.getBeanFactory().create(anno.provider()[0]);
38              words.addAll(provider.getRequiredWords(field));
39              
40          }
41          
42          if(anno.value().length == 0 && anno.provider().length == 0) {
43              throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.required")
44                      .var("property", field.getNameWithClass())
45                      .varWithAnno("anno", anno.annotationType())
46                      .var("attrName", "value or provider")
47                      .format());
48          }
49          
50          final WordRequireraint/WordRequire.html#WordRequire">WordRequire processor = next.map(n -> new WordRequire(words, n))
51                  .orElseGet(() -> new WordRequire(words));
52          processor.setValidationMessage(anno.message());
53          
54          return Optional.of(processor);
55      }
56      
57  }