org.vulpe.commons.util.VulpeValidationUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.vulpe.commons.util.VulpeValidationUtil.java

Source

/**
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 *
 * Este programa  software livre; voc pode redistribu-lo e/ou
 * modific-lo sob os termos da Licena Pblica Geral GNU, conforme
 * publicada pela Free Software Foundation; tanto a verso 2 da
 * Licena como (a seu critrio) qualquer verso mais nova.
 *
 * Este programa  distribudo na expectativa de ser til, mas SEM
 * QUALQUER GARANTIA; sem mesmo a garantia implcita de
 * COMERCIALIZAO ou de ADEQUAO A QUALQUER PROPSITO EM
 * PARTICULAR. Consulte a Licena Pblica Geral GNU para obter mais
 * detalhes.
 *
 * Voc deve ter recebido uma cpia da Licena Pblica Geral GNU
 * junto com este programa; se no, escreva para a Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/**
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 *
 * 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.vulpe.commons.util;

import java.util.Collection;

import org.apache.commons.lang.ArrayUtils;
import org.vulpe.model.entity.VulpeEntity;

/**
 * Utility class to validation.
 *
 */
@SuppressWarnings("rawtypes")
public class VulpeValidationUtil {

    /**
     * Validate if value is not empty
     *
     * @param value
     * @return returns true if is not empty
     */
    public static boolean isNotEmpty(final Object value) {
        if (value == null) {
            return false;
        }

        if (value instanceof String) {
            return !value.toString().trim().equals("");
        }

        if (value instanceof Collection) {
            return !((Collection) value).isEmpty();
        }

        if (value instanceof VulpeEntity<?>) {
            return ((VulpeEntity<?>) value).getId() != null;
        }

        if (value.getClass().isArray()) {
            return ArrayUtils.isNotEmpty((Object[]) value);
        }

        return true;
    }

    public static boolean isNotEmpty(final Object... values) {
        boolean notEmpty = true;
        for (final Object value : values) {
            if (isEmpty(value)) {
                notEmpty = false;
                break;
            }
        }
        return notEmpty;
    }

    /**
     * Validate if value is empty.
     *
     * @param value
     * @return returns true if is empty
     */
    public static boolean isEmpty(final Object value) {
        return !isNotEmpty(value);
    }

    public static boolean isEmpty(final Object... values) {
        boolean empty = true;
        for (final Object value : values) {
            if (isNotEmpty(value)) {
                empty = false;
                break;
            }
        }
        return empty;
    }

    public static boolean isNull(final Object... objects) {
        boolean nullable = true;
        for (final Object object : objects) {
            if (object != null) {
                nullable = false;
                break;
            }
        }
        return nullable;

    }

    public static boolean isNotNull(final Object... objects) {
        boolean nullable = false;
        for (final Object object : objects) {
            if (object == null) {
                nullable = true;
                break;
            }
        }
        return !nullable;

    }
}