View Javadoc
1   package net.sf.lightair.internal;
2   
3   import java.util.concurrent.Callable;
4   
5   /**
6    * Helper functions for awaiting target database state.
7    */
8   public class Awaiting {
9   
10  	private static final long DELAY_BETWEEN_CALLS = 20;
11  
12  	private static void sleep() {
13  		try {
14  			Thread.sleep(DELAY_BETWEEN_CALLS);
15  		} catch (InterruptedException e) {
16  			e.printStackTrace();
17  		}
18  	}
19  
20  	private static <T> T resolveCallable(Callable<T> callable) {
21  		try {
22  			return callable.call();
23  		} catch (Exception e) {
24  			throw new RuntimeException("Error calling function.", e);
25  		}
26  	}
27  
28  	public static String awaitEmpty(long limit, Callable<String> function) {
29  		long start = System.currentTimeMillis();
30  		String result = resolveCallable(function);
31  		while (!result.isEmpty() && (System.currentTimeMillis() - start <= limit)) {
32  			sleep();
33  			result = resolveCallable(function);
34  		}
35  		return result;
36  	}
37  }