package utilities;

import java.util.ArrayList;

/**
 *  Utility class that expands ArrayList with two new fields for binned data: the minimum edge of the first bin and the
 *  width of the bins. It also includes some methods for getting and setting these new fields, as well as a method to
 *  get the frequency/density/probability of the bin within which a certain value falls
 *
 *  @author daniel, Adrian Carro
 */
public class BinnedData<D> extends ArrayList<D> {

    //------------------//
    //----- Fields -----//
    //------------------//

	private double firstBinMin;     // Minimum edge of the first bin
	private double binWidth;        // Bin width

    //------------------------//
    //----- Constructors -----//
    //------------------------//
	
	public BinnedData(double firstBinMin, double binWidth) {
		this.firstBinMin = firstBinMin;
		this.binWidth = binWidth;
	}

    //-------------------//
    //----- Methods -----//
    //-------------------//
	
	public double getSupportLowerBound() { return firstBinMin; }

	public double getSupportUpperBound() { return firstBinMin + size()*binWidth; }

	public double getBinWidth() { return binWidth; }

    /**
     * Returns the frequency/density/probability of the bin at which the value val falls
     */
	public D getBinAt(double val) { return get((int)((val - firstBinMin)/binWidth)); }
	
	public void setBinWidth(double width) { binWidth = width; }
	
	public void setFirstBinMin(double min) { firstBinMin = min; }
}

@@ -1260,16 +1260,161 @@
 al falls
+, with security to ensure values%0A     * below the minimum bin edge return the minimum bin and values above the maximum bin return the maximum bin
 %0A     */
@@ -1414,17 +1414,20 @@
      */%0A
-%09
+    
 public D
@@ -1449,16 +1449,146 @@
 e val) %7B
+%0A        if (val %3C firstBinMin) return get(0);%0A        else if (val %3E getSupportUpperBound()) return get(size() - 1);%0A        else
  return 
@@ -1628,20 +1628,23 @@
 Width));
+%0A   
  %7D%0A
-%09
 %0A%09public

Add support for values outside the given bin range at BinnedData