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