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