
com.fengwenyi.erwin.validation.validator.TimestampValidator Maven / Gradle / Ivy
The newest version!
package com.fengwenyi.erwin.validation.validator;
import com.fengwenyi.erwin.validation.constraints.ValidTimestamp;
import com.fengwenyi.javalib.convert.DateTimeUtils;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.time.LocalDateTime;
import java.util.Objects;
/**
* @author Erwin Feng
* @since 2022-07-08
*/
public class TimestampValidator implements ConstraintValidator {
private boolean past = false;
private boolean future = false;
private boolean pastOrPresent = false;
private boolean futureOrPresent = false;
@Override
public void initialize(ValidTimestamp constraintAnnotation) {
past = constraintAnnotation.past();
future = constraintAnnotation.future();
pastOrPresent = constraintAnnotation.pastOrPresent();
futureOrPresent = constraintAnnotation.futureOrPresent();
}
@Override
public boolean isValid(Long value, ConstraintValidatorContext constraintValidatorContext) {
if (Objects.isNull(value)) {
return true;
}
long nowTimestamp = DateTimeUtils.toMillisecond(LocalDateTime.now());
if (past) {
return value < nowTimestamp;
} else if (future) {
return value > nowTimestamp;
} else if (pastOrPresent) {
return value <= nowTimestamp;
} else if (futureOrPresent) {
return value >= nowTimestamp;
}
return false;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy