KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > messaging > memory > MemoryMessageSet


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.messaging.memory;
8
9 import java.util.Comparator JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Set JavaDoc;
12 import java.util.TreeSet JavaDoc;
13
14 import org.jboss.messaging.interfaces.Consumer;
15 import org.jboss.messaging.interfaces.MessageReference;
16 import org.jboss.messaging.interfaces.MessageSet;
17
18 import EDU.oswego.cs.dl.util.concurrent.ReentrantLock;
19
20 /**
21  * An in memory message set
22  *
23  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
24  * @version $Revision: 1.1 $
25  */

26 public class MemoryMessageSet implements MessageSet
27 {
28    // Constants -----------------------------------------------------
29

30    // Attributes ----------------------------------------------------
31

32    /** The messages */
33    private Set JavaDoc messages;
34    
35    /** The lock */
36    private ReentrantLock mutex = new ReentrantLock();
37
38    // Static --------------------------------------------------------
39

40    // Constructors --------------------------------------------------
41

42    /**
43     * Create a new MemoryMessageSet.
44     *
45     * @param comparator the comparator for the messages
46     */

47    public MemoryMessageSet(Comparator JavaDoc comparator)
48    {
49       messages = new TreeSet JavaDoc(comparator);
50    }
51    
52    // Public --------------------------------------------------------
53

54    // MessageSet implementation -------------------------------------
55

56    public void add(MessageReference reference)
57    {
58       messages.add(reference);
59    }
60
61    public MessageReference remove(Consumer consumer)
62    {
63       // Do we have a message?
64
if (messages.size() > 0)
65       {
66          for (Iterator JavaDoc iterator = messages.iterator(); iterator.hasNext();)
67          {
68             MessageReference message = (MessageReference) iterator.next();
69             if (consumer.accepts(message, true))
70             {
71                iterator.remove();
72                return message;
73             }
74          }
75       }
76       return null;
77    }
78    
79    public void lock()
80    {
81       boolean interrupted = false;
82       try
83       {
84          mutex.acquire();
85       }
86       catch (InterruptedException JavaDoc e)
87       {
88          interrupted = true;
89       }
90       if (interrupted)
91          Thread.currentThread().interrupt();
92    }
93
94    public void unlock()
95    {
96       mutex.release();
97    }
98    
99    public void setConsumer(Consumer consumer)
100    {
101       // There are no out of band notifications
102
}
103    
104    // Protected -----------------------------------------------------
105

106    // Package Private -----------------------------------------------
107

108    // Private -------------------------------------------------------
109

110    // Inner Classes -------------------------------------------------
111
}
112
Popular Tags