LengthBetween.java

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

  2. import org.supercsv.cellprocessor.ift.CellProcessor;
  3. import org.supercsv.cellprocessor.ift.StringCellProcessor;
  4. import org.supercsv.exception.SuperCsvCellProcessorException;
  5. import org.supercsv.exception.SuperCsvConstraintViolationException;
  6. import org.supercsv.util.CsvContext;

  7. import com.github.mygreen.supercsv.cellprocessor.ValidationCellProcessor;


  8. /**
  9.  * 文字列長が範囲であるか検証するCellProcessor.
  10.  *
  11.  * @version 2.0
  12.  * @author T.TSUCHIE
  13.  *
  14.  */
  15. public class LengthBetween extends ValidationCellProcessor implements StringCellProcessor {
  16.    
  17.     private final int min;
  18.    
  19.     private final int max;
  20.    
  21.     public LengthBetween(final int min, final int max) {
  22.         super();
  23.         checkPreconditions(min, max);
  24.         this.min = min;
  25.         this.max = max;
  26.     }
  27.    
  28.     public LengthBetween(final int min, final int max, final CellProcessor next) {
  29.         super(next);
  30.         checkPreconditions(min, max);
  31.         this.min = min;
  32.         this.max = max;
  33.     }
  34.    
  35.     /**
  36.      * Checks the preconditions for creating a new {@link LengthBetween} processor.
  37.      *
  38.      * @param min
  39.      *            the minimum String length
  40.      * @param max
  41.      *            the maximum String length
  42.      * @throws IllegalArgumentException
  43.      *             {@literal if max < min, or min is < 0}
  44.      */
  45.     private static void checkPreconditions(final int min, final int max) {
  46.         if( min > max ) {
  47.             throw new IllegalArgumentException(String.format("max (%d) should not be < min (%d)", max, min));
  48.         }
  49.        
  50.         if( min < 0 ) {
  51.             throw new IllegalArgumentException(String.format("min length (%d) should not be < 0", min));
  52.         }
  53.     }
  54.    
  55.     /**
  56.      * {@inheritDoc}
  57.      *
  58.      * @throws SuperCsvCellProcessorException
  59.      *             {@literal if value is null}
  60.      * @throws SuperCsvConstraintViolationException
  61.      *             {@literal if length is < min or length > max}
  62.      */
  63.     @SuppressWarnings("unchecked")
  64.     public Object execute(final Object value, final CsvContext context) {
  65.        
  66.         if(value == null) {
  67.             return next.execute(value, context);
  68.         }
  69.        
  70.         final String stringValue = value.toString();
  71.         final int length = stringValue.length();
  72.         if( length < min || length > max ) {
  73.             throw createValidationException(context)
  74.                 .messageFormat("the length (%d) of value '%s' does not lie between the min (%d) and max (%d) values (inclusive)",
  75.                         length, stringValue, min, max)
  76.                 .rejectedValue(stringValue)
  77.                 .messageVariables("min", getMin())
  78.                 .messageVariables("max", getMax())
  79.                 .messageVariables("length", length)
  80.                 .build();
  81.                
  82.         }
  83.        
  84.         return next.execute(stringValue, context);
  85.     }
  86.    
  87.     /**
  88.      *
  89.      * @return 設定された最小文字長を取得する
  90.      */
  91.     public int getMin() {
  92.         return min;
  93.     }
  94.    
  95.     /**
  96.      *
  97.      * @return 設定された最大文字長を取得する
  98.      */
  99.     public int getMax() {
  100.         return max;
  101.     }

  102. }