/**
 * Copyright (c) 2005-2012 EBM WebSourcing, 2012-2015 Linagora
 * 
 * This program/library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * This program/library 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 Lesser General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program/library; If not, see <http://www.gnu.org/licenses/>
 * for the GNU Lesser General Public License version 2.1.
 */
package org.ow2.petals.microkernel.util;

import org.ow2.petals.microkernel.api.util.LoggingUtil;

/**
 * Class managing parameters to checks there nullity or emptyness.
 * @author wjoseph - EBM WebSourcing
 */
public final class ParameterCheckHelper {

    private static final String MUST_NOT_BE_EMPTY = " cannot be empty";

    private static final String MUST_NOT_BE_NULL = " cannot be null";

    private ParameterCheckHelper() {
        // avoid to be build
    }

    /**
     * Checks if the parameter is an empty string
     * 
     * @param parameter
     *            parameter to be tested
     * @param parameterName
     *            name of the parameter
     * @throws IllegalArgumentException
     *             if parameter is empty
     */
    public static void isEmptyParameter(String parameter, String parameterName)
            throws IllegalArgumentException {
        if (parameter != null && parameter.length() == 0) {
            throw new IllegalArgumentException(parameterName
                    + MUST_NOT_BE_EMPTY);
        }
    }

    /**
     * Checks if the parameter is an empty string and log in error if it is
     * empty
     * 
     * @param parameter
     *            parameter to be tested
     * @param parameterName
     *            name of the parameter
     * @param log
     *            logger to use
     * @throws IllegalArgumentException
     *             if parameter is empty
     */
    public static void isEmptyParameterWithLog(String parameter,
            String parameterName, LoggingUtil log)
            throws IllegalArgumentException {
        if (parameter != null && parameter.length() == 0) {
            log.error(parameterName + MUST_NOT_BE_EMPTY);
            throw new IllegalArgumentException(parameterName
                    + MUST_NOT_BE_EMPTY);
        }
    }

    /**
     * Checks if the parameter is null
     * 
     * @param parameter
     *            parameter to be tested
     * @param parameterName
     *            name of the parameter
     * @throws IllegalArgumentException
     *             if parameter is null
     */
    public static void isNullParameter(Object parameter, String parameterName)
            throws IllegalArgumentException {
        if (parameter == null) {
            throw new IllegalArgumentException(parameterName + MUST_NOT_BE_NULL);
        }
    }

    /**
     * Checks if the parameter is null and log in error if it is null
     * 
     * @param parameter
     *            parameter to be tested
     * @param parameterName
     *            name of the parameter
     * @param log
     *            logger to use
     * @throws IllegalArgumentException
     *             if parameter is null
     */
    public static void isNullParameterWithLog(Object parameter,
            String parameterName, LoggingUtil log)
            throws IllegalArgumentException {
        if (parameter == null) {
            log.error(parameterName + MUST_NOT_BE_NULL);
            throw new IllegalArgumentException(parameterName + MUST_NOT_BE_NULL);
        }
    }

    /**
     * Checks if the parameter is null or empty
     * 
     * @param parameter
     *            parameter to be tested
     * @param parameterName
     *            name of the parameter
     * @throws IllegalArgumentException
     *             if parameter is null
     */
    public static void isNullOrEmptyParameter(String parameter,
            String parameterName) {
        isEmptyParameter(parameter, parameterName);
        isNullParameter(parameter, parameterName);
    }

    /**
     * Checks if the parameter is null or empty and log in error if it is null
     * 
     * @param parameter
     *            parameter to be tested
     * @param parameterName
     *            name of the parameter
     * @param log
     *            logger to use
     * @throws IllegalArgumentException
     *             if parameter is null
     */
    public static void isNullOrEmptyParameterWithLog(String parameter,
            String parameterName, LoggingUtil log) {
        isEmptyParameterWithLog(parameter, parameterName, log);
        isNullParameterWithLog(parameter, parameterName, log);
    }

}
