Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  *
  * Copyright 2013 Netflix, Inc.
  *
  * 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
  *
  * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
 package com.netflix.stats.distribution;


Accumulator of statistics about a distribution of observed values that are produced incrementally.

Note that the implementation is not synchronized, and simultaneous updates may produce incorrect results. In most cases these incorrect results will be unimportant, but applications that care should synchronize carefully to ensure consistent results.

Note that this implements DistributionMBean and so can be registered as an MBean and accessed via JMX if desired.

Author(s):
netflixoss $
Version:
$Revision: $
 
 public class Distribution implements DistributionMBeanDataCollector {
 
     private long numValues;
     private double sumValues;
     private double sumSquareValues;
     private double minValue;
     private double maxValue;
 
     /*
      * Constructors
      */

    
Creates a new initially empty Distribution.
 
     public Distribution() {
          = 0L;
          = 0.0;
          = 0.0;
          = 0.0;
          = 0.0;
     }
 
     /*
      * Accumulating new values
      */

    
 
     public void noteValue(double val) {
         ++;
          += val;
          += val * val;
         if ( == 1) {
              = val;
              = val;
         } else if (val < ) {
              = val;
         } else if (val > ) {
              = val;
         }
     }

    
 
     public void clear() {
          = 0L;
          = 0.0;
          = 0.0;
          = 0.0;
          = 0.0;
     }
 
     /*
      * Accessors
      */

    
 
     public long getNumValues() {
         return ;
     }

    
 
     public double getMean() {
         if ( < 1) {
            return 0.0;
        } else {
            return  / ;
        }
    }

    
    public double getVariance() {
        if ( < 2) {
            return 0.0;
        } else if ( == 0.0) {
            return 0.0;
        } else {
            double mean = getMean();
            return ( / ) - mean * mean;
        }
    }

    
    public double getStdDev() {
        return Math.sqrt(getVariance());
    }

    
    public double getMinimum() {
        return ;
    }

    
    public double getMaximum() {
        return ;
    }

    
Add another Distribution's values to this one.

Parameters:
anotherDistribution the other Distribution instance
    public void add(Distribution anotherDistribution) {
        if (anotherDistribution != null) {
             += anotherDistribution.numValues;
             += anotherDistribution.sumValues;
             += anotherDistribution.sumSquareValues;
             = ( < anotherDistribution.minValue) ? 
                    : anotherDistribution.minValue;
             = ( > anotherDistribution.maxValue) ? 
                    : anotherDistribution.maxValue;
        }
    }
    @Override
    public String toString() {
        return new StringBuilder()
            .append("{Distribution:")
            .append("N=").append(getNumValues())
            .append(": ").append(getMinimum())
            .append("..").append(getMean())
            .append("..").append(getMaximum())
            .append("}")
            .toString();
    }
// Distribution
New to GrepCode? Check out our FAQ X