Android Open Source - retro-gauge-android Rolling Average






From Project

Back to project page retro-gauge-android.

License

The source code is released under:

Copyright (c) 2012 Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are me...

If you think the Android project retro-gauge-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.openxc.gaugedriver;
//  ww  w . j a va2  s  .  c  o m
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

import android.util.Log;

public class RollingAverage {
    private final int DEFAULT_WINDOW_SIZE = 200;

    private int mWindowSize = DEFAULT_WINDOW_SIZE;
    private Queue<Double> mValues = new LinkedBlockingQueue<Double>();

    public RollingAverage() {
    }

    public RollingAverage(int windowSize) {
        mWindowSize = windowSize;
    }

    public synchronized void add(double value) {
        mValues.add(value);
        if(mValues.size() > mWindowSize) {
            mValues.poll();
        }
    }

    public double getAverage() {
        double sum = 0;
        for(Double value : mValues) {
            sum += value;
        }
        return sum / mValues.size();
    }
}




Java Source Code List

com.openxc.gaugedriver.GaugeDriverActivity.java
com.openxc.gaugedriver.RollingAverage.java