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 NumberMax<T extends Number & Comparable<T>> extends ValidationCellProcessor
22          implements LongCellProcessor, DoubleCellProcessor {
23      
24      private final T max;
25      
26      private final boolean inclusive;
27      
28      private final TextPrinter<T> printer; 
29      
30      public NumberMax(final T max, final boolean inclusive, final TextPrinter<T> printer) {
31          super();
32          checkPreconditions(max, printer);
33          this.max = max;
34          this.inclusive = inclusive;
35          this.printer = printer;
36      }
37      
38      public NumberMax(final T max, final boolean inclusive, final TextPrinter<T> printer, final CellProcessor next) {
39          super(next);
40          checkPreconditions(max, printer);
41          this.max = max;
42          this.inclusive = inclusive;
43          this.printer = printer;
44      }
45      
46      private static <T extends Number & Comparable<T>> void checkPreconditions(final T max, 
47              final TextPrinter<T> printer) {
48          if(max == null || printer == null) {
49              throw new NullPointerException("max and printer should not be null");
50          }
51          
52      }
53  
54      @SuppressWarnings("unchecked")
55      @Override
56      public Object execute(final Object value, final CsvContext context) {
57          
58          if(value == null) {
59              return next.execute(value, context);
60          }
61          
62          final Class<?> exepectedClass = getMax().getClass();
63          if(!exepectedClass.isAssignableFrom(value.getClass())) {
64              throw new SuperCsvCellProcessorException(exepectedClass, value, context, this);
65          }
66          
67          final T result = (T) value;
68          if(!validate(result)) {
69              throw createValidationException(context)
70                  .messageFormat("%s does not lie the max (%s) value.", 
71                          printValue(result), printValue(max))
72                  .rejectedValue(result)
73                  .messageVariables("max", getMax())
74                  .messageVariables("inclusive", isInclusive())
75                  .messageVariables("printer", getPrinter())
76                  .build();
77          }   
78          
79          return next.execute(result, context);
80      }
81      
82      private boolean validate(final T value) {
83          final int compared = value.compareTo(max);
84          if(compared < 0) {
85              return true;
86          }
87          
88          if(inclusive && compared == 0) {
89              return true;
90          }
91          
92          return false;
93          
94      }
95      
96      private String printValue(final T value) {
97          return getPrinter().print(value);
98      }
99      
100     /**
101      * 
102      * @return 設定された最大値を取得する
103      */
104     public T getMax() {
105         return max;
106     }
107     
108     /**
109      *  値を比較する際に指定した値を含むかどうか。
110      * @return
111      */
112     public boolean isInclusive() {
113         return inclusive;
114     }
115     
116     /**
117      * 
118      * @return 値のフォーマッタを取得する。
119      */
120     public TextPrinter<T> getPrinter() {
121         return printer;
122     }
123     
124 }