Subversion Repositories DevTools

Rev

Rev 7033 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.erggroup.buildtool.ripple;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Phase
{
    private static final Logger mLogger = LoggerFactory.getLogger(Phase.class);
    
    public long    startTime = 0;
    public String  sText = "";
    public String  sPrefix ="";
    public long    careFactor = 0;
    public boolean bDisplay = false;
    
    public Phase()
    {
        reset(null,0);
    }
    
    public Phase(String prefix)
    {
        if ( prefix == null || prefix.length() == 0) {
            sPrefix = "";
        } else { 
            sPrefix = prefix + ":";
        }
        reset(null,0);
    }
    
    /** Reset - the internal data
     * 
     * @param text - Name of the Phase
     * @param cf   - Care Factor in seconds.
     */
    private void reset(String text, int cf)
    {
        careFactor = cf;
        startTime = System.currentTimeMillis();
        if (text == null)
            sText = "";
        else
            sText = text;   
    }

    /**
     * Set the current phase.
     * <br>Changes to the Text will reset the phase duration counter
     * 
     * @param text - Name of the Phase
     * @param cf   - Care Factor in seconds. A guess as the the max time that we should spend in this phase.
     *               Used to indicate that the program is stuck in a particular phase.
     *               Zero indicate that we do not care.
     */
    public void setPhase(String text, int cf)
    {
        
        if (bDisplay)
        {
            if ( !sText.equals(text) )
            {
                //  Display duration of previous phase
                mLogger.error("{}", this);
                reset(text,cf);
            }
        }
        else
        {
            bDisplay = true;
            reset(text,cf);
        }
    }
    
    /**
     * Set the current phase, with a expected duration of infinite
     * <br>Changes to the Text will reset the phase duration counter
     * 
     * @param text - Name of the Phase
     */
 
    public void setPhase(String text)
    {
        setPhase(text, 0);
    }
    
    /**
     * Set the expected duration of this phase
     * After this duration the phase is to be regarded as not happy
     * 
     * @param cf    - Care Factor in seconds
     */
    public void setCareFactor(long cf)
    {
        careFactor = cf;
    }
    
    public String toString()
    {
        long milliseconds = getDelta();
        int seconds = (int) (milliseconds / 1000);
        int msecs = (int) (milliseconds % 1000);
        String txt = String.format("%s%s:%d.%03d", sPrefix, sText, seconds, msecs);
        return txt;
    }
    
    public String toStringSecs()
    {
        return sPrefix + sText + ":" + getDeltaSecs();
    }
    
    public long getDelta()
    {
        if (sText.length() > 0)
            return System.currentTimeMillis() - startTime;
        return 0;
    }
    
    public long getDeltaSecs()
    {
        if (sText.length() > 0)
            return (System.currentTimeMillis() - startTime)/1000;
        return 0;
    }
    
    public boolean isHappy()
    {
        if (careFactor <= 0)
            return true;
        
        return ((System.currentTimeMillis() - startTime)/1000) < careFactor;
        
    }

    public String happyText() {
        return sPrefix + sText + ":" + getDeltaSecs() + " (" + careFactor + ")" ;
    }
}