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.energidataservice.internal.retry.strategy;
15 import java.time.Clock;
16 import java.time.Duration;
17 import java.time.LocalDate;
18 import java.time.LocalDateTime;
19 import java.time.LocalTime;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.energidataservice.internal.retry.RetryStrategy;
26 * This implements a {@link RetryStrategy} for a fixed time.
28 * @author Jacob Laursen - Initial contribution
31 public class FixedTime implements RetryStrategy {
33 private final Clock clock;
35 private LocalTime localTime;
36 private double jitter = 0.0;
38 public FixedTime(LocalTime localTime, Clock clock) {
39 this.localTime = localTime;
44 public Duration getDuration() {
45 LocalTime now = LocalTime.now(clock);
46 LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(clock), localTime);
47 if (now.isAfter(localTime)) {
48 localDateTime = localDateTime.plusDays(1);
51 Duration base = Duration.between(LocalDateTime.now(clock), localDateTime);
56 long duration = base.toMillis();
57 double rand = Math.random();
58 duration += (long) (rand * jitter * 1000 * 60);
60 return Duration.ofMillis(duration);
63 public FixedTime withJitter(double jitter) {
69 public boolean equals(@Nullable Object o) {
73 if (!(o instanceof FixedTime)) {
76 FixedTime other = (FixedTime) o;
78 return this.jitter == other.jitter && this.localTime.equals(other.localTime);
82 public final int hashCode() {