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 org.ametys.runtime.parameter.DefaultValidator;
019import org.ametys.runtime.parameter.Errors;
020
021/**
022 * An interval validator for generic type
023 * @param <T> the generic type to compare
024 */
025public abstract class AbstractIntervalFormValidator<T> extends DefaultValidator
026{
027    /** The min value */
028    protected T _min;
029    /** The max value */
030    protected T _max;
031    
032    /**
033     * The constructor
034     * @param regexp the regexp
035     * @param mandatory mandatory
036     * @param min the min value
037     * @param max the max value
038     */
039    public AbstractIntervalFormValidator (String regexp, boolean mandatory, T min, T max)
040    {
041        super(regexp, mandatory);
042        _min = min;
043        _max = max;
044    }
045    
046    @SuppressWarnings("unchecked")
047    @Override
048    protected void validateSingleValue(Object value, Errors errors)
049    {
050        super.validateSingleValue(value, errors);
051        if (value != null)
052        {
053            if (_min != null && isLessThan((T) value, _min))
054            {
055                if (getLogger().isDebugEnabled())
056                {
057                    getLogger().debug("Entry '" + value + "' is invalid : " + _min + " is greater than " + value);
058                }
059                addIntervalError(errors, _min, (T) value);
060            }
061            if (_max != null && isMoreThan((T) value, _max))
062            {
063                if (getLogger().isDebugEnabled())
064                {
065                    getLogger().debug("Entry '" + value + "' is invalid : " + _max + " is smaller than " + value);
066                }
067                addIntervalError(errors, (T) value, _max);
068            }
069        }
070    }
071    
072    @Override
073    protected void validateArrayValues(Object[] values, Errors errors)
074    {
075        super.validateArrayValues(values, errors);
076        for (Object valueToValidate : values)
077        {
078            validateSingleValue(valueToValidate, errors);
079        }
080    }
081    
082    /**
083     * Test if the first number is less than the second one.
084     * @param n1 The first number to compare.
085     * @param n2 The second number to compare.
086     * @return true if the first number is less than the first, false otherwise.
087     */
088    protected abstract boolean isLessThan(T n1, T n2);
089    
090    /**
091     * Test if the first number is more than the second one.
092     * @param n1 The first number to compare.
093     * @param n2 The second number to compare.
094     * @return true if the first number is more than the first, false otherwise.
095     */
096    protected abstract boolean isMoreThan(T n1, T n2);
097    
098    /**
099     * Add an error when the max value is less than the min value
100     * @param errors The list of errors
101     * @param min The min value
102     * @param max The max value
103     */
104    protected abstract void addIntervalError(Errors errors, T min, T max);
105}