Class Average

  • All Implemented Interfaces:
    Streamable
    Direct Known Subclasses:
    AverageMinMax

    public class Average
    extends java.lang.Object
    implements Streamable
    Maintains an approximation of an average of values. Done by keeping track of the number of samples, and computing the new average as (count*avg + new-sample) / ++count. We reset the count if count*avg would lead to an overflow.

    This class is not thread-safe and relies on external synchronization.

    Since:
    3.4
    Author:
    Bela Ban
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected double avg  
      protected long count  
    • Constructor Summary

      Constructors 
      Constructor Description
      Average()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T extends Average>
      T
      add​(long num)  
      double average()  
      void clear()  
      long count()  
      double getAverage()  
      long getCount()  
      <T extends Average>
      T
      merge​(T other)
      Merges this average with another one
      void readFrom​(java.io.DataInput in)
      Read the state of the current object (including superclasses) from instream Note that the input stream must not be closed
      java.lang.String toString()  
      void writeTo​(java.io.DataOutput out)
      Write the entire state of the current object (including superclasses) to outstream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • avg

        protected double avg
      • count

        protected long count
    • Constructor Detail

      • Average

        public Average()
    • Method Detail

      • add

        public <T extends Average> T add​(long num)
      • merge

        public <T extends Average> T merge​(T other)
        Merges this average with another one
      • getAverage

        public double getAverage()
      • average

        public double average()
      • getCount

        public long getCount()
      • count

        public long count()
      • clear

        public void clear()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • writeTo

        public void writeTo​(java.io.DataOutput out)
                     throws java.io.IOException
        Description copied from interface: Streamable
        Write the entire state of the current object (including superclasses) to outstream. Note that the output stream must not be closed
        Specified by:
        writeTo in interface Streamable
        Throws:
        java.io.IOException
      • readFrom

        public void readFrom​(java.io.DataInput in)
                      throws java.io.IOException
        Description copied from interface: Streamable
        Read the state of the current object (including superclasses) from instream Note that the input stream must not be closed
        Specified by:
        readFrom in interface Streamable
        Throws:
        java.io.IOException