package com.mindprod.example;

/**
 * Methods to display on the console the fact your app is still alive and busy working away. These are classics from the
 * days of the Apple][ and DOS. These won't work on eclipse which displays \b as a box instead of backspace.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products, based on the work of Gordon Beaton.
 * @version 1.0
 */
public final class TestImAlive
    {
    // ------------------------------ FIELDS ------------------------------

    /**
     * chars to cycle through in the bubble display
     */
    private static final char bubbles[] = { '.', 'o', 'O', 'o' };

    /**
     * chars to cycle through in the spinner display
     */
    private static final char spins[] = { '-', '\\', '|', '/' };

    /**
     * where we are in the bubble cycle 0..4
     */
    private static int bubbleCycle = 0;

    /**
     * where we are in the spinner cycle 0..4
     */
    private static int spinCycle = 0;

    // -------------------------- STATIC METHODS --------------------------

    /**
     * display an animated bubble on the console that kicks over each elapsedTime spinner is called.
     */
    private static void bubbler()
        {
        // display a varying char over top of the previous one.
        // If \b does not work, try \r

        System.out.print( bubbles[ bubbleCycle ] + "\b" );
        // use "\r" instead of '\r' to force + to mean concatenation
        bubbleCycle = ( bubbleCycle + 1 ) & 3;
        // cycles 0, 1, 2, 3, 0, 1...
        }

    /**
     * display an animated spinner on the console that kicks over each elapsedTime spinner is called.
     */
    private static void spinner()
        {
        // display a varying char over top of the previous one.
        System.out.print( spins[ spinCycle ] + "\b" );

        spinCycle = ( spinCycle + 1 ) & 3;
        // cycles 0, 1, 2, 3, 0, 1 ...
        }

    // --------------------------- main() method ---------------------------

    /**
     * Test harness for bubbler and spinner
     *
     * @param args not used
     */
    @SuppressWarnings( { "EmptyCatchBlock" } )
    public static void main( String[] args )
        {
        for ( int i = 0; i < 20; i++ )
            {
            try
                {
                // simulate the program doing useful work.
                Thread.sleep( 250 );// sleep .25 a second
                }
            catch ( InterruptedException e )
                {
                }
            bubbler();
            }// end for

        for ( int i = 0; i < 20; i++ )
            {
            try
                {
                Thread.sleep( 250 );// sleep .25 a second
                }
            catch ( InterruptedException e )
                {
                }
            spinner();
            }// end for
        }
    }