Skip to content

Enum Stop

Ori Roth edited this page Apr 2, 2017 · 3 revisions
package il.ac.technion.cs.ssdl.utils;

import static org.junit.Assert.fail;
/**
 * A no-instances class, serving as the system global manager of the policy of stopping execution due to "Design By Contract" bugs.
 * 
 * Author Yossi Gil, 2008/06/20
 */
public enum STOP {
    ;


    /**
     * A never-returning method to be used for dealing with assertions that should stop the program run.
     * 
     * → t
     *            the exception to be associated with this termination
     */
    public static void stop(final Throwable t) {
        stop(t, "Program must stop due to this error: ");
    }


    /**
     * A never-returning method to be used for dealing with assertions that should stop the program run.
     * 
     * → t
     *            the exception to be associated with this termination
     * → s
     *            a more detailed description of the error
     */
    public static void stop(final Throwable t, final String s) {
        System.err.println(s);
        t.printStackTrace();
        stop(-1);
    }
    /**
     * An interface representing a stopping policy.
     * 
     * Author Yossi Gil, 2008/06/21
     */
    public static interface StopHandler {
        /**
         * What to do in case termination with an associated exit code was requested
         * 
         * → exitCode
         *            the exit code associated with the termination
         */
        public void stop(int exitCode);


        /**
         * What to do in case terminations with a specified error message was requested
         * 
         * → s
         *            the error message associated with the termination
         */
        public void stop(String s);
    }


    /**
     * Terminate the program with a specified exit code.
     * 
     * → exitCode
     *            the exit code associated with the termination
     */
    public static void stop(final int exitCode) {
        stopHandler.stop(exitCode);
    }


    /**
     * Terminate the program with a specified error message
     * 
     * → s
     *            the error message associated with the termination
     */
    public static void stop(final String s) {
        stopHandler.stop(s);
    }


    /**
     * Set the termination policy to program exit.
     * 
     * Return a {@link StopHandler} object specifying this policy. (It is safe to ignore this returned value)
     */
    public static StopHandler stopExit() {
        return stopHandler = new StopHandler() {
            public void stop(final int exitCode) {
                throw new AssertionError("Stop " + exitCode);
            }


            public void stop(final String s) {
                System.out.println(s);
                stop(-1);
            }
        };
    }


    /**
     * Set the termination policy to JUnit failure
     * 
     * Return a {@link StopHandler} object specifying this policy. (It is safe to ignore this returned value)
     */
    public static StopHandler stopFail() {
        return stopHandler = new StopHandler() {
            public void stop(final int exitCode) {
                fail("Design by contract failue, code = " + exitCode);
            }
            public void stop(final String s) {
                fail("Design by contract failue: " + s);
            }
        };
    }


    /**
     * Set the termination policy to throwing of a {@link Runnable} failure
     * 
     * Return a {@link StopHandler} object specifying this policy. (It is safe to ignore this returned value)
     */
    public static StopHandler stopRuntimeException() {
        return stopHandler = new StopHandler() {
            public void stop(final int exitCode) {
                throw new RuntimeException("Stop called, exit code=" + exitCode);
            }
            public void stop(final String s) {
                throw new RuntimeException("Stop called:" + s);
            }
        };
    }
    /**
     * Handler for program exit requests. Default behavior: JUnit failure.
     */
    private static StopHandler stopHandler = stopFail();
}
Clone this wiki locally