1
2
3
4
5
6
7
8
9
10
11 package org.mule.routing.inbound;
12
13 import org.mule.umo.UMOEvent;
14 import org.mule.util.ClassUtils;
15
16 import java.io.Serializable;
17 import java.util.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20
21 import edu.emory.mathcs.backport.java.util.concurrent.helpers.Utils;
22 import org.apache.commons.collections.IteratorUtils;
23
24
25
26
27
28
29 public class EventGroup implements Comparable, Serializable
30 {
31
32
33
34 private static final long serialVersionUID = 953739659615692697L;
35
36 public static final UMOEvent[] EMPTY_EVENTS_ARRAY = new UMOEvent[0];
37
38 private final Object groupId;
39
40 private final List events;
41 private final long created;
42 private final int expectedSize;
43
44 public EventGroup(Object groupId)
45 {
46 this(groupId, -1);
47 }
48
49 public EventGroup(Object groupId, int expectedSize)
50 {
51 super();
52 this.created = Utils.nanoTime();
53 this.events = new ArrayList(expectedSize > 0 ? expectedSize : 10);
54 this.expectedSize = expectedSize;
55 this.groupId = groupId;
56 }
57
58
59
60
61
62
63
64
65
66 public int compareTo(Object o)
67 {
68 EventGroup other = (EventGroup) o;
69 Object otherId = other.getGroupId();
70
71 if (groupId instanceof Comparable && otherId instanceof Comparable)
72 {
73 return ((Comparable) groupId).compareTo(otherId);
74 }
75 else
76 {
77 long diff = created - other.getCreated();
78 return (diff > 0 ? 1 : (diff < 0 ? -1 : 0));
79 }
80 }
81
82
83
84
85
86
87
88
89 public boolean equals(Object obj)
90 {
91 if (this == obj)
92 {
93 return true;
94 }
95
96 if (!(obj instanceof EventGroup))
97 {
98 return false;
99 }
100
101 final EventGroup other = (EventGroup) obj;
102 if (groupId == null)
103 {
104 return (other.groupId == null);
105 }
106
107 return groupId.equals(other.groupId);
108 }
109
110
111
112
113
114
115
116
117 public int hashCode()
118 {
119 return groupId.hashCode();
120 }
121
122
123
124
125
126
127
128 public Object getGroupId()
129 {
130 return groupId;
131 }
132
133
134
135
136
137
138
139
140
141
142 public Iterator iterator()
143 {
144 synchronized (this)
145 {
146 if (events.isEmpty())
147 {
148 return IteratorUtils.emptyIterator();
149 }
150 else
151 {
152 return IteratorUtils.arrayIterator(this.toArray());
153 }
154 }
155 }
156
157
158
159
160
161
162 public UMOEvent[] toArray()
163 {
164 synchronized (this)
165 {
166 if (events.isEmpty())
167 {
168 return EMPTY_EVENTS_ARRAY;
169 }
170
171 return (UMOEvent[]) events.toArray(EMPTY_EVENTS_ARRAY);
172 }
173 }
174
175
176
177
178
179
180 public void addEvent(UMOEvent event)
181 {
182 synchronized (this)
183 {
184 events.add(event);
185 }
186 }
187
188
189
190
191
192
193 public void removeEvent(UMOEvent event)
194 {
195 synchronized (this)
196 {
197 events.remove(event);
198 }
199 }
200
201
202
203
204
205
206
207 public long getCreated()
208 {
209 return created;
210 }
211
212
213
214
215
216
217 public int size()
218 {
219 synchronized (this)
220 {
221 return events.size();
222 }
223 }
224
225
226
227
228
229
230
231 public int expectedSize()
232 {
233 return expectedSize;
234 }
235
236
237
238
239 public void clear()
240 {
241 synchronized (this)
242 {
243 events.clear();
244 }
245 }
246
247
248 public String toString()
249 {
250 StringBuffer buf = new StringBuffer(80);
251 buf.append(ClassUtils.getSimpleName(this.getClass()));
252 buf.append(" {");
253 buf.append("id=").append(groupId);
254 buf.append(", expected size=").append(expectedSize);
255
256 synchronized (this)
257 {
258 int currentSize = events.size();
259 buf.append(", current events=").append(currentSize);
260
261 if (currentSize > 0)
262 {
263 buf.append(" [");
264 Iterator i = events.iterator();
265 while (i.hasNext())
266 {
267 UMOEvent event = (UMOEvent) i.next();
268 buf.append(event.getMessage().getUniqueId());
269 if (i.hasNext())
270 {
271 buf.append(", ");
272 }
273 }
274 buf.append(']');
275 }
276 }
277
278 buf.append('}');
279
280 return buf.toString();
281 }
282
283 }