1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.cellprocessor.constraint;
17
18 import org.supercsv.cellprocessor.CellProcessorAdaptor;
19 import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
20 import org.supercsv.cellprocessor.ift.StringCellProcessor;
21 import org.supercsv.exception.SuperCsvCellProcessorException;
22 import org.supercsv.exception.SuperCsvConstraintViolationException;
23 import org.supercsv.util.CsvContext;
24
25
26
27
28
29
30
31
32
33 public class DMinMax extends CellProcessorAdaptor implements StringCellProcessor {
34
35
36 public static final double MAX_DOUBLE = Double.MAX_VALUE;
37
38
39 public static final double MIN_DOUBLE = Double.MIN_VALUE;
40
41
42 public static final double MAX_SHORT = Short.MAX_VALUE;
43
44
45 public static final double MIN_SHORT = Short.MIN_VALUE;
46
47
48 public static final double MAX_CHAR = Character.MAX_VALUE;
49
50
51 public static final double MIN_CHAR = Character.MIN_VALUE;
52
53
54 public static final int MAX_8_BIT_UNSIGNED = 255;
55
56
57 public static final int MIN_8_BIT_UNSIGNED = 0;
58
59
60 public static final int MAX_8_BIT_SIGNED = Byte.MAX_VALUE;
61
62
63 public static final int MIN_8_BIT_SIGNED = Byte.MIN_VALUE;
64
65 private final double min;
66
67 private final double max;
68
69
70
71
72
73
74
75
76
77
78
79
80 public DMinMax(final double min, final double max) {
81 super();
82 checkPreconditions(min, max);
83 this.min = min;
84 this.max = max;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public DMinMax(final double min, final double max, final DoubleCellProcessor next) {
103 super(next);
104 checkPreconditions(min, max);
105 this.min = min;
106 this.max = max;
107 }
108
109
110
111
112
113
114
115
116
117
118
119 private static void checkPreconditions(final double min, final double max) {
120 if( max < min ) {
121 throw new IllegalArgumentException(String.format("max (%f) should not be < min (%f)", max, min));
122 }
123 }
124
125
126
127
128
129
130
131
132
133 public Object execute(final Object value, final CsvContext context) {
134 validateInputNotNull(value, context);
135
136 final Double result;
137 if( value instanceof Double ) {
138 result = (Double) value;
139 } else {
140 try {
141 result = Double.parseDouble(value.toString());
142 }
143 catch(final NumberFormatException e) {
144 throw new SuperCsvCellProcessorException(String.format("'%s' could not be parsed as a Double", value),
145 context, this, e);
146 }
147 }
148
149 if( result < min || result > max ) {
150 throw new SuperCsvConstraintViolationException(String.format(
151 "%f does not lie between the min (%f) and max (%f) values (inclusive)", result, min, max), context,
152 this);
153 }
154
155 return next.execute(result, context);
156 }
157
158 }