Subversion Repositories DevTools

Rev

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

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