Example usage for com.google.common.collect EvictingQueue remainingCapacity

List of usage examples for com.google.common.collect EvictingQueue remainingCapacity

Introduction

In this page you can find the example usage for com.google.common.collect EvictingQueue remainingCapacity.

Prototype

public int remainingCapacity() 

Source Link

Document

Returns the number of additional elements that this queue can accept without evicting; zero if the queue is currently full.

Usage

From source file:org.hawkular.datamining.forecast.models.WeightedMovingAverage.java

public List<DataPoint> learn() {

    List<DataPoint> result = new ArrayList<>(dataPoints.size());
    EvictingQueue<Double> window = EvictingQueue.create(weights.length);

    int endHalf = weights.length / 2;

    // add zeros to the beginning
    for (int i = 0; i < (weights.length - endHalf) - 1; i++) {
        result.add(new DataPoint(null, dataPoints.get(i).getTimestamp()));
    }//from w w w.  ja  va2s  .  c o m

    for (int i = 0; i < dataPoints.size(); i++) {
        window.add(dataPoints.get(i).getValue());

        if (window.remainingCapacity() == 0) {
            Iterator<Double> iterator = window.iterator();
            int counter = 0;
            double sum = 0;
            while (iterator.hasNext()) {
                double value = iterator.next();
                sum += value * weights[counter++];
            }

            result.add(new DataPoint(sum, dataPoints.get(i - endHalf).getTimestamp()));
        }
    }

    // add zeros to end
    for (int i = result.size(); i < dataPoints.size(); i++) {
        result.add(new DataPoint(null, dataPoints.get(i).getTimestamp()));
    }

    return result;
}