package ru.kirillius.pf.sdn.core.Util;

import java.time.Duration;
import java.util.function.Supplier;

/**
 * Provides blocking wait utilities for polling boolean conditions with interruption support.
 */
public final class Wait {
    private Wait() {
        throw new AssertionError();
    }

    /**
     * Blocks until the supplied condition becomes true or the thread is interrupted.
     */
    public static void until(Supplier<Boolean> condition) throws InterruptedException {
        if (condition == null) {
            return;
        }

        while (!condition.get() && !Thread.currentThread().isInterrupted()) {
            Thread.sleep(Duration.ofSeconds(1));
        }
    }

    /**
     * Blocks while the supplied condition remains true or until interrupted.
     */
    public static void when(Supplier<Boolean> condition) throws InterruptedException {
        if (condition == null) {
            return;
        }

        while (condition.get() && !Thread.currentThread().isInterrupted()) {
            Thread.sleep(Duration.ofSeconds(1));
        }
    }


}
