2 * Copyright (c) 2010-2022 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.ambientweather.internal.util;
15 import java.util.Collections;
16 import java.util.Iterator;
17 import java.util.SortedMap;
18 import java.util.TreeMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
23 * The {@link SlidingTimeWindow} is responsible for managing a set of
24 * time-based values. This class is used to calculate trends over time.
26 * @author Mark Hilbush - Initial contribution
29 class SlidingTimeWindow<T> {
30 protected long period = 0;
31 protected final SortedMap<Long, T> storage = Collections.synchronizedSortedMap(new TreeMap<>());
34 * Create a sliding time window for the provided time period
36 public SlidingTimeWindow(long period) {
40 public void put(T value) {
41 storage.put(System.currentTimeMillis(), value);
44 public void removeOldEntries() {
45 long old = System.currentTimeMillis() - period;
46 synchronized (storage) {
47 for (Iterator<Long> iterator = storage.keySet().iterator(); iterator.hasNext();) {
48 long time = iterator.next();