View Javadoc

1   package cz.cuni.amis.utils.future;
2   
3   import java.util.concurrent.Future;
4   import java.util.concurrent.TimeUnit;
5   
6   import cz.cuni.amis.utils.NullCheck;
7   import cz.cuni.amis.utils.TimeUnitToMillis;
8   import cz.cuni.amis.utils.exception.PogamutTimeoutException;
9   
10  /**
11   * Used to combine multiple Future<Boolean> together into one Future<Boolean>.
12   * <p><p>
13   * During construction of the instance of this class you have to specify an array of
14   * booleans you want to combine - then all methods will wait for all futures to end
15   * (e.g. get(), etc.).
16   * <p><p>
17   * Note that you will probably want to use getAll() method to get respective Future results.
18   * 
19   * @author Jimmy
20   *
21   */
22  public class CombinedBooleanFuture implements IFuture<Boolean> {
23  	
24  	private IFuture<Boolean>[] futures;
25  	
26  	public CombinedBooleanFuture(IFuture<Boolean>[] futures) {
27  		this.futures = futures;
28  		NullCheck.check(this.futures, "futures");
29  	}
30  
31  	@Override
32  	public boolean cancel(boolean mayInterruptIfRunning) {
33  		boolean canceled = true;
34  		for (Future<Boolean> future : futures) {
35  			canceled = canceled && future.cancel(mayInterruptIfRunning);
36  		}
37  		return canceled;
38  	}
39  
40  	@Override
41  	public Boolean get() {
42  		boolean result = true;
43  		for (IFuture<Boolean> future : futures) {
44  			Boolean futureResult = future.get(); 
45  			result = result && (futureResult != null ? futureResult : false);
46  		}
47  		return result;
48  	}
49  
50  	@Override
51  	public Boolean get(long timeout, TimeUnit unit) {
52  		long timeoutMillis = TimeUnitToMillis.toMillis(timeout, unit);
53  		long start = System.currentTimeMillis();
54  		boolean result = true;
55  		for (IFuture<Boolean> future : futures) {
56  			long futureStart = System.currentTimeMillis();
57  			if (futureStart - start > timeoutMillis) throw new PogamutTimeoutException("timeouted after " + timeout + " " + unit, this);
58  			Boolean futureResult = future.get(timeoutMillis - (futureStart - start), TimeUnit.MILLISECONDS);
59  			result = result && (futureResult != null ? futureResult : false);
60  		}
61  		return result;
62  	}
63  	
64  	public Boolean[] getAll(long timeout, TimeUnit unit) {
65  		long timeoutMillis = TimeUnitToMillis.toMillis(timeout, unit);
66  		long start = System.currentTimeMillis();
67  		Boolean[] results = new Boolean[futures.length];
68  		int i = 0;
69  		for (IFuture<Boolean> future : futures) {
70  			long futureStart = System.currentTimeMillis();
71  			if (futureStart - start > timeoutMillis) throw new PogamutTimeoutException("timeouted after " + timeout + " " + unit, this);
72  			results[i++] = future.get(timeoutMillis - (futureStart - start), TimeUnit.MILLISECONDS);
73  		}
74  		return results;
75  	} 
76  
77  	@Override
78  	public boolean isCancelled() {
79  		boolean result = false;
80  		for (Future<Boolean> future : futures) {
81  			Boolean futureResult = future.isCancelled(); 
82  			result = result || (futureResult != null ? futureResult : false);			
83  		}
84  		return result;
85  	}
86  
87  	@Override
88  	public boolean isDone() {
89  		boolean result = true;
90  		for (Future<Boolean> future : futures) {			
91  			result = result && future.isDone();
92  		}
93  		return result;
94  	}
95  	
96  	public Future<Boolean>[] getFutures() {
97  		return futures;
98  	}
99  
100 }