View Javadoc
1   package com.github.mygreen.supercsv.cellprocessor.constraint;
2   
3   import org.supercsv.cellprocessor.ift.CellProcessor;
4   import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
5   import org.supercsv.cellprocessor.ift.LongCellProcessor;
6   import org.supercsv.exception.SuperCsvCellProcessorException;
7   import org.supercsv.util.CsvContext;
8   
9   import com.github.mygreen.supercsv.cellprocessor.ValidationCellProcessor;
10  import com.github.mygreen.supercsv.cellprocessor.format.TextPrinter;
11  
12  
13  /**
14   * 数値が指定した値の範囲内かどうか検証するCellProcessor.
15   * 
16   * @version 2.0
17   * @param <T> inherit number object. ex, int, Integer, double, Double,...
18   * @author T.TSUCHIE
19   *
20   */
21  public class NumberRange<T extends Number & Comparable<T>> extends ValidationCellProcessor
22          implements LongCellProcessor, DoubleCellProcessor {
23      
24      private final T min;
25      
26      private final T max;
27      
28      private final boolean inclusive;
29      
30      private final TextPrinter<T> printer; 
31      
32      public NumberRange(final T min, final T max, final boolean inclusive, final TextPrinter<T> printer) {
33          super();
34          checkPreconditions(min, max, printer);
35          this.min = min;
36          this.max = max;
37          this.inclusive = inclusive;
38          this.printer = printer;
39      }
40      
41      public NumberRange(final T min, final T max, final boolean inclusive, final TextPrinter<T> printer, final CellProcessor next) {
42          super(next);
43          checkPreconditions(min, max, printer);
44          this.min = min;
45          this.max = max;
46          this.inclusive = inclusive;
47          this.printer = printer;
48      }
49      
50      private static <T extends Number & Comparable<T>> void checkPreconditions(final T min, final T max, 
51              final TextPrinter<T> printer) {
52          if(min == null || max == null || printer == null) {
53              throw new NullPointerException("min ,max and printer should not be null");
54          }
55          
56          if(min.compareTo(max) > 0) {
57              throw new IllegalArgumentException(String.format("max (%s) should not be < min (%s)", max, min));
58          }
59      }
60  
61      @SuppressWarnings("unchecked")
62      @Override
63      public Object execute(final Object value, final CsvContext context) {
64          
65          if(value == null) {
66              return next.execute(value, context);
67          }
68          
69          final Class<?> exepectedClass = getMin().getClass();
70          if(!exepectedClass.isAssignableFrom(value.getClass())) {
71              throw new SuperCsvCellProcessorException(exepectedClass, value, context, this);
72          }
73          
74          final T result = ((T) value);
75          if(!validate(result)) {
76              throw createValidationException(context)
77                  .messageFormat("%s does not lie between the min (%s) and max (%s) value.", 
78                          printValue(result), printValue(min), printValue(max))
79                  .rejectedValue(result)
80                  .messageVariables("min", getMin())
81                  .messageVariables("max", getMax())
82                  .messageVariables("inclusive", isInclusive())
83                  .messageVariables("printer", getPrinter())
84                  .build();
85          }   
86          
87          return next.execute(result, context);
88      }
89      
90      private boolean validate(final T value) {
91          final int comparedMin = value.compareTo(min);
92          final int comparedMax = value.compareTo(max);
93          
94          if(comparedMin > 0 && comparedMax < 0) {
95              return true;
96          }
97          
98          if(inclusive && (comparedMin == 0 || comparedMax == 0)) {
99              return true;
100         }
101         
102         return false;
103         
104     }
105     
106     private String printValue(final T value) {
107         return getPrinter().print(value);
108     }
109     
110     /**
111      * 
112      * @return 設定された最小値を取得する
113      */
114     public T getMin() {
115         return min;
116     }
117     
118     /**
119      * 
120      * @return 設定された最大値を取得する
121      */
122     public T getMax() {
123         return max;
124     }
125     
126     /**
127      *  値を比較する際に指定した値を含むかどうか。
128      * @return
129      */
130     public boolean isInclusive() {
131         return inclusive;
132     }
133     
134     /**
135      * 
136      * @return フォーマッタを取得する
137      */
138     public TextPrinter<T> getPrinter() {
139         return printer;
140     }
141     
142 }