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

patterntesting.runtime.dbc.DbC Maven / Gradle / Ivy

Go to download

PatternTesting Runtime (patterntesting-rt) is the runtime component for the PatternTesting framework. It provides the annotations and base classes for the PatternTesting testing framework (e.g. patterntesting-check, patterntesting-concurrent or patterntesting-exception) but can be also used standalone for classpath monitoring or profiling. It uses AOP and AspectJ to perform this feat.

There is a newer version: 2.4.0
Show newest version
/**
 *
 */
package patterntesting.runtime.dbc;

import org.slf4j.*;

import patterntesting.runtime.util.Assertions;

/**
 * @author oliver
 *
 */
public final class DbC {

	private static final Logger log = LoggerFactory.getLogger(DbC.class);
    private static final boolean assertEnabled = Assertions.areEnabled();

    static {
        if (assertEnabled) {
            log.debug("DbC is active");
        } else {
            log.debug("DbC deactivated - call 'java -ea' (SunVM) to activate it");
        }
    }

    /** No need to instantiate it (utility class). */
    private DbC() {}

    /**
     * Use this method to describe the condition which must be true when your
     * method is called.
     *
     * @param precondition e.g. (arg0 != null)
     */
    public static void require(final boolean precondition) {
        require(precondition, "precondition violated");
    }

    /**
     * Use this method to describe the condition which must be true when your
     * method is called.
     *
     * @param precondition e.g. (arg0 != null)
     * @param message e.g. "null argument is not allowed"
     */
    public static void require(final boolean precondition, final Object message) {
        if (assertEnabled && !precondition) {
            throw new ContractViolation(message);
       }
    }

    /**
     * Use this method to describe the condition which must be true when the
     * preconditions were true and your method is finished.
     *
     * @param postcondition e.g. (value > 0)
     */
    public static void ensure(final boolean postcondition) {
        ensure(postcondition, "postcondition violated");
    }

    /**
     * Use this method to describe the condition which must be true when the
     * preconditions were true and your method is finished.
     *
     * @param postcondition e.g. (value > 0)
     * @param message e.g. "value must be positive"
     */
    public static void ensure(final boolean postcondition, final Object message) {
        if (assertEnabled && !postcondition) {
            throw new ContractViolation(message);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy