]> git.basschouten.com Git - openhab-addons.git/blob
07b92da6f8c1c8540f9089214d2bde7200c6b148
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.energidataservice.internal.retry.strategy;
14
15 import java.time.Duration;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.energidataservice.internal.retry.RetryStrategy;
20
21 /**
22  * This implements a {@link RetryStrategy} for linear retry with jitter.
23  *
24  * @author Jacob Laursen - Initial contribution
25  */
26 @NonNullByDefault
27 public class Linear implements RetryStrategy {
28
29     private double jitter = 0.0;
30     private Duration minimum = Duration.ofMillis(100);
31     private Duration maximum = Duration.ofHours(6);
32
33     public Linear() {
34     }
35
36     @Override
37     public Duration getDuration() {
38         long minimum = this.minimum.toMillis();
39         long maximum = this.maximum.toMillis();
40         long duration = minimum;
41         if (jitter != 0.0) {
42             double rand = Math.random();
43             if ((((int) Math.floor(rand * 10)) & 1) == 0) {
44                 duration += (long) (rand * jitter * duration);
45             } else {
46                 duration -= (long) (rand * jitter * duration);
47             }
48         }
49         if (duration < minimum) {
50             duration = minimum;
51         }
52         if (duration > maximum) {
53             duration = maximum;
54         }
55         return Duration.ofMillis(duration);
56     }
57
58     public Linear withJitter(double jitter) {
59         this.jitter = jitter;
60         return this;
61     }
62
63     public Linear withMinimum(Duration minimum) {
64         this.minimum = minimum;
65         return this;
66     }
67
68     public Linear withMaximum(Duration maximum) {
69         this.maximum = maximum;
70         return this;
71     }
72
73     @Override
74     public boolean equals(@Nullable Object o) {
75         if (o == this) {
76             return true;
77         }
78         if (!(o instanceof Linear)) {
79             return false;
80         }
81         Linear other = (Linear) o;
82
83         return this.jitter == other.jitter && this.minimum.equals(other.minimum) && this.maximum.equals(other.maximum);
84     }
85
86     @Override
87     public final int hashCode() {
88         final int prime = 31;
89         int result = 1;
90         result = prime * result + (int) jitter * 100;
91         result = prime * result + (int) minimum.toMillis();
92         result = prime * result + (int) maximum.toMillis();
93
94         return result;
95     }
96 }