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.tesla.internal.throttler;
15 import java.util.Iterator;
16 import java.util.LinkedList;
17 import java.util.ListIterator;
18 import java.util.concurrent.TimeUnit;
21 * The {@link Rate} defines a rate limiter that accepts a number of calls to be
22 * executed in a given time length. If the quota of calls is used, then calls
23 * are scheduled for the next block of time
25 * @author Karel Goderis - Initial contribution
27 public final class Rate {
29 private final int numberCalls;
30 private final int timeLength;
31 private final TimeUnit timeUnit;
32 private final LinkedList<Long> callHistory = new LinkedList<>();
34 public Rate(int numberCalls, int timeLength, TimeUnit timeUnit) {
35 this.numberCalls = numberCalls;
36 this.timeLength = timeLength;
37 this.timeUnit = timeUnit;
40 public long timeInMillis() {
41 return timeUnit.toMillis(timeLength);
44 void addCall(long callTime) {
45 callHistory.addLast(callTime);
48 private void cleanOld(long now) {
49 ListIterator<Long> i = callHistory.listIterator();
50 long threshold = now - timeInMillis();
52 if (i.next() <= threshold) {
60 long callTime(long now) {
62 if (callHistory.size() < numberCalls) {
65 long lastStart = callHistory.getLast() - timeInMillis();
66 long firstPeriodCall = lastStart, call;
68 Iterator<Long> i = callHistory.descendingIterator();
71 if (call < lastStart) {
75 firstPeriodCall = call;
78 if (count < numberCalls) {
79 return firstPeriodCall + 1;
81 return firstPeriodCall + timeInMillis() + 1;