forked from gooddata/gooddata-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFutureResult.java
More file actions
65 lines (55 loc) · 1.88 KB
/
FutureResult.java
File metadata and controls
65 lines (55 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
*/
package com.gooddata;
import java.util.concurrent.TimeUnit;
import static com.gooddata.Validate.notNull;
/**
* Represents the result retrieved by polling on the REST API.
*/
public final class FutureResult<T> {
private final AbstractService service;
private final PollHandler<T> handler;
/**
* Creates a new instance of the result to be eventually retrieved by polling on the REST API.<p>
* For internal usage by services employing polling.
* @param service this service
* @param handler poll handler
*/
public FutureResult(final AbstractService service, final PollHandler<T> handler) {
this.service = notNull(service, "service");
this.handler = notNull(handler, "handler");
}
/**
* Checks if the result is available
* @return true if so
* @throws GoodDataException when polling fails or the thread was interrupted
*/
public boolean isDone() {
return handler.isDone() || service.pollOnce(handler);
}
/**
* Wait for the result to be available and return it's value
* @return result value
* @throws GoodDataException when polling fails or the thread was interrupted
*/
public T get() {
return get(0, null);
}
/**
* Wait for the result to be available up to given time and return it's value
* @param timeout timeout value
* @param unit timeout unit
* @return result value
* @throws GoodDataException when polling fails, the timeout expires or the thread was interrupted
*/
public T get(final long timeout, final TimeUnit unit) {
if (handler.isDone()) {
return handler.getResult();
}
return service.poll(handler, timeout, unit);
}
public String getPollingUri() {
return handler.getPollingUri();
}
}