| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 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) |
|---|
| 54 | { |
|---|
| 55 | mName = name; |
|---|
| 56 | mNumberOfValues = 0; |
|---|
| 57 | mMinimumValue = Long.MAX_VALUE; |
|---|
| 58 | mMaximumValue = 0; |
|---|
| 59 | mSumOfValues = 0; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** {@inheritDoc} */ |
|---|
| 63 | public synchronized void reset () |
|---|
| 64 | { |
|---|
| 65 | mNumberOfValues = 0; |
|---|
| 66 | mMinimumValue = Long.MAX_VALUE; |
|---|
| 67 | mMaximumValue = 0; |
|---|
| 68 | mSumOfValues = 0; |
|---|
| 69 | } |
|---|
| 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 | { |
|---|
| 77 | if (value < 0) |
|---|
| 78 | { |
|---|
| 79 | throw new IllegalArgumentException("Values < 0 not allowed"); |
|---|
| 80 | } |
|---|
| 81 | long lastSumOfValues; |
|---|
| 82 | long sumOfValues; |
|---|
| 83 | synchronized (this) |
|---|
| 84 | { |
|---|
| 85 | if (value < mMinimumValue) |
|---|
| 86 | { |
|---|
| 87 | mMinimumValue = value; |
|---|
| 88 | } |
|---|
| 89 | if (value > mMaximumValue) |
|---|
| 90 | { |
|---|
| 91 | mMaximumValue = value; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | lastSumOfValues = mSumOfValues; |
|---|
| 95 | mSumOfValues += value; |
|---|
| 96 | sumOfValues = mSumOfValues; |
|---|
| 97 | mNumberOfValues++; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | if (sumOfValues < lastSumOfValues) |
|---|
| 101 | { |
|---|
| 102 | throw new RuntimeException("Sum overflow: " |
|---|
| 103 | + lastSumOfValues + " + " + value + " gives " + sumOfValues); |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 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 | |
|---|
| 116 | if (mNumberOfValues > 0) |
|---|
| 117 | { |
|---|
| 118 | avg = mSumOfValues / mNumberOfValues; |
|---|
| 119 | } |
|---|
| 120 | else |
|---|
| 121 | { |
|---|
| 122 | avg = 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | 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 | { |
|---|
| 135 | 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 | { |
|---|
| 145 | 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 | { |
|---|
| 154 | return mNumberOfValues; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | /** {@inheritDoc} */ |
|---|
| 158 | public String toString () |
|---|
| 159 | { |
|---|
| 160 | final StringBuffer sbuf = new StringBuffer(); |
|---|
| 161 | sbuf.append("[Average ").append(mName); |
|---|
| 162 | sbuf.append(" count=").append(getCount()); |
|---|
| 163 | sbuf.append(", min=").append(getMinimum()); |
|---|
| 164 | sbuf.append(", avg=").append(getAverage()); |
|---|
| 165 | sbuf.append(", max=").append(getMaximum()); |
|---|
| 166 | sbuf.append(']'); |
|---|
| 167 | return sbuf.toString(); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /** |
|---|
| 171 | * Returns the name. |
|---|
| 172 | * @return String |
|---|
| 173 | */ |
|---|
| 174 | public String getName () |
|---|
| 175 | { |
|---|
| 176 | return mName; |
|---|
| 177 | } |
|---|
| 178 | } |
|---|