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

ar.com.zauber.commons.validate.Validate Maven / Gradle / Ivy

There is a newer version: 3.55
Show newest version
/**
 * Copyright (c) 2005-2015 Zauber S.A. 
 *
 * 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 ar.com.zauber.commons.validate;

import org.apache.commons.lang.StringUtils;

/**
 * Extiende {@link org.apache.commons.lang.Validate} ampliando su interfaz
 * 
 * @author Pablo Grigolatto
 * @since May 31, 2010
 */
public class Validate extends org.apache.commons.lang.Validate {

    /**
     * Valida que los objetos recibidos no sean null mediante Validate.notNull()
     *  
     * @param objects los objetos a evaluar
     */
    public static void notNull(final Object... objects) {
        for (int i = 0; i < objects.length; i++) {
            notNull(objects[i], nullMessage(i));
        }
    }

    /**
     * Valida que los objetos recibidos no sean null mediante 
     * {@link Validate}.notNull() y, en caso de ser {@link String}, 
     * valida que no esté vacío mediante {@link StringUtils}.isNotBlank()
     *  
     * @param objects los objetos a evaluar
     */
    public static void notBlank(final Object... objects) {
        for (int i = 0; i < objects.length; i++) {
            final Object object = objects[i];
            if(object instanceof String) {
                isTrue(StringUtils.isNotBlank((String) object), blankMessage(i));
            } else {
                notNull(object, nullMessage(i));
            }
        }
    }

    /**
     * 

Validate that the argument condition is true; otherwise * throwing an exception with the specified message. This method is useful when * validating according to an arbitrary boolean expression, such as validating an * object or using your own custom validation expression.

* *
Validate.isTrue(myValue >= arg, "myValue(%s) should be >= arg (%s).", myValue, arg);
* * @param expression the boolean expression to check * @param format the exception message format if invalid * @param args the arguments to append to the message format when invalid * @throws IllegalArgumentException if expression is false */ public static void isTrue(final boolean expression, final String format, final Object... args) { if (!expression) { final String message = (format != null) ? String.format(format, args) : "The validated expression is false"; throw new IllegalArgumentException(message); } } /** *

Validate that the argument condition is false; otherwise * throwing an exception with the specified message. This method is useful when * validating according to an arbitrary boolean expression, such as validating an * object or using your own custom validation expression.

* *
Validate.isFalse(myValue >= arg, "myValue(%s) should not be >= arg (%s).", myValue, arg);
* * @param expression the boolean expression to check * @param format the exception message format if invalid * @param args the arguments to append to the message format when invalid * @throws IllegalArgumentException if expression is true */ public static void isFalse(final boolean expression, final String format, final Object... args) { if (expression) { final String message = (format != null) ? String.format(format, args) : "The validated expression is true"; throw new IllegalArgumentException(message); } } /** @return el mensaje de error para validación por blank*/ private static String blankMessage(final int i) { return errorMessage(i, "blank"); } /** @return el mensaje de error para validación por null */ private static String nullMessage(final int i) { return errorMessage(i, "null"); } /** @return el mensaje de error base */ private static String errorMessage(final int i, final String string) { return "The validated objects[" + i + "] is " + string; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy