1 package org.davidmoten.kool;
2
3 import java.util.concurrent.TimeUnit;
4
5 import org.davidmoten.kool.function.Predicate;
6 import org.davidmoten.kool.internal.operators.stream.RetryWhen;
7
8 public final class RetryWhenBuilderSingle<T> {
9
10 private final Single<T> single;
11 private Stream<Long> delays;
12 private int maxRetries;
13 private Predicate<? super Throwable> predicate;
14
15 public RetryWhenBuilderSingle(Single<T> single) {
16 this.single = single;
17 }
18
19 public RetryWhenBuilderSingle<T> delay(long duration, TimeUnit unit) {
20 this.delays = Single.of(unit.toMillis(duration)).repeat();
21 return this;
22 }
23
24 public RetryWhenBuilderSingle<T> maxRetries(int maxRetries) {
25 this.maxRetries = maxRetries;
26 return this;
27 }
28
29 public RetryWhenBuilderSingle<T> delays(Stream<Long> delays, TimeUnit unit) {
30 this.delays = delays.map(x -> unit.toMillis(x));
31 return this;
32 }
33
34 public RetryWhenBuilderSingle<T> isTrue(Predicate<? super Throwable> predicate) {
35 this.predicate = predicate;
36 return this;
37 }
38
39 public Single<T> build() {
40 return RetryWhen.build(single.toStream(), delays, maxRetries, predicate).single();
41 }
42
43 }