net.yudichev.jiotty.appliance.ApplianceStatusSensor Maven / Gradle / Ivy
package net.yudichev.jiotty.appliance;
import net.yudichev.jiotty.common.lang.CompletableFutures;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.function.Predicate;
import static net.yudichev.jiotty.appliance.ApplianceStatus.IN_TRANSITION;
public interface ApplianceStatusSensor {
Duration DEFAULT_TIMEOUT = Duration.ofMinutes(2);
CompletableFuture> awaitStatus(Predicate statusPredicate, Duration timeout);
default CompletableFuture> awaitStatus(Predicate statusPredicate) {
return awaitStatus(statusPredicate, DEFAULT_TIMEOUT);
}
default CompletableFuture> awaitAnyStatus() {
return awaitStatus(ignored -> true);
}
default CompletableFuture> awaitStatus(ApplianceStatus targetStatus, Duration timeout) {
return awaitStatus(status -> status == targetStatus, timeout);
}
default CompletableFuture> awaitStatus(ApplianceStatus targetStatus) {
return awaitStatus(targetStatus, DEFAULT_TIMEOUT);
}
default CompletableFuture> awaitStableStatus() {
return awaitStatus(status -> status != IN_TRANSITION);
}
static Function, CompletableFuture> failOnTimeout() {
return applianceStatus -> applianceStatus
.map(CompletableFuture::completedFuture)
.orElse(CompletableFutures.failure("Took to long to wait for requested appliance status"));
}
}