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