Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
QueueInfo |
|
| 4.0;4 |
1 | /* | |
2 | * $Id: QueueInfo.java 7963 2007-08-21 08:53:15Z dirk.olmes $ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com | |
5 | * | |
6 | * The software in this package is published under the terms of the CPAL v1.0 | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | ||
11 | package org.mule.util.queue; | |
12 | ||
13 | import java.util.LinkedList; | |
14 | ||
15 | /** | |
16 | * Stores information about a Queue | |
17 | */ | |
18 | 86 | 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 int hashCode() | |
30 | { | |
31 | 72 | return name.hashCode(); |
32 | } | |
33 | ||
34 | public void putNow(Object o) | |
35 | { | |
36 | 18 | synchronized (list) |
37 | { | |
38 | 18 | list.addLast(o); |
39 | 18 | list.notifyAll(); |
40 | 18 | } |
41 | 18 | } |
42 | ||
43 | public boolean offer(Object o, int room, long timeout) throws InterruptedException | |
44 | { | |
45 | 2070 | if (Thread.interrupted()) |
46 | { | |
47 | 0 | throw new InterruptedException(); |
48 | } | |
49 | 2070 | synchronized (list) |
50 | { | |
51 | 2070 | if (config.capacity > 0) |
52 | { | |
53 | 24 | if (config.capacity <= room) |
54 | { | |
55 | 0 | throw new IllegalStateException("Can not add more objects than the capacity in one time"); |
56 | } | |
57 | 24 | long l1 = timeout > 0L ? System.currentTimeMillis() : 0L; |
58 | 24 | long l2 = timeout; |
59 | 36 | while (list.size() >= config.capacity - room) |
60 | { | |
61 | 16 | if (l2 <= 0L) |
62 | { | |
63 | 4 | return false; |
64 | } | |
65 | 12 | list.wait(l2); |
66 | 12 | l2 = timeout - (System.currentTimeMillis() - l1); |
67 | } | |
68 | } | |
69 | 2066 | if (o != null) |
70 | { | |
71 | 2046 | list.addLast(o); |
72 | } | |
73 | 2066 | list.notifyAll(); |
74 | 2066 | return true; |
75 | 0 | } |
76 | } | |
77 | ||
78 | public Object poll(long timeout) throws InterruptedException | |
79 | { | |
80 | 2063 | if (Thread.interrupted()) |
81 | { | |
82 | 0 | throw new InterruptedException(); |
83 | } | |
84 | 2063 | synchronized (list) |
85 | { | |
86 | 2063 | long l1 = timeout > 0L ? System.currentTimeMillis() : 0L; |
87 | 2063 | long l2 = timeout; |
88 | 2087 | while (list.isEmpty()) |
89 | { | |
90 | 51 | if (l2 <= 0L) |
91 | { | |
92 | 8 | return null; |
93 | } | |
94 | 43 | list.wait(l2); |
95 | 24 | l2 = timeout - (System.currentTimeMillis() - l1); |
96 | } | |
97 | 2036 | Object o = list.removeFirst(); |
98 | 2036 | list.notifyAll(); |
99 | 2036 | return o; |
100 | 19 | } |
101 | } | |
102 | ||
103 | public Object peek() throws InterruptedException | |
104 | { | |
105 | 8 | if (Thread.interrupted()) |
106 | { | |
107 | 0 | throw new InterruptedException(); |
108 | } | |
109 | 8 | synchronized (list) |
110 | { | |
111 | 8 | if (list.isEmpty()) |
112 | { | |
113 | 4 | return null; |
114 | } | |
115 | else | |
116 | { | |
117 | 4 | return list.getFirst(); |
118 | } | |
119 | 0 | } |
120 | } | |
121 | ||
122 | } |