Subversion Repositories DevTools

Rev

Rev 7033 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6914 dpurdie 1
package com.erggroup.buildtool.ripple;
2
 
3
import org.apache.log4j.Logger;
4
 
5
public class Phase
6
{
7
    private static final Logger mLogger = Logger.getLogger(Phase.class);
8
 
9
    public long    startTime = 0;
10
    public String  sText = "";
11
    public String  sPrefix ="";
12
    public long    careFactor = 0;
13
    public boolean bDisplay = false;
14
 
15
    public Phase()
16
    {
17
        reset(null,0);
18
    }
19
 
20
    public Phase(String prefix)
21
    {
22
        if ( prefix == null || prefix.length() == 0) {
23
            sPrefix = "";
24
        } else { 
25
            sPrefix = prefix + ":";
26
        }
27
        reset(null,0);
28
    }
29
 
30
    /** Reset - the internal data
31
     * 
32
     * @param text - Name of the Phase
33
     * @param cf   - Care Factor in seconds.
34
     */
35
    private void reset(String text, int cf)
36
    {
37
        careFactor = cf;
38
        startTime = System.currentTimeMillis();
39
        if (text == null)
40
            sText = "";
41
        else
42
            sText = text;   
43
    }
44
 
45
    /**
46
     * Set the current phase.
47
     * <br>Changes to the Text will reset the phase duration counter
48
     * 
49
     * @param text - Name of the Phase
50
     * @param cf   - Care Factor in seconds. A guess as the the max time that we should spend in this phase.
51
     *               Used to indicate that the program is stuck in a particular phase.
52
     *               Zero indicate that we do not care.
53
     */
54
    public void setPhase(String text, int cf)
55
    {
56
 
57
        if (bDisplay)
58
        {
59
            if ( !sText.equals(text) )
60
            {
61
                //  Display duration of previous phase
62
                mLogger.fatal(toString());
63
                reset(text,cf);
64
            }
65
        }
66
        else
67
        {
68
            bDisplay = true;
69
            reset(text,cf);
70
        }
71
    }
72
 
73
    /**
74
     * Set the current phase, with a expected duration of infinite
75
     * <br>Changes to the Text will reset the phase duration counter
76
     * 
77
     * @param text - Name of the Phase
78
     */
79
 
80
    public void setPhase(String text)
81
    {
82
        setPhase(text, 0);
83
    }
84
 
85
    /**
86
     * Set the expected duration of this phase
87
     * After this duration the phase is to be regarded as not happy
88
     * 
89
     * @param cf    - Care Factor in seconds
90
     */
91
    public void setCareFactor(long cf)
92
    {
93
        careFactor = cf;
94
    }
95
 
96
    public String toString()
97
    {
98
        long milliseconds = getDelta();
99
        int seconds = (int) (milliseconds / 1000);
100
        int msecs = (int) (milliseconds % 1000);
101
        String txt = String.format("%s%s:%d.%03d", sPrefix, sText, seconds, msecs);
102
        return txt;
103
    }
104
 
105
    public String toStringSecs()
106
    {
107
        return sPrefix + sText + ":" + getDeltaSecs();
108
    }
109
 
110
    public long getDelta()
111
    {
112
        if (sText.length() > 0)
113
            return System.currentTimeMillis() - startTime;
114
        return 0;
115
    }
116
 
117
    public long getDeltaSecs()
118
    {
119
        if (sText.length() > 0)
120
            return (System.currentTimeMillis() - startTime)/1000;
121
        return 0;
122
    }
123
 
124
    public boolean isHappy()
125
    {
126
        if (careFactor <= 0)
127
            return true;
128
 
129
        return ((System.currentTimeMillis() - startTime)/1000) < careFactor;
130
 
131
    }
132
 
133
    public String happyText() {
134
        return sPrefix + sText + ":" + getDeltaSecs() + " (" + careFactor + ")" ;
135
    }
136
}