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

org.kiwiproject.validation.LongValueValidator Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.validation;

import static java.util.Objects.isNull;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Validates that a string value is a long value, i.e. that it can be converted to a long or {@link Long}.
 */
public class LongValueValidator implements ConstraintValidator {

    private LongValue longValue;

    @Override
    public void initialize(LongValue constraintAnnotation) {
        this.longValue = constraintAnnotation;
    }

    @Override
    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
        if (isNull(value)) {
            return longValue.allowNull();
        }

        try {
            Long.parseLong(value.toString());
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy