org.hibernate.validator.internal.constraintvalidators.hv.time.DurationMinValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bean-validator Show documentation
Show all versions of bean-validator Show documentation
JSR 380's RI, Hibernate Validator version ${hibernate-validator.version} and its dependencies repackaged as OSGi bundle
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or .
*/
package org.hibernate.validator.internal.constraintvalidators.hv.time;
import java.time.Duration;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.hibernate.validator.constraints.time.DurationMin;
/**
* Checks that a validated {@link Duration} length is longer than or equal to the
* one specified with the annotation.
*
* @author Marko Bekhta
*/
public class DurationMinValidator implements ConstraintValidator {
private Duration minDuration;
private boolean inclusive;
@Override
public void initialize(DurationMin constraintAnnotation) {
this.minDuration = Duration.ofNanos( constraintAnnotation.nanos() )
.plusMillis( constraintAnnotation.millis() )
.plusSeconds( constraintAnnotation.seconds() )
.plusMinutes( constraintAnnotation.minutes() )
.plusHours( constraintAnnotation.hours() )
.plusDays( constraintAnnotation.days() );
this.inclusive = constraintAnnotation.inclusive();
}
@Override
public boolean isValid(Duration value, ConstraintValidatorContext context) {
// null values are valid
if ( value == null ) {
return true;
}
int comparisonResult = minDuration.compareTo( value );
return inclusive ? comparisonResult <= 0 : comparisonResult < 0;
}
}