Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
33   139   19   2.54
14   96   0.58   13
13     1.46  
1    
 
 
  AbstractLongRunningTask       Line # 17 33 19 18.3% 0.18333334
 
  (1)
 
1    /*
2    * Created by IntelliJ IDEA.
3    * User: Mike
4    * Date: Feb 13, 2004
5    * Time: 7:47:06 PM
6    */
7    package com.atlassian.core.task.longrunning;
8   
9    import com.atlassian.core.util.DateUtils;
10    import com.atlassian.core.util.ProgressMeter;
11   
12    import java.util.MissingResourceException;
13    import java.util.ResourceBundle;
14   
15    import org.apache.log4j.Category;
16   
 
17    public abstract class AbstractLongRunningTask implements LongRunningTask
18    {
19    public static final Category log = Category.getInstance(AbstractLongRunningTask.class);
20    long startTime = System.currentTimeMillis();
21    long stopTime = 0;
22    protected ProgressMeter progress;
23   
 
24  1 toggle protected AbstractLongRunningTask()
25    {
26  1 progress = new ProgressMeter();
27  1 progress.setStatus("Initializing... ");
28    }
29   
 
30  1 toggle public void run()
31    {
32  1 progress.setStatus("Starting... ");
33  1 startTime = System.currentTimeMillis();
34    }
35   
36    /**
37    * Internationalisation key for the name of the task, so that the task's name
38    * can be displayed in the User Interface. Default implementation returns null,
39    * since some uses of LongRunningTask may not require internationalisation of
40    * the name (eg if the name of the task is never displayed in the UI).
41    *
42    * @return I18n key as a string, or null if no key defined. Null if not overridden.
43    */
 
44  0 toggle public String getNameKey()
45    {
46  0 return null;
47    }
48   
49   
 
50  0 toggle public int getPercentageComplete()
51    {
52  0 return progress.getPercentageComplete();
53    }
54   
 
55  0 toggle public String getCurrentStatus()
56    {
57  0 return progress.getStatus();
58    }
59   
 
60  2 toggle public long getElapsedTime()
61    {
62  2 return (stopTime == 0 ? System.currentTimeMillis() : stopTime) - startTime;
63    }
64   
 
65  0 toggle public long getEstimatedTimeRemaining()
66    {
67  0 long elapsedTime = getElapsedTime();
68   
69  0 if (getPercentageComplete() == 0)
70  0 return 0;
71   
72  0 long totalTimeEstimate = 100 * elapsedTime / getPercentageComplete();
73  0 return totalTimeEstimate - elapsedTime;
74    }
75   
 
76  0 toggle public boolean isComplete()
77    {
78  0 return getPercentageComplete() == 100;
79    }
80   
 
81  0 toggle public String getPrettyElapsedTime()
82    {
83  0 return prettyTime(getElapsedTime());
84    }
85   
86    protected abstract ResourceBundle getResourceBundle();
87   
 
88  0 toggle private String prettyTime(long time)
89    {
90  0 if (time < 1000)
91    {
92  0 return "Less than a second";
93    }
94  0 else if (time / DateUtils.SECOND_MILLIS < 60)
95    {
96  0 return time / DateUtils.SECOND_MILLIS + " seconds";
97    }
98   
99  0 String minutesAndAbove = null;
100   
101  0 try
102    {
103  0 minutesAndAbove = DateUtils.getDurationPretty(time / DateUtils.SECOND_MILLIS, getResourceBundle());
104    }
105    catch (MissingResourceException e)
106    {
107  0 log.error("Could not load resourcebundle for 'minute'!'", e);
108    }
109   
110  0 long secondsRemainder = (time / DateUtils.SECOND_MILLIS) % 60;
111   
112  0 if (secondsRemainder > 0)
113    {
114  0 minutesAndAbove += ", " + secondsRemainder + " second" + (secondsRemainder == 1 ? "" : "s");
115    }
116   
117  0 return minutesAndAbove;
118    }
119   
 
120  0 toggle public String getPrettyTimeRemaining()
121    {
122  0 long estimatedTimeRemaining = getEstimatedTimeRemaining();
123   
124  0 if (estimatedTimeRemaining == 0)
125  0 return "Unknown";
126   
127  0 return prettyTime(estimatedTimeRemaining);
128    }
129   
 
130  0 toggle public boolean isSuccessful()
131    {
132  0 return progress.isCompletedSuccessfully();
133    }
134   
 
135  1 toggle protected void stopTimer()
136    {
137  1 stopTime = System.currentTimeMillis();
138    }
139    }