ReplacedWordProvider.java

  1. package com.github.mygreen.supercsv.cellprocessor.conversion;

  2. import java.io.Serializable;
  3. import java.util.Collection;

  4. import com.github.mygreen.supercsv.annotation.conversion.CsvWordReplace;
  5. import com.github.mygreen.supercsv.builder.FieldAccessor;
  6. import com.github.mygreen.supercsv.util.ArgUtils;

  7. /**
  8.  * {@link CsvWordReplace}による語彙による置換処理をを個ナウ際の語彙を提供するためのインタフェースです。
  9.  * <p>語彙を別ファイルやDBから取得する時などサービスクラスとして実装します。</p>
  10.  *
  11.  * <p>基本的な使い方は、 {@link CsvWordReplace}のJavaDocを参照してください。</p>
  12.  *
  13.  * <h3 class="description">フィールドごとにリソースを切り替えたい場合</h3>
  14.  * <p>フィールドごとにリソースを切り替えたい場合は、メソッドの引数{@link FieldAccessor}で判定を行います。</p>
  15.  * <p>また、独自のパラメータを渡したい時は、独自のアノテーションを作成し、それをフィールドに付与して、
  16.  *    引数{@link FieldAccessor}から取得して判定します。
  17.  * </p>
  18.  *
  19.  * <pre class="highlight"><code class="java">
  20.  * // 読み込むリソースを定義されているフィールドやクラスで分ける場合
  21.  * public class FileReplacedWordProvider implements ReplacedWordProvider {
  22.  *    
  23.  *     {@literal @Override}
  24.  *     public {@literal Collection<Word>} getReplacedWords(final FieldAccessor field) {
  25.  *        
  26.  *         final String path;
  27.  *         if(field.getDeclaredClass().equals(AdminCsv.class)) {
  28.  *             path = "replaced_word_admin.txt";
  29.  *         } else {
  30.  *             path = "replaced_word.txt";
  31.  *         }
  32.  *        
  33.  *         // ファイルから語彙の定義を読み込む
  34.  *         {@literal List<String>} lines;
  35.  *         try {
  36.  *              
  37.  *              lines = Files.readAllLines(new File(path).toPath(), Charset.forName("UTF-8"));
  38.  *              
  39.  *         } catch (IOException e) {
  40.  *             throw new RuntimeException("fail reading the replaced words file.", e);
  41.  *         }
  42.  *        
  43.  *         // 読み込んだ各行の値を分割して、ReplacedWordProvider.Word クラスに変換する。
  44.  *         return lines.stream()
  45.  *             .map(l {@literal ->} l.split(","))
  46.  *             .map(s {@literal ->} new Word(s[0], s[1]))
  47.  *             .collect(Collectors.toLit());
  48.  *        
  49.  *     }
  50.  * }
  51.  * </code></pre>
  52.  *
  53.  * <h3 class="description">Spring Frameworkと連携する場合</h3>
  54.  * <p>Springと連携している場合は、プロバイダクラスをSpringBeanとして登録しておくことでインジェクションできます。</p>
  55.  * <p>また、メソッド{@link ReplacedWordProvider#getReplacedWords(FieldAccessor)}は、定義したフィールド単位に呼ばれるため、多数のフィールドで定義していると何度も呼ばれ、効率が悪くなる場合があります。
  56.  *   <br>このようなときは、Spring Framework3.1から追加された Cache Abstraction(キャッシュの抽象化)機能を使うと改善できます。
  57.  * </p>
  58.  *
  59.  * <pre class="highlight"><code class="java">
  60.  * // SpringBeanとして登録する場合。
  61.  * {@literal @Service}
  62.  * {@literal @Transactional}
  63.  * public class ReplacedWordProviderImpl implements ReplacedWordProvider {
  64.  *    
  65.  *     // リポジトリのインジェクション
  66.  *     {@literal @Autowired}
  67.  *     private ReplacedWordRepository replacedWordRepository;
  68.  *    
  69.  *     {@literal @Override}
  70.  *     public {@literal Collection<Word>} getReplacedWords(final FieldAccessor field) {
  71.  *        
  72.  *         final Role role;
  73.  *         if(field.getDeclaredClass().equals(AdminCsv.class)) {
  74.  *             role = Role.admin;
  75.  *         } else {
  76.  *             role = Role.normal;
  77.  *         }
  78.  *        
  79.  *         return loadWords(role).stream()
  80.  *             .map(dto {@literal ->} new Word(dto.getWord(), dto.getReplacement()))
  81.  *             .collect(Collectors.toList());
  82.  *        
  83.  *     }
  84.  *    
  85.  *     // リポジトリから取得した内容をキャッシュする。
  86.  *     // 引数 role をキーにして、区別する。
  87.  *     {@literal @Transactional(readOnly = true)}
  88.  *     {@literal @Cacheable(cacheNames="replacedWords", key="#role")}
  89.  *     public {@literal List<WordDto>} loadWords(Role role) {
  90.  *          
  91.  *          if(role.euals(Role.admin)) {
  92.  *              return replacedWordRepository.findByRole(role);
  93.  *          } else {
  94.  *              return replacedWordRepository.findAll();
  95.  *          }
  96.  *          
  97.  *     }
  98.  * }
  99.  * </code></pre>
  100.  *
  101.  * @since 2.0
  102.  * @author T.TSUCHIE
  103.  *
  104.  */
  105. @FunctionalInterface
  106. public interface ReplacedWordProvider {
  107.    
  108.     /**
  109.      * 語彙の一覧を取得する。
  110.      * @param field フィールド情報
  111.      * @return 語彙を返します。チェック対象の文字がない場合は、空のリストを返します。
  112.      */
  113.     Collection<Word> getReplacedWords(FieldAccessor field);
  114.    
  115.     /**
  116.      * 置換語彙を表現するクラス。
  117.      *
  118.      * @since 2.0.1
  119.      * @author T.TSUCHIE
  120.      *
  121.      */
  122.     public static class Word implements Serializable {
  123.        
  124.         /** serialVersionUID */
  125.         private static final long serialVersionUID = 1L;
  126.        
  127.         private final String word;
  128.        
  129.         private final String replacement;
  130.        
  131.         /**
  132.          * 置換語彙のコンストラクタ。
  133.          * @param word 置換対象の文字。
  134.          * @param replacement 置換後の文字。
  135.          * @throws NullPointerException word or replacement is null.
  136.          * @throws IllegalArgumentException word is empty.
  137.          */
  138.         public Word(final String word, final String replacement) {
  139.             ArgUtils.notEmpty(word, "word");
  140.             ArgUtils.notNull(replacement, "replacement");
  141.            
  142.             this.word = word;
  143.             this.replacement = replacement;
  144.         }
  145.        
  146.         /**
  147.          * 置換対象の語彙を取得する。
  148.          * @return 置換対象の語彙。
  149.          */
  150.         public String getWord() {
  151.             return word;
  152.         }
  153.        
  154.         /**
  155.          * 置換後の文字列を返します。
  156.          * @return 置換語彙の文字列
  157.          */
  158.         public String getReplacement() {
  159.             return replacement;
  160.         }
  161.        
  162.     }
  163.    
  164. }