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