View Javadoc
1   package com.github.mygreen.supercsv.cellprocessor.conversion;
2   
3   import org.supercsv.cellprocessor.CellProcessorAdaptor;
4   import org.supercsv.cellprocessor.ift.StringCellProcessor;
5   import org.supercsv.util.CsvContext;
6   
7   /**
8    * 一致する語彙を置換するCellProcessor。
9    * 
10   * @since 2.0
11   * @author T.TSUCHIE
12   *
13   */
14  public class WordReplace extends CellProcessorAdaptor implements StringCellProcessor {
15      
16      private final CharReplacer replacer;
17      
18      public WordReplace(final CharReplacer replacer) {
19          super();
20          checkPreconditions(replacer);
21          this.replacer = replacer;
22      }
23      
24      public WordReplace(final CharReplacer replacer, final StringCellProcessor next) {
25          super(next);
26          checkPreconditions(replacer);
27          this.replacer = replacer;
28      }
29      
30      private static void checkPreconditions(final CharReplacer replacer) {
31          if(replacer == null) {
32              throw new NullPointerException("replacer should not be null.");
33          }
34      }
35      
36      @Override
37      public <T> T execute(final Object value, final CsvContext context) {
38          
39          if(value == null) {
40              return next.execute(value, context);
41          }
42          
43          final String result = replacer.replace(value.toString());
44          return next.execute(result, context);
45      }
46      
47  }