2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.minecraft.internal.util;
15 import java.util.concurrent.TimeUnit;
18 import rx.functions.Func1;
21 * RX operator subscribing to observable with a delay after it has finished.
23 * @author Mattias Markehed - Initial contribution
25 public class RetryWithDelay implements Func1<Observable<? extends Throwable>, Observable<?>> {
27 private final int maxRetries;
28 private final long retryDelayMillis;
29 private int retryCount;
31 public RetryWithDelay(final long retryDelay, TimeUnit unit) {
32 this(-1, TimeUnit.MILLISECONDS.convert(retryDelay, unit));
35 public RetryWithDelay(final int maxRetries, final long retryDelay, TimeUnit unit) {
36 this(maxRetries, TimeUnit.MILLISECONDS.convert(retryDelay, unit));
39 private RetryWithDelay(final int maxRetries, final long retryDelayMillis) {
40 this.maxRetries = maxRetries;
41 this.retryDelayMillis = retryDelayMillis;
46 public Observable<?> call(Observable<? extends Throwable> attempts) {
47 return attempts.flatMap(new Func1<Throwable, Observable<?>>() {
49 public Observable<?> call(Throwable throwable) {
50 if (maxRetries < 0 || ++retryCount < maxRetries) {
51 return Observable.timer(retryDelayMillis, TimeUnit.MILLISECONDS);
54 return Observable.error(throwable);