2 * Copyright (c) 2010-2024 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.energidataservice.internal.retry.strategy;
15 import java.time.Duration;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.energidataservice.internal.retry.RetryStrategy;
22 * This implements a {@link RetryStrategy} for linear retry with jitter.
24 * @author Jacob Laursen - Initial contribution
27 public class Linear implements RetryStrategy {
29 private double jitter = 0.0;
30 private Duration minimum = Duration.ofMillis(100);
31 private Duration maximum = Duration.ofHours(6);
37 public Duration getDuration() {
38 long minimum = this.minimum.toMillis();
39 long maximum = this.maximum.toMillis();
40 long duration = minimum;
42 double rand = Math.random();
43 if ((((int) Math.floor(rand * 10)) & 1) == 0) {
44 duration += (long) (rand * jitter * duration);
46 duration -= (long) (rand * jitter * duration);
49 if (duration < minimum) {
52 if (duration > maximum) {
55 return Duration.ofMillis(duration);
58 public Linear withJitter(double jitter) {
63 public Linear withMinimum(Duration minimum) {
64 this.minimum = minimum;
68 public Linear withMaximum(Duration maximum) {
69 this.maximum = maximum;
74 public boolean equals(@Nullable Object o) {
78 if (!(o instanceof Linear)) {
81 Linear other = (Linear) o;
83 return this.jitter == other.jitter && this.minimum.equals(other.minimum) && this.maximum.equals(other.maximum);
87 public final int hashCode() {
90 result = prime * result + (int) jitter * 100;
91 result = prime * result + (int) minimum.toMillis();
92 result = prime * result + (int) maximum.toMillis();