EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hiveutils.collections.impl]

COVERAGE SUMMARY FOR SOURCE FILE [QueueImpl.java]

nameclass, %method, %block, %line, %
QueueImpl.java100% (1/1)78%  (7/9)85%  (139/163)78%  (36/46)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class QueueImpl100% (1/1)78%  (7/9)85%  (139/163)78%  (36/46)
isEmpty (): boolean 0%   (0/1)0%   (0/4)0%   (0/1)
unblock (): void 0%   (0/1)0%   (0/9)0%   (0/4)
take (int): List 100% (1/1)87%  (40/46)77%  (10/13)
take (int, long): List 100% (1/1)93%  (64/69)89%  (16/18)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
QueueImpl (): void 100% (1/1)100% (14/14)100% (4/4)
add (Object): void 100% (1/1)100% (8/8)100% (3/3)
take (): List 100% (1/1)100% (4/4)100% (1/1)
take (long): List 100% (1/1)100% (5/5)100% (1/1)

1//  Copyright 2004-2007 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14 
15package net.sourceforge.hiveutils.collections.impl;
16 
17import java.util.ArrayList;
18import java.util.List;
19 
20import org.apache.commons.logging.Log;
21import org.apache.commons.logging.LogFactory;
22 
23import net.sourceforge.hiveutils.collections.Queue;
24 
25/**
26 * Implementation of <code>Queue</code> based on <code>ArrayList</code>.
27 *
28 * @author Jean-Francois Poilpret
29 */
30public class QueueImpl<T> implements Queue<T>
31{
32        static private final Log        _logger = LogFactory.getLog(QueueImpl.class);
33        
34        synchronized public boolean        isEmpty()
35        {
36                return _list.isEmpty();
37        }
38        
39        synchronized public void        add(T o)
40        {
41                _list.add(o);
42                notifyAll();
43        }
44 
45        synchronized public void        unblock()
46        {
47                if (_waiting > 0)
48                {
49                        _reset = true;
50                        notifyAll();
51                }
52        }
53        
54        public List<T>        take()
55        {
56                return take(1);
57        }
58        
59        synchronized public List<T>        take(int minCount)
60        {
61                _waiting++;
62                while ((_list.size() < minCount) && !_reset)
63                {
64                        try
65                        {
66                                wait();
67                        }
68                        catch (InterruptedException e)
69                        {
70                                _logger.warn("take", e);
71                        }
72                }
73                List<T> list = new ArrayList<T>(_list);
74                _list.clear();
75                _waiting--;
76                if (_waiting == 0)
77                {
78                        _reset = false;
79                }
80                return list;
81        }
82        
83        public List<T>        take(long timeout)
84        {
85                return take(1, timeout);
86        }
87 
88        synchronized public List<T>        take(int minCount, long timeout)
89        {
90                if (timeout <= 0)
91                {
92                        return take(minCount);
93                }
94                        
95                _waiting++;
96                long actualTimeout = timeout;
97                long maxTime = System.currentTimeMillis() + actualTimeout;
98                while ((_list.size() < minCount) && (actualTimeout > 0) && !_reset)
99                {
100                        try
101                        {
102                                wait(actualTimeout);
103                        }
104                        catch (InterruptedException e)
105                        {
106                                _logger.warn("take", e);
107                        }
108                        actualTimeout = maxTime - System.currentTimeMillis();
109                }
110                List<T> list = new ArrayList<T>(_list);
111                _list.clear();
112                _waiting--;
113                if (_waiting == 0)
114                {
115                        _reset = false;
116                }
117                return list;
118        }
119 
120        private final List<T>        _list = new ArrayList<T>();
121        private boolean                        _reset = false;
122        private int                                _waiting = 0;
123}

[all classes][net.sourceforge.hiveutils.collections.impl]
EMMA 2.0.5312 (C) Vladimir Roubtsov