001    /**
002     *
003     */
004    package patterntesting.runtime.dbc;
005    
006    import org.slf4j.*;
007    
008    import patterntesting.runtime.util.Assertions;
009    
010    /**
011     * @author oliver
012     *
013     */
014    public final class DbC {
015    
016            private static final Logger log = LoggerFactory.getLogger(DbC.class);
017        private static final boolean assertEnabled = Assertions.areEnabled();
018    
019        static {
020            if (assertEnabled) {
021                log.debug("DbC is active");
022            } else {
023                log.debug("DbC deactivated - call 'java -ea' (SunVM) to activate it");
024            }
025        }
026    
027        /** No need to instantiate it (utility class). */
028        private DbC() {}
029    
030        /**
031         * Use this method to describe the condition which must be true when your
032         * method is called.
033         *
034         * @param precondition e.g. (arg0 != null)
035         */
036        public static void require(final boolean precondition) {
037            require(precondition, "precondition violated");
038        }
039    
040        /**
041         * Use this method to describe the condition which must be true when your
042         * method is called.
043         *
044         * @param precondition e.g. (arg0 != null)
045         * @param message e.g. "null argument is not allowed"
046         */
047        public static void require(final boolean precondition, final Object message) {
048            if (assertEnabled && !precondition) {
049                throw new ContractViolation(message);
050           }
051        }
052    
053        /**
054         * Use this method to describe the condition which must be true when the
055         * preconditions were true and your method is finished.
056         *
057         * @param postcondition e.g. (value > 0)
058         */
059        public static void ensure(final boolean postcondition) {
060            ensure(postcondition, "postcondition violated");
061        }
062    
063        /**
064         * Use this method to describe the condition which must be true when the
065         * preconditions were true and your method is finished.
066         *
067         * @param postcondition e.g. (value > 0)
068         * @param message e.g. "value must be positive"
069         */
070        public static void ensure(final boolean postcondition, final Object message) {
071            if (assertEnabled && !postcondition) {
072                throw new ContractViolation(message);
073            }
074        }
075    
076    }