Skip to content

Commit cc43602

Browse files
committed
default futures
1 parent d20cac5 commit cc43602

File tree

4 files changed

+201
-0
lines changed

4 files changed

+201
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.logicaldoc.core.document;
2+
3+
import java.util.concurrent.Future;
4+
5+
import com.logicaldoc.util.concurrent.FutureElaboration;
6+
7+
/**
8+
* A future used to track asynchronous elaborations on a document.
9+
*
10+
* @author Marco Meschieri - LogicalDOC
11+
* @since 9.2
12+
*/
13+
public class DocumentFuture extends FutureElaboration<Document, Document> {
14+
15+
public DocumentFuture(Document document, Future<Document> future) {
16+
super(document, future);
17+
}
18+
19+
public Document getDocument() {
20+
return super.getObject();
21+
}
22+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.logicaldoc.util.concurrent;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.Future;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.TimeoutException;
7+
8+
/**
9+
* A future that wraps an other future representing some kind of elaboration on a specified object
10+
*
11+
* @author Marco Meschieri - LogicalDOC
12+
* @since 9.2
13+
*
14+
* @param <V> Type of the wrapped future
15+
* @param <T> Type of the object being elaborated
16+
*/
17+
public class FutureElaboration<T, V> implements Future<V> {
18+
19+
private T object;
20+
21+
private Future<V> future;
22+
23+
public FutureElaboration(T object, Future<V> future) {
24+
this.object = object;
25+
this.future = future;
26+
}
27+
28+
@Override
29+
public boolean cancel(boolean mayInterruptIfRunning) {
30+
return future.cancel(mayInterruptIfRunning);
31+
}
32+
33+
@Override
34+
public boolean isCancelled() {
35+
return future.isCancelled();
36+
}
37+
38+
@Override
39+
public boolean isDone() {
40+
return future.isDone();
41+
}
42+
43+
@Override
44+
public V get() throws InterruptedException, ExecutionException {
45+
return future.get();
46+
}
47+
48+
@Override
49+
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
50+
return future.get(timeout, unit);
51+
}
52+
53+
public T getObject() {
54+
return object;
55+
}
56+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.logicaldoc.util.concurrent;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.Future;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.TimeoutException;
7+
8+
/**
9+
* A simple future that is always complete and always returns the given value
10+
*
11+
* @author Marco Meschieri - LogicalDOC
12+
* @since 9.2
13+
*
14+
* @param <V> Type of future
15+
*/
16+
public class FutureValue<V> implements Future<V> {
17+
18+
private V value;
19+
20+
public FutureValue() {
21+
super();
22+
}
23+
24+
public FutureValue(V value) {
25+
super();
26+
this.value = value;
27+
}
28+
29+
@Override
30+
public boolean cancel(boolean mayInterruptIfRunning) {
31+
return false;
32+
}
33+
34+
@Override
35+
public boolean isCancelled() {
36+
return false;
37+
}
38+
39+
@Override
40+
public boolean isDone() {
41+
return true;
42+
}
43+
44+
@Override
45+
public V get() throws InterruptedException, ExecutionException {
46+
return value;
47+
}
48+
49+
@Override
50+
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
51+
return value;
52+
}
53+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.logicaldoc.util.concurrent;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.Future;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.TimeoutException;
10+
11+
/**
12+
* A future returns null but waits for the completion of a given collection of
13+
* futures
14+
*
15+
* @author Marco Meschieri - LogicalDOC
16+
* @since 9.2
17+
*
18+
* @param <V> Type of all the futures to wait for
19+
*/
20+
public class SerialFuture<V> implements Future<V> {
21+
22+
private List<Future<V>> futures = new ArrayList<>();
23+
24+
public SerialFuture(Collection<Future<V>> futures) {
25+
for (Future<V> future : futures) {
26+
this.futures.add(future);
27+
}
28+
}
29+
30+
@Override
31+
public boolean cancel(boolean mayInterruptIfRunning) {
32+
return futures.stream().allMatch(f -> f.cancel(mayInterruptIfRunning));
33+
}
34+
35+
@Override
36+
public boolean isCancelled() {
37+
return futures.stream().allMatch(f -> f.isCancelled());
38+
}
39+
40+
@Override
41+
public boolean isDone() {
42+
return futures.stream().allMatch(f -> f.isDone());
43+
}
44+
45+
@Override
46+
public V get() throws InterruptedException, ExecutionException {
47+
return getAll().stream().findFirst().orElse(null);
48+
}
49+
50+
public List<V> getAll() throws InterruptedException, ExecutionException {
51+
List<V> gets = new ArrayList<>();
52+
for (Future<V> future : futures)
53+
gets.add(future.get());
54+
return gets;
55+
}
56+
57+
@Override
58+
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
59+
return getAll(timeout, unit).stream().findFirst().orElse(null);
60+
}
61+
62+
public List<V> getAll(long timeout, TimeUnit unit)
63+
throws InterruptedException, ExecutionException, TimeoutException {
64+
List<V> gets = new ArrayList<>();
65+
for (Future<V> future : futures)
66+
gets.add(future.get(timeout, unit));
67+
return gets;
68+
}
69+
70+
}

0 commit comments

Comments
 (0)