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