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