001/*
002 *  Copyright 2022 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.forms.question.validators;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.ametys.runtime.i18n.I18nizableText;
022import org.ametys.runtime.i18n.I18nizableTextParameter;
023import org.ametys.runtime.parameter.Errors;
024
025/**
026 * A number interval form validator
027 */
028public class NumberIntervalFormValidator extends AbstractIntervalFormValidator<Number>
029{
030    /**
031     * The constructor
032     * @param regexp the regexp
033     * @param mandatory mandatory
034     * @param min the min value
035     * @param max the max value
036     */
037    public NumberIntervalFormValidator(String regexp, boolean mandatory, Number min, Number max)
038    {
039        super(regexp, mandatory, min, max);
040    }
041
042    @Override
043    protected boolean isLessThan(Number value, Number min)
044    {
045        if (value.doubleValue() < min.doubleValue())
046        {
047            return true;
048        }
049        return false;
050    }
051
052    @Override
053    protected boolean isMoreThan(Number value, Number max)
054    {
055        if (value.doubleValue() > max.doubleValue())
056        {
057            return true;
058        }
059        return false;
060    }
061
062    @Override
063    protected void addIntervalError(Errors errors, Number min, Number max)
064    {
065        Map<String, I18nizableTextParameter> i18nParams = new HashMap<>();
066        i18nParams.put("minValue", new I18nizableText(min.toString()));
067        i18nParams.put("maxValue", new I18nizableText(max.toString()));
068        errors.addError(new I18nizableText("plugin.cms", "PLUGINS_FORMS_ENTRY_VALIDATOR_NUMBER_INTERVAL_ERROR", i18nParams));
069    }
070}