Project Report: fawkez

Packagesummary org.jcoderz.commons.statistics

org.jcoderz.commons.statistics.Average

LineHitsNoteSource
1  /*
2   * $Id: Average.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.statistics;
34  
35  /**
36   * Compute the average of a number of positive long values.
37   * @author Albrecht Messner
38   */
39  public class Average
40     implements Resettable
41  {
42     private final String mName;
43  
44     private long mNumberOfValues;
45     private long mMinimumValue;
46     private long mMaximumValue;
47     private long mSumOfValues;
48  
49     /**
50      * Constructor.
51      * @param name The name of the average
52      */
53     public Average (String name)
540    {
550       mName = name;
560       mNumberOfValues = 0;
570       mMinimumValue = Long.MAX_VALUE;
580       mMaximumValue = 0;
590       mSumOfValues = 0;
600    }
61  
62     /** {@inheritDoc} */
63     public synchronized void reset ()
64     {
650       mNumberOfValues = 0;
660       mMinimumValue = Long.MAX_VALUE;
670       mMaximumValue = 0;
680       mSumOfValues = 0;
690    }
70  
71     /**
72      * Update the average value with the given argument.
73      * @param value the value with which the average is updated
74      */
75     public void update (long value)
76     {
770       if (value < 0)
78        {
790(1)         throw new IllegalArgumentException("Values < 0 not allowed");
80        }
81        long lastSumOfValues;
82        long sumOfValues;
830(2)      synchronized (this)
84        {
850          if (value < mMinimumValue)
86           {
870             mMinimumValue = value;
88           }
890          if (value > mMaximumValue)
90           {
910             mMaximumValue = value;
92           }
93  
940          lastSumOfValues = mSumOfValues;
950          mSumOfValues += value;
960          sumOfValues = mSumOfValues;
970          mNumberOfValues++;
980       }
99  
1000       if (sumOfValues < lastSumOfValues)
101        {
1020          throw new RuntimeException("Sum overflow: "
103              + lastSumOfValues + " + " + value + " gives " + sumOfValues);
104        }
1050    }
106  
107     /**
108      * Computes the average of the values added with {@link #update(long)}
109      * so far.
110      * @return the average
111      */
112     public synchronized long getAverage ()
113     {
114        final long avg;
115  
1160       if (mNumberOfValues > 0)
117        {
1180          avg = mSumOfValues / mNumberOfValues;
119        }
120        else
121        {
1220          avg = 0;
123        }
124  
1250       return avg;
126     }
127  
128     /**
129      * Returns the minimum value of this average, or Long.MAX_VALUE if
130      * no value has been added so far.
131      * @return the minimum value of this average
132      */
133     public synchronized long getMinimum ()
134     {
1350       return mMinimumValue;
136     }
137  
138     /**
139      * Returns the maximum value of this average, or zero if no
140      * value has been added so far.
141      * @return the maximum value of this average
142      */
143     public synchronized long getMaximum ()
144     {
1450       return mMaximumValue;
146     }
147  
148     /**
149      * Returns the number of values that have been added to this average.
150      * @return the number of values that have been added to this average
151      */
152     public synchronized long getCount ()
153     {
1540       return mNumberOfValues;
155     }
156  
157     /** {@inheritDoc} */
158     public String toString ()
159     {
1600(3)      final StringBuffer sbuf = new StringBuffer();
1610       sbuf.append("[Average ").append(mName);
1620       sbuf.append(" count=").append(getCount());
1630       sbuf.append(", min=").append(getMinimum());
1640       sbuf.append(", avg=").append(getAverage());
1650       sbuf.append(", max=").append(getMaximum());
1660       sbuf.append(']');
1670       return sbuf.toString();
168     }
169  
170     /**
171      * Returns the name.
172      * @return String
173      */
174     public String getName ()
175     {
1760       return mName;
177     }
178  }

Findings in this File

i (1) 79 : 0 method org.jcoderz.commons.statistics.Average.update(long) throws exception with static message string
i (2) 83 : 0 class org.jcoderz.commons.statistics.Average uses non owned variables to synchronize on
i (3) 160 : 26 StringBuffer constructor is initialized with size 16, but has at least 35 characters appended.