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 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 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 o)
38 {
39 synchronized (list)
40 {
41 list.addLast(o);
42 list.notifyAll();
43 }
44 }
45
46 public boolean offer(Object o, int room, long timeout) throws InterruptedException
47 {
48 if (Thread.interrupted())
49 {
50 throw new InterruptedException();
51 }
52 synchronized (list)
53 {
54 if (config.capacity > 0)
55 {
56 if (config.capacity <= room)
57 {
58 throw new IllegalStateException("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 poll(long timeout) throws InterruptedException
82 {
83 if (Thread.interrupted())
84 {
85 throw new InterruptedException();
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 o = list.removeFirst();
101 list.notifyAll();
102 return o;
103 }
104 }
105
106 public Object peek() throws InterruptedException
107 {
108 if (Thread.interrupted())
109 {
110 throw new InterruptedException();
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 }