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.LongCellProcessor;
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 LMinMax extends CellProcessorAdaptor implements StringCellProcessor {
34
35
36 public static final long MAX_LONG = Long.MAX_VALUE;
37
38
39 public static final long MIN_LONG = Long.MIN_VALUE;
40
41
42 public static final int MAX_INTEGER = Integer.MAX_VALUE;
43
44
45 public static final int MIN_INTEGER = Integer.MIN_VALUE;
46
47
48 public static final short MAX_SHORT = Short.MAX_VALUE;
49
50
51 public static final short MIN_SHORT = Short.MIN_VALUE;
52
53
54 public static final int MAX_CHAR = Character.MAX_VALUE;
55
56
57 public static final int MIN_CHAR = Character.MIN_VALUE;
58
59
60 public static final int MAX_8_BIT_UNSIGNED = 255;
61
62
63 public static final int MIN_8_BIT_UNSIGNED = 0;
64
65
66 public static final int MAX_8_BIT_SIGNED = Byte.MAX_VALUE;
67
68
69 public static final int MIN_8_BIT_SIGNED = Byte.MIN_VALUE;
70
71 private final long min;
72
73 private final long max;
74
75
76
77
78
79
80
81
82
83
84
85
86 public LMinMax(final long min, final long max) {
87 super();
88 checkPreconditions(min, max);
89 this.min = min;
90 this.max = max;
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public LMinMax(final long min, final long max, final LongCellProcessor next) {
109 super(next);
110 checkPreconditions(min, max);
111 this.min = min;
112 this.max = max;
113 }
114
115
116
117
118
119
120
121
122
123
124
125 private static void checkPreconditions(final long min, final long max) {
126 if( max < min ) {
127 throw new IllegalArgumentException(String.format("max (%d) should not be < min (%d)", max, min));
128 }
129 }
130
131
132
133
134
135
136
137
138
139 public Object execute(final Object value, final CsvContext context) {
140 validateInputNotNull(value, context);
141
142 final Long result;
143 if( value instanceof Long ) {
144 result = (Long) value;
145 } else {
146 try {
147 result = Long.parseLong(value.toString());
148 }
149 catch(final NumberFormatException e) {
150 throw new SuperCsvCellProcessorException(String.format("'%s' could not be parsed as a Long", value),
151 context, this, e);
152 }
153 }
154
155 if( result < min || result > max ) {
156 throw new SuperCsvConstraintViolationException(String.format(
157 "%d does not lie between the min (%d) and max (%d) values (inclusive)", result, min, max), context,
158 this);
159 }
160
161 return next.execute(result, context);
162 }
163
164 }