All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jpower.core.ConditionalExecutor Maven / Gradle / Ivy

There is a newer version: 0.0.5
Show newest version
package jpower.core;

import jpower.core.utils.ThreadUtils;

import java.util.concurrent.TimeUnit;

public class ConditionalExecutor
{

    private final Runnable task;

    public ConditionalExecutor(Runnable task)
    {
        this.task = task;
    }

    public void until(Condition condition)
    {
        while (condition.inverted())
        {
            task.run();
        }
    }

    public void when(Condition condition)
    {
        while (condition.check())
        {
            task.run();
        }
    }

    public void intervalUntil(long amount, TimeUnit unit, Condition condition)
    {
        while (condition.inverted())
        {
            task.run();
            ThreadUtils.sleep(unit.toMillis(amount));
        }
    }

    public void intervalWhen(long amount, TimeUnit unit, Condition condition)
    {
        while (condition.check())
        {
            task.run();
            ThreadUtils.sleep(unit.toMillis(amount));
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy