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

colesico.framework.dslvalidator.commands.AbstractIntervalVerifier Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
/*
 * Copyright © 2014-2020 Vladlen V. Larionov and others as noted.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package colesico.framework.dslvalidator.commands;

import colesico.framework.dslvalidator.Command;
import colesico.framework.dslvalidator.ValidationContext;

/**
 * @author Vladlen Larionov
 */
abstract public class AbstractIntervalVerifier implements Command {

    protected final Number min;
    protected final Number max;
    protected final boolean includeEndpoints;

    public AbstractIntervalVerifier(Number min, Number max,
                                    boolean includeEndpoints) {
        this.min = min;
        this.max = max;
        this.includeEndpoints = includeEndpoints;
    }

    abstract protected String valueShouldBeBetween(Number min, Number max);

    abstract protected String valueShouldBeGreaterThan(Number min);

    abstract protected String valueShouldBeLessThan(Number max);

    protected void execute(Number value, ValidationContext context) {
        boolean checkEqual = !includeEndpoints;
        if (value != null) {
            if (min != null && max != null) {
                if (lte(value, min, checkEqual) || lte(max, value, checkEqual)) {
                    context.addError(this.getClass().getSimpleName() + "Between", valueShouldBeBetween(min, max));
                }
            } else if (min != null) {
                if (lte(value, min, checkEqual)) {
                    context.addError(getClass().getSimpleName() + "Min", valueShouldBeGreaterThan(min));
                }
            } else if (max != null) {
                if (lte(max, value, checkEqual)) {
                    context.addError(getClass().getSimpleName() + "Max", valueShouldBeLessThan(max));
                }
            } else {
                throw new RuntimeException("Min or max value required");
            }
        }
    }

    /**
     * Test valA less than valB or valA equal valB
     *
     * @param valA
     * @param valB
     * @return
     */
    protected boolean lte(Number valA, Number valB, boolean checkEqual) {
        if (valA instanceof Integer && valB instanceof Integer) {
            if (checkEqual) {
                return valA.intValue() <= valB.intValue();
            } else {
                return valA.intValue() < valB.intValue();
            }
        }
        if (valA instanceof Long && valB instanceof Long) {
            if (checkEqual) {
                return valA.longValue() <= valB.longValue();
            } else {
                return valA.longValue() < valB.longValue();
            }
        }
        if (valA instanceof Double && valB instanceof Double) {
            if (checkEqual) {
                return valA.doubleValue() <= valB.doubleValue();
            } else {
                return valA.doubleValue() < valB.doubleValue();
            }
        }
        if (valA instanceof Short && valB instanceof Short) {
            if (checkEqual) {
                return valA.shortValue() <= valB.shortValue();
            } else {
                return valA.shortValue() < valB.shortValue();
            }
        }
        if (valA instanceof Float && valB instanceof Float) {
            if (checkEqual) {
                return valA.floatValue() <= valB.floatValue();
            } else {
                return valA.floatValue() < valB.floatValue();
            }
        }
        if (valA instanceof Byte && valB instanceof Byte) {
            if (checkEqual) {
                return valA.byteValue() <= valB.byteValue();
            } else {
                return valA.byteValue() < valB.byteValue();
            }
        }
        throw new RuntimeException("Unsupported value type:" + valA.getClass().getName() + " or " + valB.getClass().getName());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy