DefaultCellFormatter.java

  1. package com.gh.mygreen.xlsmapper;

  2. import java.util.Locale;
  3. import java.util.Map;
  4. import java.util.concurrent.ConcurrentHashMap;

  5. import org.apache.poi.ss.usermodel.Cell;

  6. import com.github.mygreen.cellformatter.FormatterResolver;
  7. import com.github.mygreen.cellformatter.POICell;
  8. import com.github.mygreen.cellformatter.POICellFormatter;


  9. /**
  10.  * 標準のセルフォーマッター。
  11.  * 書式をフォーマットするライブラリ、<a href="https://github.com/mygreen/excel-cellformatter" target="_blank">excel-cellformatter</a>を利用する。
  12.  *
  13.  * @version 2.0
  14.  * @since 0.5
  15.  * @author T.TSUCHIE
  16.  *
  17.  */
  18. public class DefaultCellFormatter implements CellFormatter {

  19.     /**
  20.      * 値をキャッシュするかどうか
  21.      */
  22.     private boolean cached;

  23.     /**
  24.      * 値のキャッシュ
  25.      */
  26.     private Map<String, String> cacheData = new ConcurrentHashMap<>();

  27.     private POICellFormatter poiCellFormatter = new POICellFormatter();

  28.     @Override
  29.     public void init(boolean cached) {
  30.         setCached(cached);
  31.         clearCacheData();
  32.     }

  33.     @Override
  34.     public String format(final Cell cell) {
  35.         return format(cell, Locale.getDefault());
  36.     }

  37.     @Override
  38.     public String format(final Cell cell, final Locale locale) {

  39.         if(isCached()) {
  40.             final String cachedKey = createKey(cell, locale);
  41.             return cacheData.computeIfAbsent(cachedKey, key -> poiCellFormatter.formatAsString(cell, locale));

  42.         } else {
  43.             return poiCellFormatter.formatAsString(cell, locale);
  44.         }
  45.     }

  46.     /**
  47.      * キャッシュのキーを作成する。
  48.      * @param cell
  49.      * @param locale
  50.      * @return
  51.      */
  52.     private String createKey(final Cell cell, final Locale locale) {

  53.         if(cell == null) {
  54.             return "empty_cell_value";
  55.         }

  56.         StringBuilder sb = new StringBuilder();
  57.         sb.append(":sheet=").append(cell.getSheet().getSheetName());
  58.         sb.append(":address=").append(cell.getAddress().formatAsString());
  59.         sb.append(":locale=").append(locale.toString());

  60.         return sb.toString();
  61.     }

  62.     /**
  63.      * POICellFormatterを取得する
  64.      * @return
  65.      */
  66.     public POICellFormatter getPoiCellFormatter() {
  67.         return poiCellFormatter;
  68.     }

  69.     /**
  70.      * POICellFormatterを設定する。
  71.      * @param poiCellFormatter
  72.      */
  73.     public void setPoiCellFormatter(POICellFormatter poiCellFormatter) {
  74.         this.poiCellFormatter = poiCellFormatter;
  75.     }

  76.     /**
  77.      * 値をキャッシュするかどうか。
  78.      * @return trueのときキャッシュする。
  79.      */
  80.     public boolean isCached() {
  81.         return cached;
  82.     }

  83.     /**
  84.      * 値をキャッシュするかどうか設定します。
  85.      * @param cached trueのときキャッシュする。
  86.      */
  87.     public void setCached(boolean cached) {
  88.         this.cached = cached;
  89.     }

  90.     /**
  91.      * キャッシュをクリアします。
  92.      */
  93.     public void clearCacheData() {
  94.         this.cacheData.clear();
  95.     }

  96.     @Override
  97.     public String getPattern(final Cell cell) {
  98.         return getPattern(cell, Locale.getDefault());
  99.     }

  100.     @Override
  101.     public String getPattern(final Cell cell, final Locale locale) {

  102.         final POICell poiCell = new POICell(cell);
  103.         final FormatterResolver formatterResolver = poiCellFormatter.getFormatterResolver();

  104.         final short formatIndex = poiCell.getFormatIndex();
  105.         final String formatPattern = poiCell.getFormatPattern();

  106.         if(formatterResolver.canResolve(formatIndex)) {
  107.             return formatterResolver.getFormatter(formatIndex).getPattern(locale);

  108.         } else if(formatterResolver.canResolve(formatPattern)) {
  109.             return formatterResolver.getFormatter(formatPattern).getPattern(locale);

  110.         } else {
  111.             return formatPattern;

  112.         }
  113.     }

  114. }