Android Open Source - Bluetooth-Proximity-Lock Circular Queue

From Project

Back to project page Bluetooth-Proximity-Lock.


The source code is released under:

Apache License

If you think the Android project Bluetooth-Proximity-Lock 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

  Copyright 2014 Cullin Moran/* w w w  .java  2 s .com*/

  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

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  See the License for the specific language governing permissions and
  limitations under the License.

package com.javadog.bluetoothproximitylock.helpers;

import android.util.Log;

import com.javadog.bluetoothproximitylock.MainActivity;

import java.util.Collection;
import java.util.LinkedList;

 * A queue (FIFO) which holds the last N elements. Used to keep an average of proximity samples.
public class CircularQueue<Integer> extends LinkedList<Integer> {
  private final int limit;

   * @param maxElements The maximum number of elements to hold.
  public CircularQueue(int maxElements) {
    limit = maxElements;

  public boolean add(Integer element) {
    while(size() > limit) {

    return true;

   * Note: Only works if the values stored in this CircularQueue are Integer objects.
   * @return an average of all N values stored in this CircularQueue. If # samples < limit, returns a very small #.
  public float getAverageOfElements() {
    float result;

    if(limit == 0) {
      throw new ArithmeticException("Division by zero!");
    if(size() < limit) {
      result = Float.MIN_VALUE;
    } else {
      Float avg = 0f;
      for(Integer element : this) {
        avg += (java.lang.Integer) element;
      result = avg / limit;

    Log.d(MainActivity.DEBUG_TAG, "Average of last five distance samples: " + result);

    return result;

  public boolean addAll(int location, Collection<? extends Integer> collection) {
    return false;

  public boolean addAll(Collection<? extends Integer> collection) {
    return false;

Java Source Code List