KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > util > queue > QueueInfo


1 /*
2  * $Id: QueueInfo.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.util.queue;
12
13 import java.util.LinkedList JavaDoc;
14
15 /**
16  * Stores information about a Queue
17  *
18  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
19  * @version $Revision: 3798 $
20  */

21 public class QueueInfo
22 {
23     protected LinkedList JavaDoc list;
24     protected String JavaDoc name;
25     protected QueueConfiguration config;
26
27     public boolean equals(Object JavaDoc obj)
28     {
29         return (obj instanceof QueueInfo && name.equals(((QueueInfo)obj).name));
30     }
31
32     public int hashCode()
33     {
34         return name.hashCode();
35     }
36
37     public void putNow(Object JavaDoc o)
38     {
39         synchronized (list)
40         {
41             list.addLast(o);
42             list.notifyAll();
43         }
44     }
45
46     public boolean offer(Object JavaDoc o, int room, long timeout) throws InterruptedException JavaDoc
47     {
48         if (Thread.interrupted())
49         {
50             throw new InterruptedException JavaDoc();
51         }
52         synchronized (list)
53         {
54             if (config.capacity > 0)
55             {
56                 if (config.capacity <= room)
57                 {
58                     throw new IllegalStateException JavaDoc("Can not add more objects than the capacity in one time");
59                 }
60                 long l1 = timeout > 0L ? System.currentTimeMillis() : 0L;
61                 long l2 = timeout;
62                 while (list.size() >= config.capacity - room)
63                 {
64                     if (l2 <= 0L)
65                     {
66                         return false;
67                     }
68                     list.wait(l2);
69                     l2 = timeout - (System.currentTimeMillis() - l1);
70                 }
71             }
72             if (o != null)
73             {
74                 list.addLast(o);
75             }
76             list.notifyAll();
77             return true;
78         }
79     }
80
81     public Object JavaDoc poll(long timeout) throws InterruptedException JavaDoc
82     {
83         if (Thread.interrupted())
84         {
85             throw new InterruptedException JavaDoc();
86         }
87         synchronized (list)
88         {
89             long l1 = timeout > 0L ? System.currentTimeMillis() : 0L;
90             long l2 = timeout;
91             while (list.isEmpty())
92             {
93                 if (l2 <= 0L)
94                 {
95                     return null;
96                 }
97                 list.wait(l2);
98                 l2 = timeout - (System.currentTimeMillis() - l1);
99             }
100             Object JavaDoc o = list.removeFirst();
101             list.notifyAll();
102             return o;
103         }
104     }
105
106     public Object JavaDoc peek() throws InterruptedException JavaDoc
107     {
108         if (Thread.interrupted())
109         {
110             throw new InterruptedException JavaDoc();
111         }
112         synchronized (list)
113         {
114             if (list.isEmpty())
115             {
116                 return null;
117             }
118             else
119             {
120                 return list.getFirst();
121             }
122         }
123     }
124
125 }
126
Popular Tags