KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > inbound > EventGroup


1 /*
2  * $Id: EventGroup.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.routing.inbound;
12
13 import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
14
15 import java.io.Serializable JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.mule.umo.UMOEvent;
20
21 /**
22  * <code>EventGroup</code> is a holder over events grouped by a common group Id.
23  * This can be used by components such as routers to managed related events.
24  */

25 // @ThreadSafe
26
public class EventGroup implements Serializable JavaDoc
27 {
28     /**
29      * Serial version
30      */

31     private static final long serialVersionUID = -7337182983687406403L;
32
33     private final Object JavaDoc groupId;
34     private final List events;
35     private final long created;
36     private final int expectedSize;
37
38     public EventGroup(Object JavaDoc groupId)
39     {
40         this(groupId, -1);
41     }
42
43     public EventGroup(Object JavaDoc groupId, int expectedSize)
44     {
45         super();
46         this.created = System.currentTimeMillis();
47         this.events = new CopyOnWriteArrayList();
48         this.expectedSize = expectedSize;
49         this.groupId = groupId;
50     }
51
52     public Object JavaDoc getGroupId()
53     {
54         return groupId;
55     }
56
57     public Iterator JavaDoc iterator()
58     {
59         return events.iterator();
60     }
61
62     public void addEvent(UMOEvent event)
63     {
64         events.add(event);
65     }
66
67     public void removeEvent(UMOEvent event)
68     {
69         events.remove(event);
70     }
71
72     public long getCreated()
73     {
74         return created;
75     }
76
77     public int size()
78     {
79         return events.size();
80     }
81
82     public void clear()
83     {
84         events.clear();
85     }
86
87     public int expectedSize()
88     {
89         return expectedSize;
90     }
91
92     public String JavaDoc toString()
93     {
94         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(80);
95         buf.append("Event Group Id=").append(groupId);
96         buf.append(", expected size=").append(expectedSize);
97
98         // COWArrayList synchronizes on itself so we can use that to prevent changes
99
// to the group while we iterate over it. This is only necessary to prevent
100
// output with size=1 and then printing 2 or more events because someone
101
// snuck in behind our back..
102
synchronized (events)
103         {
104             int currentSize = events.size();
105             buf.append(", current events=").append(currentSize);
106
107             if (currentSize > 0)
108             {
109                 buf.append(" [");
110                 Iterator JavaDoc i = events.iterator();
111                 while (i.hasNext())
112                 {
113                     UMOEvent event = (UMOEvent)i.next();
114                     buf.append(event.getMessage().getUniqueId());
115                     if (i.hasNext())
116                     {
117                         buf.append(", ");
118                     }
119                 }
120                 buf.append(']');
121             }
122             return buf.toString();
123         }
124     }
125
126 }
127
Popular Tags