Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.codahale.metrics;
  
  import java.util.Arrays;
  
 import static java.lang.Math.floor;

A statistical snapshot of a Snapshot.
 
 public class Snapshot {
     private static final Charset UTF_8 = Charset.forName("UTF-8");
 
     private final long[] values;

    
Create a new Snapshot with the given values.

Parameters:
values an unordered set of values in the reservoir
 
     public Snapshot(Collection<Longvalues) {
         final Object[] copy = values.toArray();
         this. = new long[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 reservoir
 
     public Snapshot(long[] values) {
         this. = Arrays.copyOf(valuesvalues.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
 
     public int size() {
         return .;
     }

    
Returns the median value in the distribution.

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

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

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

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

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

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

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

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

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

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

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

    
Returns the entire set of values in the snapshot.

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

    
Returns the highest value in the snapshot.

Returns:
the highest value
    public long getMax() {
        if (. == 0) {
            return 0;
        }
        return [. - 1];
    }

    
Returns the lowest value in the snapshot.

Returns:
the lowest value
    public long getMin() {
        if (. == 0) {
            return 0;
        }
        return [0];
    }

    
Returns the arithmetic mean of the values in the snapshot.

Returns:
the arithmetic mean
    public double getMean() {
        if (. == 0) {
            return 0;
        }
        double sum = 0;
        for (long value : ) {
            sum += value;
        }
        return sum / .;
    }

    
Returns the standard deviation of the values in the snapshot.

Returns:
the standard value
    public double getStdDev() {
        // two-pass algorithm for variance, avoids numeric overflow
        if (. <= 1) {
            return 0;
        }
        final double mean = getMean();
        double sum = 0;
        for (long value : ) {
            final double diff = value - mean;
            sum += diff * diff;
        }
        final double variance = sum / (. - 1);
        return Math.sqrt(variance);
    }

    
Writes the values of the snapshot to the given stream.

Parameters:
output an output stream
    public void dump(OutputStream output) {
        final PrintWriter out = new PrintWriter(new OutputStreamWriter(output));
        try {
            for (long value : ) {
                out.printf("%d%n"value);
            }
        } finally {
            out.close();
        }
    }
New to GrepCode? Check out our FAQ X