<commit_before>/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.

 * Author: <NAME>
 * Created: 06.05.2009 15:00:15
 *
 * $Id$
 */
package com.haulmont.cuba.gui.components.validators;

import com.haulmont.chile.core.datatypes.Datatype;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.datatypes.impl.DoubleDatatype;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.core.global.MessageUtils;
import com.haulmont.cuba.core.global.UserSessionProvider;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.Field;
import com.haulmont.cuba.gui.components.ValidationException;
import org.dom4j.Element;

import java.math.BigDecimal;
import java.text.ParseException;

public class DoubleValidator implements Field.Validator {

    protected String message;
    protected String messagesPack;

    private static final long serialVersionUID = 7129516061104979525L;

    public DoubleValidator(Element element, String messagesPack) {
        message = element.attributeValue("message");
        this.messagesPack = messagesPack;
    }

    public DoubleValidator(String message) {
        this.message = message;
    }

    public DoubleValidator() {
        this.message = MessageProvider.getMessage(AppConfig.getMessagesPack(), "validation.invalidNumber");
    }

    public void validate(Object value) throws ValidationException {
        boolean result;
        if (value instanceof String) {
            try {
                Datatype<Double> datatype = Datatypes.get(DoubleDatatype.NAME);
                datatype.parse((String) value, UserSessionProvider.getLocale());
                result = true;
            } catch (ParseException e) {
                result = false;
            }
        } else {
            result = (value instanceof Double) || (value instanceof BigDecimal);
        }

        if (!result) {
            String msg = message != null ? MessageUtils.loadString(messagesPack, message) : "Invalid value '%s'";
            throw new ValidationException(String.format(msg, value));
        }
    }
}<commit_msg>DoubleValudator - support "onlyPositive" tag<commit_after>/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.

 * Author: <NAME>
 * Created: 06.05.2009 15:00:15
 *
 * $Id$
 */
package com.haulmont.cuba.gui.components.validators;

import com.haulmont.chile.core.datatypes.Datatype;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.datatypes.impl.DoubleDatatype;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.core.global.MessageUtils;
import com.haulmont.cuba.core.global.UserSessionProvider;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.Field;
import com.haulmont.cuba.gui.components.ValidationException;
import org.apache.commons.lang.ObjectUtils;
import org.dom4j.Element;

import java.math.BigDecimal;
import java.text.ParseException;

public class DoubleValidator implements Field.Validator {

    protected String message;
    protected String messagesPack;
    protected String onlyPositive;

    private static final long serialVersionUID = 7129516061104979525L;

    public DoubleValidator(Element element, String messagesPack) {
        message = element.attributeValue("message");
        onlyPositive = element.attributeValue("onlyPositive");
        this.messagesPack = messagesPack;
    }

    public DoubleValidator(String message) {
        this.message = message;
    }

    public DoubleValidator() {
        this.message = MessageProvider.getMessage(AppConfig.getMessagesPack(), "validation.invalidNumber");
    }

    private boolean checkDoubleOnPositive(Double value) {
        return !ObjectUtils.equals("true", onlyPositive) || value >= 0;
    }

    private boolean checkBigDecimalOnPositive(BigDecimal value) {
        return !ObjectUtils.equals("true", onlyPositive) || value.compareTo(BigDecimal.ZERO) >= 0;
    }

    public void validate(Object value) throws ValidationException {
        boolean result;
        if (value instanceof String) {
            try {
                Datatype<Double> datatype = Datatypes.get(DoubleDatatype.NAME);
                Double num = datatype.parse((String) value, UserSessionProvider.getLocale());
                result = checkDoubleOnPositive(num);
            } catch (ParseException e) {
                result = false;
            }
        } else {
            result = (value instanceof Double && checkDoubleOnPositive((Double) value)) || (value instanceof BigDecimal && checkBigDecimalOnPositive((BigDecimal) value));
        }

        if (!result) {
            String msg = message != null ? MessageUtils.loadString(messagesPack, message) : "Invalid value '%s'";
            throw new ValidationException(String.format(msg, value));
        }
    }
}
{'fraction_non_alphanumeric': 0.0996644957568581, 'fraction_numerical': 0.015393724097098875, 'mean_word_length': 3.3095238095238093, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3439863', 'n_tokens_mistral': 1466, 'n_tokens_neox': 1362, 'n_words': 310}