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.jeelink.internal;
16 * Computes a rolling average of readings.
18 * @author Volker Bier - Initial contribution
20 public abstract class RollingReadingAverage<R extends Reading> {
23 private R total = null;
24 private int index = 0;
27 public RollingReadingAverage(R[] array) {
28 maxSize = array.length;
32 public void add(R reading) {
40 total = add(total, reading);
41 total = substract(total, samples[index]);
44 samples[index] = reading;
45 if (++index == maxSize) {
50 public R getAverage() {
54 return divide(total, size);
57 protected abstract R add(R value1, R value2);
59 protected abstract R substract(R from, R value);
61 protected abstract R divide(R value, int count);