/*
 * Copyright (c) 2016 by Gerrit Grunwald
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.hansolo.tilesfx.tools;

import java.time.Duration;
import java.time.Instant;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;


/**
 * Created by hansolo on 01.11.16.
 */
public class MovingAverage {
    public  static final int         MAX_PERIOD     = 2_073_600; // 24h in seconds
    private static final int         DEFAULT_PERIOD = 10;
    private        final Queue<TimeData> window;
    private              int             period;
    private              double          sum;


    // ******************** Constructors **************************************
    public MovingAverage() {
        this(DEFAULT_PERIOD);
    }
    public MovingAverage(final int PERIOD) {
        period = Helper.clamp(0, MAX_PERIOD, PERIOD);
        window = new ConcurrentLinkedQueue<>();
    }


    // ******************** Methods *******************************************
    public void addData(final TimeData DATA) {
        sum += DATA.getValue();
        window.add(DATA);
        if (window.size() > period) {
            sum -= window.remove().getValue();
        }
    }
    public void addValue(final double VALUE) {
        addData(new TimeData(VALUE));
    }
    public void addListOfData(final List<TimeData> LIST_OF_DATA) {
        LIST_OF_DATA.forEach(data -> addData(data));
    }

    public Queue<TimeData> getWindow() { return new LinkedList<>(window); }

    public TimeData getFirstEntry() { return window.peek(); }
    public TimeData getLastEntry() { return window.stream().reduce((first, second) -> second).orElse(null); }

    public Instant getTimeSpan() {
        TimeData firstEntry = getFirstEntry();
        TimeData lastEntry  = getLastEntry();
        if (null == firstEntry || null == lastEntry) return null;
        return lastEntry.getTimestamp().minusSeconds(firstEntry.getTimestamp().getEpochSecond());
    }

    public double getAverage() {
        if (window.isEmpty()) return 0; // technically the average is undefined
        return (sum / window.size());
    }

    public double getTimeBasedAverageOf(final Duration DURATION) {
        assert !DURATION.isNegative() : "Time period must be positive";
        Instant now     = Instant.now();
        return window.stream()
                     .filter(v -> v.getTimestamp().isAfter(now.minus(DURATION)))
                     .mapToDouble(TimeData::getValue)
                     .average()
                     .getAsDouble();
    }

    public int getPeriod() { return period; }
    public void setPeriod(final int PERIOD) {
        period = Helper.clamp(0, MAX_PERIOD, PERIOD);
        reset();
    }

    public void reset() { window.clear(); }
}

@@ -815,16 +815,52 @@
 dQueue;%0A
+import java.util.stream.Collectors;%0A
 %0A%0A/**%0A *
@@ -962,16 +962,20 @@
         
+    
 MAX_PERI
@@ -1049,16 +1049,20 @@
         
+    
 DEFAULT_
@@ -2068,41 +2068,202 @@
 () %7B
- return new LinkedList%3C%3E(window);
+%0A        return window.stream()%0A                     .map(item -%3E new TimeData(item.getValue(), item.getTimestamp()))%0A                     .collect(Collectors.toCollection(LinkedList::new));%0A   
  %7D%0A%0A

Return deep copy of averaging data in MovingAverage