All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.validation.MinDurationValidator Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
package io.dropwizard.validation;

import io.dropwizard.util.Duration;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.concurrent.TimeUnit;

/**
 * Check that a {@link Duration} being validated is greater than or equal to the
 * minimum value specified.
 */
public class MinDurationValidator implements ConstraintValidator {

    private long minQty;
    private TimeUnit minUnit;

    @Override
    public void initialize(MinDuration constraintAnnotation) {
        this.minQty = constraintAnnotation.value();
        this.minUnit = constraintAnnotation.unit();
    }

    @Override
    public boolean isValid(Duration value, ConstraintValidatorContext context) {
        return (value == null) || (value.toNanoseconds() >= minUnit.toNanos(minQty));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy