
com.github.nginate.commons.lang.await.Await Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-lang Show documentation
Show all versions of commons-lang Show documentation
Misc utilities to hide most common boilerplate code. Requires jdk8 at least
The newest version!
/**
* Copyright © 2016
* Maksim Lozbin
* Oleksii Ihnachuk
*
* This work is free. You can redistribute it and/or modify it under the
* terms of the Do What The Fuck You Want To Public License, Version 2,
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
*/
package com.github.nginate.commons.lang.await;
import com.google.common.base.Preconditions;
import com.google.common.base.Stopwatch;
import lombok.experimental.UtilityClass;
import java.util.concurrent.Callable;
import static com.google.common.base.Throwables.propagate;
import static java.lang.Thread.sleep;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
/**
* Better waiting for required periodically polling for result and not just sleeping for some hardcoded period
*
* @since 1.0
*/
@UtilityClass
public final class Await {
/**
* Default interval for conditions checks
*/
private static final int DEFAULT_STEP_MILLIS = 200;
/**
* Wait till the condition will become true during at most {@code timeout} millis. Will be checking the condition
* each {@link Await#DEFAULT_STEP_MILLIS} millis.
*
* @param timeout max wait millis
* @param condition required condition
* @throws ConditionTimeoutException if condition was not satisfied in configured period
* @see Await#waitUntil(int, int, Callable)
*/
public static void waitUntil(int timeout, Callable condition) {
waitUntil(timeout, DEFAULT_STEP_MILLIS, condition);
}
/**
* Wait till the condition will become true during at most {@code timeout} millis. Will be checking the condition
* each {@code waitStepMillis} millis.
*
* @param timeout max wait millis
* @param waitStepMillis step wait millis
* @param condition required condition
* @throws ConditionTimeoutException if condition was not satisfied in configured period
* @throws IllegalArgumentException if step millis are more or equal to overall wait millis
* @see Await#waitUntil(int, int, String, Callable)
*/
public static void waitUntil(int timeout, int waitStepMillis, Callable condition) {
waitUntil(timeout, waitStepMillis, "Waiting for condition timed out", condition);
}
/**
* Wait till the condition will become true during at most {@code timeout} millis. Will be checking the condition
* each {@link Await#DEFAULT_STEP_MILLIS} millis.
*
* @param timeout max wait millis
* @param failureMessage message to see if waiting fails
* @param condition required condition
* @throws ConditionTimeoutException if condition was not satisfied in configured period
* @throws IllegalArgumentException if step millis are more or equal to overall wait millis
* @see Await#waitUntil(int, int, String, Callable)
*/
public static void waitUntil(int timeout, String failureMessage, Callable condition) {
waitUntil(timeout, DEFAULT_STEP_MILLIS, failureMessage, condition);
}
/**
* Wait till the condition will become true during at most {@code timeout} millis. Will be checking the condition
* each {@code waitStepMillis} millis.
*
* @param timeout max wait millis
* @param waitStepMillis step wait millis
* @param failureMessage message to see if waiting fails
* @param condition required condition
* @throws ConditionTimeoutException if condition was not satisfied in configured period
* @throws IllegalArgumentException if step millis are more or equal to overall wait millis
*/
public static void waitUntil(int timeout, int waitStepMillis, String failureMessage, Callable condition) {
Preconditions.checkArgument(waitStepMillis > 0, "step sleep time should be positive");
Preconditions.checkArgument(waitStepMillis <= timeout, "step sleep time must be less or equal to timeout");
Stopwatch stopwatch = Stopwatch.createStarted();
while (stopwatch.elapsed(MILLISECONDS) < timeout) {
try {
if (condition.call()) {
return;
}
sleep(waitStepMillis);
} catch (Exception e) {
throw propagate(e);
}
}
throw new ConditionTimeoutException(failureMessage);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy