EMMA Coverage Report (generated Tue Apr 17 08:51:20 BST 2007)
[all classes][org.jtoolkit.essence.concurrency.impl]

COVERAGE SUMMARY FOR SOURCE FILE [CallbackQueue.java]

nameclass, %method, %block, %line, %
CallbackQueue.java100% (1/1)56%  (5/9)70%  (118/169)74%  (32.4/44)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CallbackQueue100% (1/1)56%  (5/9)70%  (118/169)74%  (32.4/44)
close (): void 0%   (0/1)0%   (0/9)0%   (0/3)
isClosed (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
onException (Throwable): void 0%   (0/1)0%   (0/8)0%   (0/3)
toString (): String 0%   (0/1)0%   (0/10)0%   (0/1)
checkClosed (): void 100% (1/1)44%  (4/9)68%  (1.4/2)
take (Timeout): Object 100% (1/1)70%  (37/53)75%  (9/12)
CallbackQueue (): void 100% (1/1)100% (11/11)100% (3/3)
drain (int, Timeout): List 100% (1/1)100% (58/58)100% (16/16)
onCallback (Object): void 100% (1/1)100% (8/8)100% (3/3)

1package org.jtoolkit.essence.concurrency.impl;
2 
3import org.jetbrains.annotations.NotNull;
4import org.jtoolkit.essence.concurrency.CallbackEx;
5import org.jtoolkit.essence.concurrency.Timeout;
6import org.jtoolkit.essence.utils.Closeable;
7 
8import java.util.ArrayList;
9import java.util.List;
10import java.util.concurrent.*;
11 
12/*
13   Copyright 2006 Peter Lawrey
14 
15   Licensed under the Apache License, Version 2.0 (the "License");
16   you may not use this file except in compliance with the License.
17   You may obtain a copy of the License at
18 
19       http://www.apache.org/licenses/LICENSE-2.0
20 
21   Unless required by applicable law or agreed to in writing, software
22   distributed under the License is distributed on an "AS IS" BASIS,
23   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24   See the License for the specific language governing permissions and
25   limitations under the License.
26*/
27 
28/**
29 * @author Peter Lawrey
30 */
31public class CallbackQueue<E> implements CallbackEx<E>, Closeable {
32    private final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
33    private boolean closed = false;
34 
35    public void onCallback(@NotNull E element) throws IllegalStateException {
36        checkClosed();
37        queue.add(element);
38    }
39 
40    public void onException(@NotNull Throwable t) throws IllegalStateException {
41        checkClosed();
42        queue.add(t);
43    }
44 
45    public E take(Timeout timeout) throws InterruptedException, TimeoutException, ExecutionException {
46        checkClosed();
47        Object element;
48        if (timeout.isNever())
49            element = queue.take();
50        else {
51            long timeout2 = timeout.getMillisLeft();
52            if (timeout2 <= 0)
53                element = queue.poll();
54            else
55                element = queue.poll(timeout2, TimeUnit.MILLISECONDS);
56        }
57        if (element == null)
58            throw new TimeoutException("After " + timeout);
59 
60        if (element instanceof Throwable)
61            throw new ExecutionException((Throwable) element);
62        return (E) element;
63    }
64 
65    private void checkClosed() {
66        if (closed) throw new IllegalStateException(CLOSED);
67    }
68 
69    public List drain(int maxCount, Timeout timeout) throws InterruptedException {
70//        long start = System.nanoTime();
71        List ret = new ArrayList();
72        queue.drainTo(ret, maxCount);
73        while (!closed && ret.size() < maxCount) {
74            long millisLeft = timeout.getMillisLeft();
75            if (millisLeft < 0) break;
76            Object element2;
77            if (millisLeft == 0) {
78                element2 = queue.poll();
79                if (element2 == null) {
80                    // noinspection CallToThreadYield
81                    Thread.yield();
82                    element2 = queue.poll();
83                }
84                if (element2 == null) break;
85            } else {
86                element2 = queue.poll(millisLeft, TimeUnit.MILLISECONDS);
87            }
88            if (element2 != null)
89                ret.add(element2);
90        }
91        return ret;
92    }
93 
94    public String toString() {
95        return "CallbackQueue " + queue;
96    }
97 
98    public void close() {
99        closed = true;
100        queue.add(null);
101    }
102 
103    public boolean isClosed() {
104        return closed;
105    }
106}

[all classes][org.jtoolkit.essence.concurrency.impl]
EMMA 2.0.5312 (C) Vladimir Roubtsov