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