-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiggerThanValueSource.java
More file actions
118 lines (95 loc) · 4.24 KB
/
Copy pathBiggerThanValueSource.java
File metadata and controls
118 lines (95 loc) · 4.24 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
package it.aboutbits.springboot.testing.validation.source;
import it.aboutbits.springboot.testing.validation.core.ValueSource;
import it.aboutbits.springboot.toolbox.type.ScaledBigDecimal;
import lombok.NonNull;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Stream;
public class BiggerThanValueSource implements ValueSource {
private static final Map<Class<?>, Function<Object[], Stream<?>>> TYPE_SOURCES = new HashMap<>();
private static final Random RANDOM = new Random();
static {
TYPE_SOURCES.put(Integer.class, BiggerThanValueSource::getIntegerStream);
TYPE_SOURCES.put(int.class, BiggerThanValueSource::getIntegerStream);
TYPE_SOURCES.put(Long.class, BiggerThanValueSource::getLongStream);
TYPE_SOURCES.put(long.class, BiggerThanValueSource::getLongStream);
TYPE_SOURCES.put(Float.class, BiggerThanValueSource::getFloatStream);
TYPE_SOURCES.put(float.class, BiggerThanValueSource::getFloatStream);
TYPE_SOURCES.put(Double.class, BiggerThanValueSource::getDoubleStream);
TYPE_SOURCES.put(double.class, BiggerThanValueSource::getDoubleStream);
TYPE_SOURCES.put(BigDecimal.class, BiggerThanValueSource::getBigDecimalStream);
TYPE_SOURCES.put(ScaledBigDecimal.class, BiggerThanValueSource::getScaledBigDecimalStream);
}
@SuppressWarnings("unchecked")
public static void registerType(Class<?> type, Function<Object[], Stream<?>> source) {
TYPE_SOURCES.put(type, source);
}
@Override
@SuppressWarnings("unchecked")
public <T> Stream<T> values(Class<T> propertyClass, Object... args) {
var sourceFunction = TYPE_SOURCES.get(propertyClass);
if (sourceFunction != null) {
return (Stream<T>) sourceFunction.apply(args);
}
throw new IllegalArgumentException("Property class not supported!");
}
@NonNull
private static Stream<Integer> getIntegerStream(Object[] args) {
var minValue = Long.valueOf((long) args[0]).intValue() + 1;
var maxValue = Integer.MAX_VALUE;
return Stream.concat(
Stream.of(minValue, maxValue),
RANDOM.ints(minValue, maxValue).limit(1).boxed()
);
}
@NonNull
private static Stream<Long> getLongStream(Object[] args) {
var minValue = (long) args[0] + 1;
var maxValue = Long.MAX_VALUE;
return Stream.concat(
Stream.of(minValue, maxValue),
RANDOM.longs(minValue, maxValue).limit(1).boxed()
);
}
@NonNull
private static Stream<Float> getFloatStream(Object[] args) {
var minValue = Long.valueOf((long) args[0]).floatValue() + 0.1f;
var maxValue = Float.MAX_VALUE;
return Stream.concat(
Stream.of(minValue, maxValue),
RANDOM.doubles(minValue, maxValue).limit(1).boxed().map(
Double::floatValue
)
);
}
@NonNull
private static Stream<Double> getDoubleStream(Object[] args) {
var minValue = Long.valueOf((long) args[0]).doubleValue() + 0.1d;
var maxValue = Double.MAX_VALUE;
return Stream.concat(
Stream.of(minValue, maxValue),
RANDOM.doubles(minValue, maxValue).limit(1).boxed()
);
}
@NonNull
private static Stream<ScaledBigDecimal> getScaledBigDecimalStream(Object[] args) {
var minValue = Long.valueOf((long) args[0]).doubleValue() + 0.1d;
var maxValue = Double.MAX_VALUE;
return Stream.concat(
Stream.of(ScaledBigDecimal.valueOf(minValue), ScaledBigDecimal.valueOf(maxValue)),
RANDOM.doubles(minValue, maxValue).limit(1).boxed().map(ScaledBigDecimal::valueOf)
);
}
@NonNull
private static Stream<BigDecimal> getBigDecimalStream(Object[] args) {
var minValue = Long.valueOf((long) args[0]).doubleValue() + 0.1d;
var maxValue = Double.MAX_VALUE;
return Stream.concat(
Stream.of(BigDecimal.valueOf(minValue), BigDecimal.valueOf(maxValue)),
RANDOM.doubles(minValue, maxValue).limit(1).boxed().map(BigDecimal::valueOf)
);
}
}