import org.jooq.lambda.tuple.Tuple2;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public class TimeUtils {
public static Boolean dateTimeInRange(LocalDateTime dateTime, Optional dateTimeFrom, Optional dateTimeTo, boolean includeHead, boolean includeLast) {
if (Objects.isNull(dateTime)) {
return false;
}
return dateTimeFrom.map(from -> includeHead ? !dateTime.isBefore(from) : dateTime.isAfter(from)).orElse(true) && dateTimeTo.map(to -> includeLast ? !dateTime.isAfter(to) : dateTime.isBefore(to)).orElse(true);
}
public static Boolean dateInRange(LocalDate date, Optional dateFrom, Optional dateTo, boolean includeHead, boolean includeLast) {
if (Objects.isNull(date)) {
return false;
}
return dateFrom.map(from -> includeHead ? !date.isBefore(from) : date.isAfter(from)).orElse(true) && dateTo.map(to -> includeLast ? !date.isAfter(to) : date.isBefore(to)).orElse(true);
}
public static Boolean timeInRange(LocalTime time, Optional timeFrom, Optional timeTo, boolean includeHead, boolean includeLast) {
if (Objects.isNull(time)) {
return false;
}
return timeFrom.map(from -> includeHead ? !time.isBefore(from) : time.isAfter(from)).orElse(true) && timeTo.map(to -> includeLast ? !time.isAfter(to) : time.isBefore(to)).orElse(true);
}
public static Boolean dateTimeInRange(LocalDateTime dateTime, Optional dateTimeFrom, Optional dateTimeTo) {
return dateTimeInRange(dateTime, dateTimeFrom, dateTimeTo, true, true);
}
public static Boolean dateInRange(LocalDate date, Optional dateFrom, Optional dateTo) {
return dateInRange(date, dateFrom, dateTo, true, true);
}
public static Boolean timeInRange(LocalTime time, Optional timeFrom, Optional timeTo) {
return timeInRange(time, timeFrom, timeTo, true, true);
}
public static Tuple2, Optional> dateTimeRange(List, Optional>> dateTimeList) {
if (dateTimeList.isEmpty()) {
throw new RuntimeException("dateTimeList is empty");
}
return dateTimeList.stream().reduce(new Tuple2<>(null, null), (z, e) -> {
Optional from0 = z.v1();
Optional to0 = z.v2();
final Optional from;
final Optional to;
if (Objects.nonNull(from0)) {
from = from0.flatMap(fromTime -> e.v1().flatMap(f -> fromTime.isBefore(f) ? Optional.of(fromTime) : Optional.of(f)));
}
else {
from = e.v1();
}
if (Objects.nonNull(to0)) {
to = to0.flatMap(toTime -> e.v2().flatMap(t -> toTime.isAfter(t) ? Optional.of(toTime) : Optional.of(t)));
}
else {
to = e.v2();
}
return new Tuple2<>(from, to);
}, (l, r) -> null);
}
public static Tuple2, Optional> dateRange(List, Optional>> dateList) {
if (dateList.isEmpty()) {
throw new RuntimeException("dateList is empty");
}
return dateList.stream().reduce(new Tuple2<>(null, null), (z, e) -> {
Optional from0 = z.v1();
Optional to0 = z.v2();
final Optional from;
final Optional to;
if (Objects.nonNull(from0)) {
from = from0.flatMap(fromTime -> e.v1().flatMap(f -> fromTime.isBefore(f) ? Optional.of(fromTime) : Optional.of(f)));
}
else {
from = e.v1();
}
if (Objects.nonNull(to0)) {
to = to0.flatMap(toTime -> e.v2().flatMap(t -> toTime.isAfter(t) ? Optional.of(toTime) : Optional.of(t)));
}
else {
to = e.v2();
}
return new Tuple2<>(from, to);
}, (l, r) -> null);
}
}