EMMA Coverage Report (generated Thu Jan 24 13:37:04 CST 2013)
[all classes][org.springframework.batch.poller]

COVERAGE SUMMARY FOR SOURCE FILE [DirectPoller.java]

nameclass, %method, %block, %line, %
DirectPoller.java100% (2/2)50%  (4/8)69%  (98/143)73%  (27.7/38)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DirectPoller$DirectPollingFuture100% (1/1)33%  (2/6)65%  (85/130)70%  (23.7/34)
cancel (boolean): boolean 0%   (0/1)0%   (0/5)0%   (0/2)
get (): Object 0%   (0/1)0%   (0/12)0%   (0/3)
isCancelled (): boolean 0%   (0/1)0%   (0/3)0%   (0/1)
isDone (): boolean 0%   (0/1)0%   (0/10)0%   (0/1)
get (long, TimeUnit): Object 100% (1/1)82%  (70/85)84%  (17.7/21)
DirectPoller$DirectPollingFuture (long, Callable): void 100% (1/1)100% (15/15)100% (6/6)
     
class DirectPoller100% (1/1)100% (2/2)100% (13/13)100% (4/4)
DirectPoller (long): void 100% (1/1)100% (6/6)100% (3/3)
poll (Callable): Future 100% (1/1)100% (7/7)100% (1/1)

1/*
2 * Copyright 2006-2010 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.springframework.batch.poller;
17 
18import java.util.concurrent.Callable;
19import java.util.concurrent.ExecutionException;
20import java.util.concurrent.Future;
21import java.util.concurrent.TimeUnit;
22import java.util.concurrent.TimeoutException;
23 
24/**
25 * A {@link Poller} that uses the callers thread to poll for a result as soon as
26 * it is asked for. This is often appropriate if you expect a result relatively
27 * quickly, or if there is only one such result expected (otherwise it is more
28 * efficient to use a background thread to do the polling).
29 * 
30 * @author Dave Syer
31 * 
32 * @param <S> the type of the result
33 */
34public class DirectPoller<S> implements Poller<S> {
35 
36        private final long interval;
37 
38        public DirectPoller(long interval) {
39                this.interval = interval;
40        }
41 
42        /**
43         * Get a future for a non-null result from the callback. Only when the
44         * result is asked for (using {@link Future#get()} or
45         * {@link Future#get(long, TimeUnit)} will the polling actually start.
46         * 
47         * @see Poller#poll(Callable)
48         */
49        public Future<S> poll(Callable<S> callable) throws Exception {
50                return new DirectPollingFuture<S>(interval, callable);
51        }
52 
53        private static class DirectPollingFuture<S> implements Future<S> {
54 
55                private final long startTime = System.currentTimeMillis();
56 
57                private volatile boolean cancelled;
58 
59                private volatile S result = null;
60 
61                private final long interval;
62 
63                private final Callable<S> callable;
64 
65                public DirectPollingFuture(long interval, Callable<S> callable) {
66                        this.interval = interval;
67                        this.callable = callable;
68                }
69 
70                public boolean cancel(boolean mayInterruptIfRunning) {
71                        cancelled = true;
72                        return true;
73                }
74 
75                public S get() throws InterruptedException, ExecutionException {
76                        try {
77                                return get(-1, TimeUnit.MILLISECONDS);
78                        }
79                        catch (TimeoutException e) {
80                                throw new IllegalStateException("Unexpected timeout waiting for result", e);
81                        }
82                }
83 
84                public S get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
85 
86                        try {
87                                result = callable.call();
88                        }
89                        catch (Exception e) {
90                                throw new ExecutionException(e);
91                        }
92 
93                        Long nextExecutionTime = startTime + interval;
94                        long currentTimeMillis = System.currentTimeMillis();
95                        long timeoutMillis = TimeUnit.MILLISECONDS.convert(timeout, unit);
96 
97                        while (result == null && !cancelled) {
98 
99                                long delta = nextExecutionTime - startTime;
100                                if (delta >= timeoutMillis && timeoutMillis > 0) {
101                                        throw new TimeoutException("Timed out waiting for task to return non-null result");
102                                }
103 
104                                if (nextExecutionTime > currentTimeMillis) {
105                                        Thread.sleep(nextExecutionTime - currentTimeMillis);
106                                }
107 
108                                currentTimeMillis = System.currentTimeMillis();
109                                nextExecutionTime = currentTimeMillis + interval;
110 
111                                try {
112                                        result = callable.call();
113                                }
114                                catch (Exception e) {
115                                        throw new ExecutionException(e);
116                                }
117 
118                        }
119 
120                        return result;
121 
122                }
123 
124                public boolean isCancelled() {
125                        return cancelled;
126                }
127 
128                public boolean isDone() {
129                        return cancelled || result != null;
130                }
131 
132        }
133 
134}

[all classes][org.springframework.batch.poller]
EMMA 2.0.5312 (C) Vladimir Roubtsov