com.sample.common.util.AssertUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.sample.common.util.AssertUtil.java

Source

/**
 *   27/giu/2011
 *
 * Copyright (c) 2010 Alten Italia, All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Alten Italia ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered 
 * into with Alten Italia.
 *
 * Alten Italia - MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY 
 * OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 
 * OR NON-INFRINGEMENT. ALTEN ITALIA SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
 * BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR 
 * ITS DERIVATIVES.
 */
package com.sample.common.util;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;

/**
 * Una classe di utilita per le validazioni
 * 
 * @author Massimo Ugues
 * 
 */
public class AssertUtil {

    /**
     * Solleva una @link(IllegalArgumentException) con message message in caso di object null
     * 
     * @param object
     * @param message
     */
    public static void assertNotNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void assertNotNull(Object object, RuntimeException cause) {
        if (object == null) {
            cause.initCause(cause.getCause());
            throw cause;
        }
    }

    /**
     * Solleva una @link(IllegalArgumentException) con message message in caso di object non null
     * 
     * @param object
     * @param message
     */
    public static void assertNull(Object object, String message) {
        if (object != null) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Solleva una @link(IllegalArgumentException) con message message in caso di string empty
     * 
     * @param str
     * @param message
     */
    public static void assertNotEmpty(String str, String message) {
        if (StringUtils.isEmpty(str)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Solleva una @link(IllegalArgumentException) con message message in caso di object non null
     * 
     * @param object
     * @param message
     */
    @SuppressWarnings("rawtypes")
    public static void assertEmpty(Collection list, String message) {
        if (!list.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }

    @SuppressWarnings("rawtypes")
    public static void assertNotEmpty(Collection list, String message) {
        if (list.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Solleva una @link(IllegalArgumentException) con message message in caso di object non null
     * 
     * @param object
     * @param message
     */
    public static void assertEquals(Object obj1, Object obj2, String message) {
        if (!obj1.equals(obj2)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Solleva una @link(IllegalArgumentException) con message message in caso di condition false
     * 
     * @param object
     * @param message
     */
    public static void assertTrue(boolean condition, String message) {
        if (!condition) {
            throw new IllegalArgumentException(message);
        }
    }
}