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

play.data.validation.MinCheck Maven / Gradle / Ivy

There is a newer version: 2.6.3
Show newest version
package play.data.validation;

import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.OValContext;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("serial")
public class MinCheck extends AbstractAnnotationCheck {

    static final String mes = "validation.min";

    double min;

    @Override
    public void configure(Min min) {
        this.min = min.value();
        setMessage(min.message());
    }

    @Override
    public boolean isSatisfied(Object validatedObject, Object value, OValContext context, Validator validator) {
        requireMessageVariablesRecreation();
        if (value == null) {
            return true;
        }
        if (value instanceof String) {
            try {
                return Double.parseDouble(value.toString()) >= min;
            } catch (Exception e) {
                return false;
            }
        }
        if (value instanceof Number) {
            try {
                return ((Number) value).doubleValue() >= min;
            } catch (Exception e) {
                return false;
            }
        }
        return false;
    }

    @Override
    public Map createMessageVariables() {
        Map messageVariables = new HashMap<>();
        messageVariables.put("min", Double.toString(min));
        return messageVariables;
    }
   
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy