io.github.sashirestela.openai.support.Poller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simple-openai Show documentation
Show all versions of simple-openai Show documentation
A Java library to use the OpenAI API in the simplest possible way.
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;
}
}