ValidatorUtils.java

/**
 * TFG 75.678 - TFG Desarrollo web 2020 e-Learning for Schools
 * Copyright (C) 2020  Eduardo Rodriguez Carro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.uoc.tfg.sel.validation;

import org.uoc.tfg.sel.web.model.ErrorCodes;

/**
 * Utilidades de validacion
 * @author Eduardo Rodriguez Carro
 */
public abstract class ValidatorUtils {


    /**
     * No instanciable
     */
    private ValidatorUtils() {
        /* Not implemented */
    }

    /**
     * Verifica que el campo tiene un valor
     *
     * @param <T> the generic type
     * @param field campo a evaluar
     * @return true/false
     */
    public static <T> boolean hasValue(T field) {
        return field != null && !field.toString().trim().isEmpty();
    }

    /**
     * Verifica que el campo no es nulo
     *
     * @param <T>	tipo generico
     * @param field	campo a evaluar
     * @return true/false
     */
    public static <T> boolean isNonNull(T field) {
        return field != null;
    }

    /**
     * Verifica que el campo no es nulo
     *
     * @param <T> tipo generico
     * @param fieldName Nombre del campo
     * @param field campo a evaluar
     * @throws ValidationException error en la validacion
     */
    public static <T> void assertIsNonNull(String fieldName, T field) throws ValidationException {
    	assertIsNonNull(ErrorCodes.VALIDATION,fieldName,field);
    }

    /**
     * Verifica que el campo no es nulo
     *
     * @param <T> tipo generico
     * @param code codigo de error
     * @param fieldName Nombre del campo
     * @param field campo a evaluar
     * @throws ValidationException error en la validacion
     */
    public static <T> void assertIsNonNull(ErrorCodes code, String fieldName, T field) throws ValidationException {
        if (!isNonNull(field)) {
            throw new ValidationException(code, String.format("Field %s is required", fieldName));
        }
    }

    /**
     * Verifica que el campo tiene valor
     *
     * @param <T> tipo generico
     * @param fieldName Nombre del campo
     * @param field campo a evaluar
     * @throws ValidationException error en la validacion
     */
    public static <T> void assertHasValue(String fieldName, T field) throws ValidationException {
        assertHasValue(ErrorCodes.VALIDATION, fieldName, field);
    }

    /**
     * Verifica que el campo tiene valor
     *
     * @param <T> tipo generico
     * @param code codigo de error
     * @param fieldName Nombre del campo
     * @param field campo a evaluar
     * @throws ValidationException error en la validacion
     */
    public static <T> void assertHasValue(ErrorCodes code, String fieldName, T field) throws ValidationException {
        if (!hasValue(field)) {
            throw new ValidationException(code, String.format("Field %s is required", fieldName));
        }
    }

}