-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureValueSource.java
More file actions
118 lines (93 loc) · 4.55 KB
/
FutureValueSource.java
File metadata and controls
118 lines (93 loc) · 4.55 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 org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
@NullMarked
public class FutureValueSource implements ValueSource {
private static final Map<Class<?>, Function<Object[], Stream<?>>> TYPE_SOURCES = new HashMap<>();
// https://jakarta.ee/specifications/bean-validation/3.0/apidocs/jakarta/validation/constraints/future
static {
TYPE_SOURCES.put(Instant.class, FutureValueSource::getInstantStream);
TYPE_SOURCES.put(LocalTime.class, FutureValueSource::getLocalTimeStream);
TYPE_SOURCES.put(LocalDate.class, FutureValueSource::getLocalDateStream);
TYPE_SOURCES.put(LocalDateTime.class, FutureValueSource::getLocalDateTimeStream);
TYPE_SOURCES.put(OffsetTime.class, FutureValueSource::getOffsetTimeStream);
TYPE_SOURCES.put(OffsetDateTime.class, FutureValueSource::getOffsetDateTimeStream);
TYPE_SOURCES.put(Year.class, FutureValueSource::getYearStream);
TYPE_SOURCES.put(YearMonth.class, FutureValueSource::getYearMonthStream);
TYPE_SOURCES.put(ZonedDateTime.class, FutureValueSource::getZonedDateTimeStream);
}
@SuppressWarnings("unused")
public static void registerType(Class<?> type, Function<Object[], Stream<?>> source) {
TYPE_SOURCES.put(type, source);
}
@Override
@SuppressWarnings("unchecked")
public <T> Stream<@Nullable T> values(Class<T> propertyClass, Object... args) {
var sourceFunction = TYPE_SOURCES.get(propertyClass);
if (sourceFunction != null) {
return (Stream<@Nullable T>) sourceFunction.apply(args);
}
throw new IllegalArgumentException("Property class not supported!");
}
private static Stream<Instant> getInstantStream(Object[] args) {
var currentInstant = Instant.now();
var largestInstant = Instant.MAX;
return Stream.of(currentInstant.plusSeconds(1L), largestInstant);
}
private static Stream<LocalTime> getLocalTimeStream(Object[] args) {
var currentLocalTime = LocalTime.now();
var largestLocalTime = LocalTime.MAX;
return Stream.of(currentLocalTime.plusSeconds(1L), largestLocalTime);
}
private static Stream<LocalDate> getLocalDateStream(Object[] args) {
var currentDate = LocalDate.now();
var largestDate = LocalDate.MAX;
return Stream.of(currentDate.plusDays(1L), largestDate);
}
private static Stream<LocalDateTime> getLocalDateTimeStream(Object[] args) {
var currentDateTime = LocalDateTime.now();
var largestDateTime = LocalDateTime.MAX;
return Stream.of(currentDateTime.plusSeconds(1L), largestDateTime);
}
private static Stream<OffsetTime> getOffsetTimeStream(Object[] args) {
var currentOffsetTime = OffsetTime.now(ZoneOffset.UTC);
var largestOffsetTime = OffsetTime.MAX;
return Stream.of(currentOffsetTime.plusSeconds(1L), largestOffsetTime);
}
private static Stream<OffsetDateTime> getOffsetDateTimeStream(Object[] args) {
var currentOffsetDateTime = OffsetDateTime.now(ZoneOffset.UTC);
var largestOffsetDateTime = OffsetDateTime.MAX;
return Stream.of(currentOffsetDateTime.plusSeconds(1L), largestOffsetDateTime);
}
private static Stream<Year> getYearStream(Object[] args) {
var currentYear = Year.now();
var largestYear = Year.of(Year.MAX_VALUE);
return Stream.of(currentYear.plusYears(1L), largestYear);
}
private static Stream<YearMonth> getYearMonthStream(Object[] args) {
var currentYearMonth = YearMonth.now();
var largestYearMonth = YearMonth.of(Year.MAX_VALUE, Month.DECEMBER);
return Stream.of(currentYearMonth.plusMonths(1L), largestYearMonth);
}
private static Stream<ZonedDateTime> getZonedDateTimeStream(Object[] args) {
var currentZonedDateTime = ZonedDateTime.now(ZoneOffset.UTC);
var largestZonedDateTime = LocalDateTime.MAX.atZone(ZoneOffset.MIN);
return Stream.of(currentZonedDateTime.plusSeconds(1L), largestZonedDateTime);
}
}