package com.javonharper.tempo;

import java.util.ArrayList;

public class BpmCalculator {
	private static final Long MILLISECONDS_IN_A_MINUTE = Long.valueOf(60000);
	public ArrayList<Long> times;
	private boolean isRecording;

	public BpmCalculator() {
		times = new ArrayList<Long>();
		isRecording = false;
	}

	public void recordTime() {
		long time = System.currentTimeMillis();
		times.add(Long.valueOf(time));
		isRecording = true;
	}

	public int getBpm() {
		ArrayList<Long> deltas = getDeltas();
		return calculateBpm(deltas);
	}

	public void clearTimes() {
		times.clear();
		isRecording = false;
	}

	private ArrayList<Long> getDeltas() {
		ArrayList<Long> deltas = new ArrayList<Long>();

		for (int i = 0; i < times.size() - 1; i++) {
			Long delta = times.get(i + 1) - times.get(i);
			deltas.add(delta);
		}

		return deltas;
	}

	private int calculateBpm(ArrayList<Long> deltas) {
		Long sum = Long.valueOf(0);

		for (Long delta : deltas) {
			sum = sum + delta;
		}

		Long average = sum / deltas.size();

		return (int) (MILLISECONDS_IN_A_MINUTE / average);
	}

	public boolean isRecording() {
		return isRecording;
	}
}

@@ -141,27 +141,14 @@
 E = 
-Long.valueOf(
 60000
-)
+L
 ;%0A%09p
@@ -323,17 +323,17 @@
 e() %7B%0A%09%09
-l
+L
 ong time
@@ -379,27 +379,13 @@
 add(
-Long.valueOf(
 time)
-)
 ;%0A%09%09
@@ -881,23 +881,10 @@
 m = 
+0
 L
-ong.valueOf(0)
 ;%0A%0A%09

Normalize object/primitive usage