FieldProcessorRegistry.java

  1. package com.gh.mygreen.xlsmapper.fieldprocessor;

  2. import java.lang.annotation.Annotation;
  3. import java.util.Map;
  4. import java.util.concurrent.ConcurrentHashMap;

  5. import com.gh.mygreen.xlsmapper.annotation.XlsArrayCells;
  6. import com.gh.mygreen.xlsmapper.annotation.XlsCell;
  7. import com.gh.mygreen.xlsmapper.annotation.XlsComment;
  8. import com.gh.mygreen.xlsmapper.annotation.XlsHorizontalRecords;
  9. import com.gh.mygreen.xlsmapper.annotation.XlsIterateTables;
  10. import com.gh.mygreen.xlsmapper.annotation.XlsLabelledArrayCells;
  11. import com.gh.mygreen.xlsmapper.annotation.XlsLabelledCell;
  12. import com.gh.mygreen.xlsmapper.annotation.XlsLabelledComment;
  13. import com.gh.mygreen.xlsmapper.annotation.XlsSheetName;
  14. import com.gh.mygreen.xlsmapper.annotation.XlsVerticalRecords;
  15. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.ArrayCellsProcessor;
  16. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.CellProcessor;
  17. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.CommentProcessor;
  18. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.HorizontalRecordsProcessor;
  19. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.IterateTablesProcessor;
  20. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.LabelledArrayCellsProcessor;
  21. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.LabelledCellProcessor;
  22. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.LabelledCommentProcessor;
  23. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.SheetNameProcessor;
  24. import com.gh.mygreen.xlsmapper.fieldprocessor.impl.VerticalRecordsProcessor;
  25. import com.gh.mygreen.xlsmapper.util.ArgUtils;


  26. /**
  27.  * {@link FieldProcessor}を管理するクラス。
  28.  *
  29.  * @version 2.1
  30.  * @author Naoki Takezoe
  31.  * @author T.TSUCHIE
  32.  *
  33.  */
  34. public class FieldProcessorRegistry {
  35.    
  36.     private Map<Class<? extends Annotation>, FieldProcessor<?>> pocessorMap = new ConcurrentHashMap<>();
  37.    
  38.     public FieldProcessorRegistry() {
  39.         this.pocessorMap = new ConcurrentHashMap<>();
  40.        
  41.         init();
  42.     }
  43.    
  44.     /**
  45.      * {@link FieldProcessor}の登録状態を初期値に戻します。
  46.      */
  47.     public void init() {
  48.        
  49.         pocessorMap.clear();
  50.        
  51.         //標準のフィールドプロセッサを登録する。
  52.         registerProcessor(XlsSheetName.class, new SheetNameProcessor());
  53.         registerProcessor(XlsCell.class, new CellProcessor());
  54.         registerProcessor(XlsLabelledCell.class, new LabelledCellProcessor());
  55.         registerProcessor(XlsHorizontalRecords.class, new HorizontalRecordsProcessor());
  56.         registerProcessor(XlsVerticalRecords.class, new VerticalRecordsProcessor());
  57.         registerProcessor(XlsIterateTables.class, new IterateTablesProcessor());
  58.         registerProcessor(XlsArrayCells.class, new ArrayCellsProcessor());
  59.         registerProcessor(XlsLabelledArrayCells.class, new LabelledArrayCellsProcessor());
  60.         registerProcessor(XlsComment.class, new CommentProcessor());
  61.         registerProcessor(XlsLabelledComment.class, new LabelledCommentProcessor());
  62.        
  63.     }
  64.    
  65.     /**
  66.      * アノテーションに対する{@link FieldProcessor}を取得する。
  67.      * @param annoClass  取得対象のアノテーションのクラスタイプ。
  68.      * @return 見つからない場合はnullを返す。
  69.      * @throws NullPointerException {@literal annoClass}
  70.      */
  71.     @SuppressWarnings("unchecked")
  72.     public <A extends Annotation> FieldProcessor<A> getProcessor(final Class<A> annoClass) {
  73.         ArgUtils.notNull(annoClass, "annoClass");
  74.        
  75.         return (FieldProcessor<A>) pocessorMap.get(annoClass);
  76.     }
  77.    
  78.     /**
  79.      * アノテーションに対する{@link FieldProcessor}を登録する。
  80.      * @param annoClass 登録対象のアノテーションのクラスタイプ。
  81.      * @param processor フィールドプロセッサーのインスタンス。{@link FieldProcessor}を実装している必要がある。
  82.      * @throws NullPointerException {@literal annoClass == null or processor == null.}
  83.      */
  84.     public <A extends Annotation> void registerProcessor(final Class<A> annoClass, final FieldProcessor<A> processor) {
  85.         ArgUtils.notNull(annoClass, "annoClass");
  86.         ArgUtils.notNull(processor, "processor");
  87.        
  88.         pocessorMap.put(annoClass, processor);
  89.        
  90.     }
  91.    
  92. }