View Javadoc

1   //----------------------------------------------------------------------
2   // 
3   // PerfectJPattern: "Design patterns are good but components are better!" 
4   // StatusData.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  import org.apache.commons.lang.*;
24  import org.perfectjpattern.core.api.behavioral.observer.data.*;
25  
26  /**
27   * Immutable Observer event data intended to be used in notifying status
28   * information.
29   * 
30   * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
31   * @version $Revision: 1.0 $ $Date: Jun 17, 2007 2:20:44 PM $
32   */
33  public
34  class StatusData 
35  implements IEventData 
36  {
37      //------------------------------------------------------------------------
38      // public
39      //------------------------------------------------------------------------
40      /**
41       * Constructor that creates a StatusData instance from a Status and message.
42       * 
43       * @param aStatus Status of the task
44       * @param aMessage Description of the current theStatus
45       * @throws IllegalArgumentException "'aStatus' must not be null"
46       * @throws IllegalArgumentException "'aMessage' must not be null"
47       */
48      public
49      StatusData(Status aStatus, String aMessage) 
50      {
51          Validate.notNull(aStatus, "'aStatus' must not be null");
52          Validate.notNull(aMessage, "'aMessage' must not be null");
53          
54          theStatus = aStatus;
55          theMessage = aMessage;
56      }
57  
58      //------------------------------------------------------------------------
59      /**
60       * Copy constructor.
61       * 
62       * @param aStatusData Existing StatusData
63       * @throws IllegalArgumentException "'aStatus' must not be null"
64       * @throws IllegalArgumentException "'aMessage' must not be null"
65       */
66      public
67      StatusData(StatusData aStatusData) 
68      {
69          this(aStatusData.getStatus(), aStatusData.getMessage());
70      }        
71      
72      //------------------------------------------------------------------------
73      /**
74       * Returns message associated with this StatusData. Offers a user-friendly
75       * description of the status.
76       * 
77       * @return Message associated with this StatusData. Offers a user-friendly
78       * description of the status.
79       */
80      public String 
81      getMessage() 
82      {
83          return theMessage;
84      }
85  
86      //------------------------------------------------------------------------
87      /**
88       * Returns the Status value associated with this StatusData from the set of
89       * predefined possible values {@link Status}.
90       * 
91       * @return Status value associated with this StatusData from the set of
92       * predefined possible values {@link Status}.
93       */
94      public Status 
95      getStatus() 
96      {
97          return theStatus;
98      }
99  
100     //------------------------------------------------------------------------
101     /** 
102      * {@inheritDoc}
103      */
104     @Override
105     public boolean 
106     equals(Object anObject)
107     {
108         if (this == anObject)
109         {
110             return true;
111         }
112         
113         if (anObject == null)
114         {
115             return false;
116         }
117         
118         if (getClass() != anObject.getClass())
119         {
120             return false;
121         }
122         
123         final StatusData myAnother = (StatusData) anObject;
124         if (!theMessage.equals(myAnother.theMessage))
125         {
126             return false;
127         }
128         
129         if (!theStatus.equals(myAnother.theStatus))
130         {
131             return false;
132         }
133         
134         return true;
135     }
136 
137     
138     //------------------------------------------------------------------------
139     /** 
140      * {@inheritDoc}
141      */
142     @Override
143     public int 
144     hashCode()
145     {
146         final int myPrime = 31;
147         int myResult = 1;
148         myResult = myPrime * myResult + ((theMessage == null) ? 0 : 
149             theMessage.hashCode());
150         myResult = myPrime * myResult + ((theStatus == null) ? 0 : 
151             theStatus.hashCode());
152         
153         return myResult;
154     }
155     
156     //------------------------------------------------------------------------
157     /** 
158      * {@inheritDoc}
159      */
160     @Override
161     public String 
162     toString() 
163     {
164         return "StatusData(Status='" + getStatus().toString() + 
165             "', Message='" + getMessage() + "')";
166     }
167         
168     //------------------------------------------------------------------------
169     // members
170     //------------------------------------------------------------------------
171     /**
172      * Status information.
173      */
174     private final Status theStatus;
175 
176     /**
177      * Descriptive information on the task status
178      */
179     private final String theMessage;
180     
181     /**
182      * Reusable <code>StatusData</code> instance corresponding to a 
183      * <b>Started</b> event.
184      */
185     public static final StatusData STARTED = new StatusData(Status.STARTED, 
186         "Task has started.");
187 
188     /**
189      * Reusable <code>StatusData</code> instance corresponding to a 
190      * <b>Failed</b> event.
191      */
192     public static final StatusData FAILED = new StatusData(Status.FAILED, 
193         "Task has failed");
194 
195     /**
196      * Reusable <code>StatusData</code> instance corresponding to a 
197      * <b>Completed</b> state.
198      */
199     public static final StatusData COMPLETED = new StatusData(Status.COMPLETED, 
200         "Task has completed.");    
201 }