View Javadoc

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