View Javadoc

1   /*
2    * $Id: LongDistribution.java 1540 2011-08-17 13:30:37Z hoheisel $
3    *
4    * Copyright (c) 2008, Fraunhofer FIRST
5    * Fraunhofer Institute for Computer Architecture and Software Technology
6    * See http://www.first.fraunhofer.de and http://www.gridworkflow.org/gwes for more details.
7    */
8   package net.kwfgrid.gwes.workflowanalyzer;
9   
10  import org.apache.log4j.Logger;
11  
12  /**
13   * @author Andreas Hoheisel
14   *         (<a href="http://www.andreas-hoheisel.de">www.andreas-hoheisel.de</a>)
15   * @author Dietmar Sommerfeld / GWDG
16   * @version $Id: LongDistribution.java 1540 2011-08-17 13:30:37Z hoheisel $
17   */
18  public class LongDistribution {
19  
20      static Logger logger = Logger.getLogger(LongDistribution.class);
21  
22      private long number;
23      private double mean;
24      private double sumSquare;
25      private double stdDeviation;
26      private double expSmooth;
27      private long min;
28      private long max;
29  
30      public LongDistribution() {
31          this.number = 0;
32          this.mean = 0d;
33          this.sumSquare = 0d;
34          this.stdDeviation = 0d;
35          this.expSmooth = 0d;
36          this.min = Long.MAX_VALUE;
37          this.max = Long.MIN_VALUE;
38      }
39  
40      public LongDistribution(long number, double mean, double stdDeviation, double expSmooth, long min, long max) {
41          this.number = number;
42          this.mean = mean;
43          this.stdDeviation = stdDeviation;
44          this.expSmooth = expSmooth;
45          this.min = min;
46          this.max = max;
47          this.sumSquare = (number - 1) * stdDeviation * stdDeviation + number * mean * mean;
48      }
49  
50      public synchronized void put(long value) {
51          if (logger.isDebugEnabled()) logger.debug("put: number=" + number + " expSmooth=" + expSmooth + " put value=" + value);
52          if (number == 0) {   // first insert
53  		    expSmooth = value;
54          } else {
55  		    expSmooth = 0.3d * value + 0.7d * expSmooth;
56          }
57  
58          if (value < min) min = value;
59          if (value > max) max = value;
60          mean = (number * mean + value) / (number + 1);
61          sumSquare += value*value; 
62          number++;
63          stdDeviation = (number == 1) ? 0d : Math.sqrt( (sumSquare - number * mean * mean ) / (number - 1) );
64      }
65  
66      public synchronized void add(LongDistribution distribution) {
67          if (logger.isDebugEnabled()) logger.debug("add: number=" + number + " expSmooth=" + expSmooth + " add expSmooth=" + distribution.expSmooth);
68          if (number == 0) {   // first insert
69          	expSmooth = distribution.expSmooth;
70          } else {
71          	expSmooth = 0.3d * distribution.expSmooth + 0.7d * expSmooth;
72          }
73  
74          long newNumber = number + distribution.number;
75          double newMean = (number * mean + distribution.number * distribution.mean) / newNumber;
76          double newSumSquare = sumSquare + distribution.sumSquare;
77          double newStdDeviation = (newNumber == 1) ? 0d : Math.sqrt( (newSumSquare - newNumber * newMean * newMean ) / (newNumber - 1) );
78          number = newNumber;
79          mean = newMean;
80          sumSquare = newSumSquare;
81          stdDeviation = newStdDeviation;
82          if (distribution.min < min) min = distribution.min;
83          if (distribution.max > max) max = distribution.max;
84      }
85  
86      public long getNumber() {
87          return number;
88      }
89  
90      public double getMean() {
91          return mean;
92      }
93  
94      public double getSumSquare() {
95          return sumSquare;
96      }
97  
98      public double getStdDeviation() {
99          return stdDeviation;
100     }
101 
102     public double getExpSmooth() {
103         return expSmooth;
104     }
105 
106     public long getMin() {
107         return min;
108     }
109 
110     public long getMax() {
111         return max;
112     }
113 
114     public String toString() {
115         StringBuffer ret = new StringBuffer("LongDistribution(number=");
116         ret.append(number);
117         ret.append(",mean=").append(mean);
118         ret.append(",stdDeviation=").append(stdDeviation);
119         ret.append(",expSmooth=").append(expSmooth);
120         ret.append(",min=").append(min);
121         ret.append(",max=").append(max);
122         ret.append(")");
123         return ret.toString();
124     }
125 
126     @Override
127     public Object clone() {
128         return new LongDistribution(number,mean,stdDeviation,expSmooth,min,max);
129     }
130 
131     @Override
132     public boolean equals(Object o) {
133         if (this == o) return true;
134         if (o == null || getClass() != o.getClass()) return false;
135 
136         LongDistribution that = (LongDistribution) o;
137 
138         return max == that.max && Double.compare(that.mean, mean) == 0 && min == that.min && number == that.number && Double.compare(that.stdDeviation, stdDeviation) == 0 && Double.compare(that.expSmooth, expSmooth) == 0;
139 
140     }
141 
142     @Override
143     public int hashCode() {
144         int result;
145         long temp;
146         result = (int) (number ^ (number >>> 32));
147         temp = mean != +0.0d ? Double.doubleToLongBits(mean) : 0L;
148         result = 31 * result + (int) (temp ^ (temp >>> 32));
149         temp = stdDeviation != +0.0d ? Double.doubleToLongBits(stdDeviation) : 0L;
150         result = 31 * result + (int) (temp ^ (temp >>> 32));
151         temp = expSmooth != +0.0d ? Double.doubleToLongBits(expSmooth) : 0L;
152         result = 31 * result + (int) (temp ^ (temp >>> 32));
153         result = 31 * result + (int) (min ^ (min >>> 32));
154         result = 31 * result + (int) (max ^ (max >>> 32));
155         return result;
156     }
157 }