Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.metrics.stats;
  
  import java.io.File;
  import java.util.*;
  
  import static java.lang.Math.floor;

A statistical snapshot of a Snapshot.
 
 public class Snapshot {
     private static final double MEDIAN_Q = 0.5;
     private static final double P75_Q = 0.75;
     private static final double P95_Q = 0.95;
     private static final double P98_Q = 0.98;
     private static final double P99_Q = 0.99;
     private static final double P999_Q = 0.999;
 
     private final double[] values;

    
Create a new Snapshot with the given values.

Parameters:
values an unordered set of values in the sample
 
     public Snapshot(Collection<Longvalues) {
         final Object[] copy = values.toArray();
         this. = new double[copy.length];
         for (int i = 0; i < copy.lengthi++) {
             this.[i] = (Longcopy[i];
         }
         Arrays.sort(this.);
     }

    
Create a new Snapshot with the given values.

Parameters:
values an unordered set of values in the sample
 
     public Snapshot(double[] values) {
         this. = new double[values.length];
         System.arraycopy(values, 0, this., 0, values.length);
         Arrays.sort(this.);
     }

    
Returns the value at the given quantile.

Parameters:
quantile a given quantile, in [0..1]
Returns:
the value in the distribution at quantile
 
     public double getValue(double quantile) {
         if (quantile < 0.0 || quantile > 1.0) {
             throw new IllegalArgumentException(quantile + " is not in [0..1]");
         }
 
         if (. == 0) {
             return 0.0;
         }
 
         final double pos = quantile * (. + 1);
 
         if (pos < 1) {
             return [0];
         }
 
         if (pos >= .) {
             return [. - 1];
         }
 
         final double lower = [(intpos - 1];
         final double upper = [(intpos];
         return lower + (pos - floor(pos)) * (upper - lower);
     }

    
Returns the number of values in the snapshot.

Returns:
the number of values in the snapshot
 
     public int size() {
         return .;
     }

    
Returns the median value in the distribution.

Returns:
the median value in the distribution
 
     public double getMedian() {
         return getValue();
     }

    
Returns the value at the 75th percentile in the distribution.

Returns:
the value at the 75th percentile in the distribution
    public double get75thPercentile() {
        return getValue();
    }

    
Returns the value at the 95th percentile in the distribution.

Returns:
the value at the 95th percentile in the distribution
    public double get95thPercentile() {
        return getValue();
    }

    
Returns the value at the 98th percentile in the distribution.

Returns:
the value at the 98th percentile in the distribution
    public double get98thPercentile() {
        return getValue();
    }

    
Returns the value at the 99th percentile in the distribution.

Returns:
the value at the 99th percentile in the distribution
    public double get99thPercentile() {
        return getValue();
    }

    
Returns the value at the 99.9th percentile in the distribution.

Returns:
the value at the 99.9th percentile in the distribution
    public double get999thPercentile() {
        return getValue();
    }

    
Returns the entire set of values in the snapshot.

Returns:
the entire set of values in the snapshot
    public double[] getValues() {
        return Arrays.copyOf(.);
    }

    
Writes the values of the sample to the given file.

Parameters:
output the file to which the values will be written
Throws:
java.io.IOException if there is an error writing the values
    public void dump(File outputthrows IOException {
        final PrintWriter writer = new PrintWriter(output);
        try {
            for (double value : ) {
                writer.printf("%f\n"value);
            }
        } finally {
            writer.close();
        }
    }
New to GrepCode? Check out our FAQ X