1   /*
2    * Created by IntelliJ IDEA.
3    * User: Mike
4    * Date: Mar 5, 2004
5    * Time: 12:57:20 PM
6    */
7   package com.atlassian.core.util;
8   
9   /**
10   * A ProgressMeter class may be used to record progress of an on-going task while this progress is being monitored by
11   * another client. To support this use case the class is thread safe.
12   */
13  public class ProgressMeter
14  {
15      int percentageComplete;
16      private String status;
17      private int total;
18      private int currentCount;
19      private boolean completedSuccessfully = true;
20  
21      /**
22       * Use this method to set the completion %age to object 10 of 30 etc.
23       *
24       * @param count The current object count in progress
25       * @param total The total number of objects to be processed
26       */
27      public void setPercentage(int count, int total)
28      {
29          if (total < 0)
30          {
31              setPercentage(0);
32          }
33          else if (total <= count)
34          {
35              setPercentage(100);
36          }
37          else
38          {
39              int calculatedPercentage = ((int) (100 * (float) count / (float) total));
40  
41              if (count < total && calculatedPercentage == 100)
42                  calculatedPercentage = 99;
43  
44              setPercentage(calculatedPercentage);
45          }
46      }
47  
48      public synchronized void setStatus(String status)
49      {
50          this.status = status;
51      }
52  
53      /**
54       * @return the percentage completion. A value of 100 can be taken to indicate that progress is finished.
55       */
56      public synchronized int getPercentageComplete()
57      {
58          return percentageComplete;
59      }
60  
61      public synchronized String getStatus()
62      {
63          return status;
64      }
65  
66      public synchronized void setPercentage(int percentageComplete)
67      {
68          this.percentageComplete = percentageComplete;
69      }
70  
71      public synchronized int getCurrentCount()
72      {
73          return currentCount;
74      }
75  
76      public synchronized void setCurrentCount(int currentCount)
77      {
78          this.currentCount = currentCount;
79          updatePercentageComplete();
80      }
81  
82      private void updatePercentageComplete()
83      {
84          setPercentage(getCurrentCount(),getTotal());
85      }
86  
87      public synchronized int getTotal()
88      {
89          return total;
90      }
91  
92      public synchronized void setTotalObjects(int total)
93      {
94          this.total = total;
95          updatePercentageComplete();
96      }
97  
98      /**
99       * This method should only be called once you know the task is complete (which is discovered by calling
100      * {@link ProgressMeter#getPercentageComplete()}.
101      * 
102      * @return true if the task completed successfully; false if there was an error which prevented the task from
103      *         completing.
104      */
105     public synchronized boolean isCompletedSuccessfully()
106     {
107         return completedSuccessfully;
108     }
109 
110     public synchronized void setCompletedSuccessfully(boolean completedSuccessfully)
111     {
112         this.completedSuccessfully = completedSuccessfully;
113     }
114 }