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