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

io.github.sashirestela.openai.support.Poller Maven / Gradle / Ivy

There is a newer version: 3.9.2
Show newest version
package io.github.sashirestela.openai.support;

import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;

import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

@Builder
@Getter
public class Poller {

    @NonNull
    private TimeUnit timeUnit;

    @NonNull
    private Integer timeValue;

    @NonNull
    private UnaryOperator queryMethod;

    @NonNull
    private Predicate whileMethod;

    public T execute(T startValue) {
        T object = startValue;
        do {
            try {
                timeUnit.sleep(timeValue.longValue());
            } catch (InterruptedException e) {
                java.lang.Thread.currentThread().interrupt();
            }
            object = queryMethod.apply(object);
        } while (whileMethod.test(object));
        return object;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy