-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumericChecks.java
More file actions
259 lines (222 loc) · 11.2 KB
/
Copy pathNumericChecks.java
File metadata and controls
259 lines (222 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* BSD 2-Clause License
*
* Copyright (c) 2021, Ondrej Fischer
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package fluent.validation;
import fluent.validation.processor.Factory;
import java.math.BigDecimal;
import java.math.BigInteger;
import static fluent.validation.BasicChecks.*;
import static fluent.validation.Transformation.dontTransformNull;
import static java.lang.Math.abs;
/**
* Factory of ready to use most frequent checks used for validation of numbers.
* The main groups are:
* 1. Validation of floating point numbers, where FP precision needs to be taken into account
* 2. Validation of invalid values (NaN)
* 3. Validation of strings containing numbers using respective parse* methods.
*/
@Factory
public final class NumericChecks {
private static final Double DEFAULT_TOLERANCE = Double.parseDouble(System.getProperty("check.default.tolerance", "0.000001"));
private NumericChecks() {}
/* ------------------------------------------------------------------------------------------------------
* Numeric conditions.
* ------------------------------------------------------------------------------------------------------
*/
/**
* Check, that a double number is close enough to the expected value, given the explicit tolerance.
* This is the standard approach to validation of floating point number representations, because due to
* the internal representation in the CPU/FPU, numbers coming from e.g. different calculation, but which
* would have the same real value, can differ in the CPU/FPU representation.
* Simplest example could be: 1/3 vs. (1 - 2/3). Results of these formulas will differ in computer representation,
* although in real math they are equal.
*
* @param expectedValue Expected value.
* @param precision Tolerance, which is still accepted as difference from the expected value.
* @return Check of the double value.
*/
public static Check<Double> closeTo(double expectedValue, double precision) {
return check(data -> abs(expectedValue - data) < precision, "<" + expectedValue + " ±" + precision + ">");
}
/**
* Check, that a float number is close enough to the expected value, given the explicit tolerance.
* This is the standard approach to validation of floating point number representations, because due to
* the internal representation in the CPU/FPU, numbers coming from e.g. different calculation, but which
* would have the same real value, can differ in the CPU/FPU representation.
* Simplest example could be: 1/3 vs. (1 - 2/3). Results of these formulas will differ in computer representation,
* although in real math they are equal.
*
* @param expectedValue Expected value.
* @param precision Tolerance, which is still accepted as difference from the expected value.
* @return Check of the double value.
*/
public static Check<Float> closeTo(float expectedValue, float precision) {
return check(data -> abs(expectedValue - data) < precision, expectedValue + " ±" + precision);
}
/**
* Check, that a BigDecimal number is close enough to the expected value, given the explicit tolerance.
* This is the standard approach to validation of floating point number representations, because due to
* the internal representation in the CPU/FPU, numbers coming from e.g. different calculation, but which
* would have the same real value, can differ in the CPU/FPU representation.
* Simplest example could be: 1/3 vs. (1 - 2/3). Results of these formulas will differ in computer representation,
* although in real math they are equal.
*
* @param expectedValue Expected value.
* @param precision Tolerance, which is still accepted as difference from the expected value.
* @return Check of the double value.
*/
public static Check<BigDecimal> closeTo(BigDecimal expectedValue, BigDecimal precision) {
return check(data -> expectedValue.subtract(data).abs().compareTo(precision) < 0, expectedValue + " ±" + precision);
}
/**
* Specific overloaded method for "tolerant" equalTo implementation for Double floating point values.
* It's implemented using closeTo() with default tolerance, which is by default 0.000001, but can be specified
* via system property to different value.
*
* @param expectedValue Expected value, from which the actual shouldn't differ more, than by the default tolerance.
* @return Check of the double equality.
* @see #closeTo(double, double)
* @see #DEFAULT_TOLERANCE
*/
public static Check<Double> equalTo(Double expectedValue) {
return expectedValue == null ? sameInstance(null) : closeTo(expectedValue, DEFAULT_TOLERANCE);
}
/**
* Specific overloaded method for "tolerant" equalTo implementation for Float floating point values.
* It's implemented using closeTo() with default tolerance, which is by default 0.000001, but can be specified
* via system property to different value.
*
* @param expectedValue Expected value, from which the actual shouldn't differ more, than by the default tolerance.
* @return Check of the float equality.
* @see #closeTo(float, float)
* @see #DEFAULT_TOLERANCE
*/
public static Check<Float> equalTo(Float expectedValue) {
return expectedValue == null ? sameInstance(null) : closeTo(expectedValue, DEFAULT_TOLERANCE.floatValue());
}
/**
* Specific overloaded method for "tolerant" equalTo implementation for BigDecimal real number values.
* It's implemented using closeTo() with default tolerance, which is by default 0.000001, but can be specified
* via system property to different value.
*
* @param expectedValue Expected value, from which the actual shouldn't differ more, than by the default tolerance.
* @return Check of the BigDecimal equality.
* @see #closeTo(BigDecimal, BigDecimal)
* @see #DEFAULT_TOLERANCE
*/
public static Check<BigDecimal> equalTo(BigDecimal expectedValue) {
return expectedValue == null ? sameInstance(null) : closeTo(expectedValue, BigDecimal.valueOf(DEFAULT_TOLERANCE));
}
/**
* Check of the string containing double number implemented by composition of parsing double from string and
* application of check of the Double value.
*
* @param check Check of the double value.
* @return Check applicable on String.
*/
public static Check<String> parseDouble(Check<? super Double> check) {
return compose(dontTransformNull(Double::parseDouble), check);
}
/**
* Check of the string containing double number implemented by composition of parsing double from string and
* expected Double value.
*
* @param expectedValue Expected double value.
* @return Check applicable on String.
*/
public static Check<String> parseDouble(Double expectedValue) {
return parseDouble(equalTo(expectedValue));
}
/**
* Check of the string containing double number implemented by composition of parsing double from string and
* expected Double value.
*
* @param expectedValue Expected double value.
* @return Check applicable on String.
*/
public static Check<String> parseDouble(double expectedValue) {
return parseDouble(equalTo(expectedValue));
}
public static Check<String> parseFloat(Check<? super Float> check) {
return compose(dontTransformNull(Float::parseFloat), check);
}
public static Check<String> parseFloat(Float expectedValue) {
return parseFloat(equalTo(expectedValue));
}
public static Check<String> parseFloat(float expectedValue) {
return parseFloat(equalTo(expectedValue));
}
public static Check<String> parseInt(Check<? super Integer> check) {
return compose(dontTransformNull(Integer::parseInt), check);
}
public static Check<String> parseInt(Integer expectedValue) {
return parseInt(BasicChecks.equalTo(expectedValue));
}
public static Check<String> parseInt(int expectedValue) {
return parseInt(BasicChecks.equalTo(expectedValue));
}
public static Check<String> parseLong(Check<? super Long> check) {
return compose(dontTransformNull(Long::parseLong), check);
}
public static Check<String> parseLong(Long expectedValue) {
return parseLong(BasicChecks.equalTo(expectedValue));
}
public static Check<String> parseLong(long expectedValue) {
return parseLong(BasicChecks.equalTo(expectedValue));
}
public static Check<String> parseShort(Check<? super Short> check) {
return compose(dontTransformNull(Short::parseShort), check);
}
public static Check<String> parseShort(Short expectedValue) {
return parseShort(BasicChecks.equalTo(expectedValue));
}
public static Check<String> parseShort(short expectedValue) {
return parseShort(BasicChecks.equalTo(expectedValue));
}
public static Check<String> parseBigDecimal(Check<? super BigDecimal> check) {
return compose(dontTransformNull(BigDecimal::new), check);
}
public static Check<String> parseBigInt(Check<? super BigInteger> check) {
return compose(dontTransformNull(BigInteger::new), check);
}
public static Check<String> parseByte(Check<? super Byte> check) {
return compose(dontTransformNull(Byte::parseByte), check);
}
public static Check<String> parseByte(Byte expectedValue) {
return parseByte(BasicChecks.equalTo(expectedValue));
}
public static Check<String> parseByte(byte expectedValue) {
return parseByte(BasicChecks.equalTo(expectedValue));
}
public static Check<String> parseBoolean(Check<? super Boolean> check) {
return compose(dontTransformNull(Boolean::parseBoolean), check);
}
public static Check<String> parseBoolean(Boolean expectedValue) {
return parseBoolean(BasicChecks.equalTo(expectedValue));
}
}