View Javadoc
1   package org.djutils.means;
2   
3   /**
4    * Compute arithmetic (weighted) mean of a set of values.
5    * <p>
6    * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
7    * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
8    * <p>
9    * @version $Revision$, $LastChangedDate$, by $Author$, initial version Oct 26, 2018 <br>
10   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
11   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
12   * @param <V> value type
13   * @param <W> weight type
14   */
15  public class ArithmeticMean<V extends Number, W extends Number> extends AbstractMean<ArithmeticMean<V, W>, V, W>
16  {
17      /** {@inheritDoc} */
18      @Override
19      public final double getMean()
20      {
21          return getSum() / getSumOfWeights();
22      }
23  
24      /** {@inheritDoc} */
25      @Override
26      public final ArithmeticMean<V, W> addImpl(final V value, final Number weight)
27      {
28          increment(weight.doubleValue() * value.doubleValue(), weight.doubleValue());
29          return this;
30      }
31  
32      /** {@inheritDoc} */
33      @Override
34      public final String toString()
35      {
36          return "ArithmeticMean [current sum=" + getSum() + ", current sum of weights=" + getSumOfWeights()
37                  + ", current arithmetic mean=" + getMean() + "]";
38      }
39  
40  }