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