View Javadoc

1   /**
2    * Atlassian Source Code Template.
3    * User: Bobby
4    * Date: Apr 8, 2003
5    * Time: 9:21:46 AM
6    * CVS Revision: $Revision: 1.4 $
7    * Last CVS Commit: $Date: 2006/11/03 01:52:13 $
8    * Author of last CVS Commit: $Author: jwesleysmith $
9    */
10  package com.atlassian.johnson.event;
11  
12  import java.text.SimpleDateFormat;
13  import java.util.Date;
14  import java.util.Map;
15  import java.util.HashMap;
16  import java.util.Collections;
17  
18  /**
19   * This class represents an ApplicationEvent
20   */
21  public class Event
22  {
23      private static String getFormattedDate()
24      {
25          return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
26      }
27  
28      private EventType key;
29      private String desc;
30      private String exception;
31      private EventLevel level;
32      private String date;
33      private int progress = -1;
34      private final Map attributes;
35  
36      public Event(EventType key, String desc)
37      {
38          this.key = key;
39          this.desc = desc;
40          this.date = getFormattedDate();
41          this.attributes = new HashMap();
42      }
43  
44      public Event(EventType key, String desc, String exception)
45      {
46          this.key = key;
47          this.desc = desc;
48          this.exception = exception;
49          this.date = getFormattedDate();
50          this.attributes = new HashMap();
51      }
52  
53      public Event(EventType key, String desc, EventLevel level)
54      {
55          this.key = key;
56          this.desc = desc;
57          this.level = level;
58          this.date = getFormattedDate();
59          this.attributes = new HashMap();
60      }
61  
62      public Event(EventType key, String desc, String exception, EventLevel level)
63      {
64          this.key = key;
65          this.desc = desc;
66          this.exception = exception;
67          this.level = level;
68          this.date = getFormattedDate();
69          this.attributes = new HashMap();
70      }
71  
72      public EventType getKey()
73      {
74          return key;
75      }
76  
77      public void setKey(EventType name)
78      {
79          this.key = name;
80      }
81  
82      public String getDesc()
83      {
84          return desc;
85      }
86  
87      public void setDesc(String desc)
88      {
89          this.desc = desc;
90      }
91  
92      public String getException()
93      {
94          return exception;
95      }
96  
97      public void setException(String exception)
98      {
99          this.exception = exception;
100     }
101 
102     public EventLevel getLevel()
103     {
104         return level;
105     }
106 
107     public void setLevel(EventLevel level)
108     {
109         this.level = level;
110     }
111 
112     public String getDate()
113     {
114         return date;
115     }
116 
117     public void setDate(String date)
118     {
119         this.date = date;
120     }
121 
122     public int getProgress()
123     {
124         return progress;
125     }
126 
127     public void setProgress(int progress)
128     {
129         this.progress = progress;
130     }
131 
132     public boolean hasProgress()
133     {
134         return (progress != -1);
135     }
136 
137     public void addAttribute(Object key, Object value)
138     {
139         attributes.put(key, value);
140     }
141 
142     public Object getAttribute(Object key)
143     {
144         return attributes.get(key);
145     }
146 
147     public Map getAttributes()
148     {
149         return Collections.unmodifiableMap(attributes);
150     }
151 
152     public boolean equals(Object o)
153     {
154         if (this == o) return true;
155         if (!(o instanceof Event)) return false;
156 
157         final Event event = (Event) o;
158 
159         if (date != null ? !date.equals(event.date) : event.date != null)
160         {
161             return false;
162         }
163         if (desc != null ? !desc.equals(event.desc) : event.desc != null)
164         {
165             return false;
166         }
167         if (exception != null ? !exception.equals(event.exception) : event.exception != null)
168         {
169             return false;
170         }
171         if (key != null ? !key.equals(event.key) : event.key != null)
172         {
173             return false;
174         }
175         if (level != null ? !level.equals(event.level) : event.level != null)
176         {
177             return false;
178         }
179 
180         return true;
181     }
182 
183     public int hashCode()
184     {
185         int result;
186         result = (key != null ? key.hashCode() : 0);
187         result = 29 * result + (desc != null ? desc.hashCode() : 0);
188         result = 29 * result + (exception != null ? exception.hashCode() : 0);
189         result = 29 * result + (level != null ? level.hashCode() : 0);
190         result = 29 * result + (date != null ? date.hashCode() : 0);
191         return result;
192     }
193 
194     public String toString()
195     {
196         return "Level = " + (getLevel() == null ? "" : getLevel() + " ") + ", Key = " + (getKey() == null ? "" : getKey() + " ") + ", Desc = "
197             + (getDesc() == null ? "" : getDesc() + " ") + ", Exception = " + (getException() == null ? "" : getException() + " ");
198     }
199 }