View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // ProgressData.java Copyright (c) 2009 Giovanni Azua Garcia
5   // bravegag@hotmail.com
6   //  
7   // This program is free software; you can redistribute it and/or
8   // modify it under the terms of the GNU General Public License
9   // as published by the Free Software Foundation; either version 3
10  // of the License, or (at your option) any later version.
11  //
12  // This program is distributed in the hope that it will be useful,
13  // but WITHOUT ANY WARRANTY; without even the implied warranty of
14  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  // GNU General Public License for more details.
16  //
17  // You should have received a copy of the GNU General Public License
18  // along with this program; if not, see <http://www.gnu.org/licenses/>.
19  //
20  //----------------------------------------------------------------------
21  package org.perfectjpattern.core.behavioral.observer.data;
22  
23  /**
24   * Immutable Observer event data intended to be used in notifying progress 
25   * information.
26   *
27   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
28   * @version $Revision: 1.0 $ $Date: Jun 17, 2007 3:00:29 PM $
29   */
30  public 
31  class ProgressData
32  extends StatusData
33  {
34      //------------------------------------------------------------------------
35      // public
36      //------------------------------------------------------------------------
37      /**
38       * Creates a progress information with precalculated task percentage
39       * completed.
40       * 
41       * @param aStatus Status of the task
42       * @param aMessage Description of the current status
43       * @param aProgress Percentage of completion of the task [0 .. 100]
44       * @throws IllegalArgumentException "'aStatus' must not be null"
45       * @throws IllegalArgumentException "'aMessage' must not be null"
46       */
47      public 
48      ProgressData(Status aStatus, String aMessage, int aProgress) 
49      {
50          super(aStatus, aMessage);
51          
52          theProgress = aProgress;
53      }
54      
55      //------------------------------------------------------------------------
56      /**
57       * Creates a progress information with total and current progress values,
58       * this variation of the constructor calculates the percentage based on the
59       * two values.
60       * 
61       * @param aStatus Status of the task
62       * @param aMessage Description of the current status
63       * @param aTotal Numeric total of the task
64       * @param aCurrent Current value of the task in reference to the total
65       */
66      public 
67      ProgressData(Status aStatus, String aMessage, int aTotal, int aCurrent) 
68      {
69          this(aStatus, aMessage, Math.round((float) aCurrent / 
70              (float) aTotal * 100.0f));
71      }
72      
73      //------------------------------------------------------------------------
74      /**
75       * Copy constructor.
76       * 
77       * @param aProgressData Existing ProgressData
78       */
79      public
80      ProgressData(ProgressData aProgressData) 
81      {
82          this(aProgressData.getStatus(), aProgressData.getMessage(), 
83              aProgressData.getProgress());
84      }    
85  
86      //------------------------------------------------------------------------
87      /**
88       * Returns the progress information as an Integer with value between 0 
89       * and 100.
90       * 
91       * @return Progress information as an Integer with value between 0 
92       * and 100.
93       */
94      public int 
95      getProgress() 
96      {
97          return theProgress;
98      }
99      
100     //------------------------------------------------------------------------
101 
102     /** 
103      * {@inheritDoc}
104      */
105     @Override
106     public boolean 
107     equals(Object anObject)
108     {
109         if (this == anObject)
110         {
111             return true;
112         }
113         
114         if (!super.equals(anObject))
115         {
116             return false;
117         }
118         
119         if (getClass() != anObject.getClass())
120         {
121             return false;            
122         }
123         
124         final ProgressData myAnother = (ProgressData) anObject;
125         if (theProgress != myAnother.theProgress)
126         {
127             return false;
128         }
129         
130         return true;
131     }
132     
133     //------------------------------------------------------------------------
134     /** 
135      * {@inheritDoc}
136      */
137     @Override
138     public int 
139     hashCode()
140     {
141         final int myPrime = 31;
142         int myResult = super.hashCode();
143         
144         myResult = myPrime * myResult + theProgress;
145         
146         return myResult;
147     }    
148     
149     //------------------------------------------------------------------------
150     /** 
151      * {@inheritDoc}
152      */
153     @Override
154     public String 
155     toString() 
156     {
157         return "ProgressData(" + super.toString() + ", Progress='" + 
158             getProgress() + "%')";
159     }
160 
161     //------------------------------------------------------------------------
162     // members
163     //------------------------------------------------------------------------
164     /**
165       * Progress completed in percent.
166       */
167     private final int theProgress;
168     
169     /**
170      * Reusable <code>ProgressData</code> instance corresponding to a 
171      * <b>Started</b> event.
172      */
173     public static final ProgressData STARTED = 
174        new ProgressData(Status.STARTED, "Task has started.", 0);
175 
176     /**
177      * Reusable <code>ProgressData</code> instance corresponding to a 
178      * <b>Failed</b> event.
179      */
180     public static final ProgressData FAILED = 
181        new ProgressData(Status.FAILED, "Task has failed", 0);
182 
183     /**
184      * Reusable <code>ProgressData</code> instance corresponding to a 
185      * <b>Completed</b> event.
186      */
187     public static final ProgressData COMPLETED = 
188        new ProgressData(Status.COMPLETED, "Task has completed.", 100);
189 }