1 package net.sf.lightair.internal;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5
6 import java.util.concurrent.Callable;
7
8
9
10
11 public class Awaiting {
12
13 private static final Logger log = LoggerFactory.getLogger(Awaiting.class);
14
15 private static final long DELAY_BETWEEN_CALLS = 20;
16
17 private static void sleep() {
18 try {
19 Thread.sleep(DELAY_BETWEEN_CALLS);
20 } catch (InterruptedException e) {
21 log.error("Await interrupted.", e);
22 }
23 }
24
25 private static <T> T resolveCallable(Callable<T> callable) {
26 try {
27 return callable.call();
28 } catch (Exception e) {
29 throw new RuntimeException("Error calling function.", e);
30 }
31 }
32
33 public static String awaitEmpty(long limit, Callable<String> function) {
34 long start = System.currentTimeMillis();
35 String result = resolveCallable(function);
36 while (!result.isEmpty() && (System.currentTimeMillis() - start <= limit)) {
37 sleep();
38 result = resolveCallable(function);
39 }
40 return result;
41 }
42 }