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 positive values by keeping a number of values and overwriting values when new values are added.
    This class uses lockless algorithms and is thread-safe
    Since:
    3.4
    Author:
    Bela Ban
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.atomic.LongAdder count  
      protected int index  
      protected java.util.concurrent.locks.Lock lock  
      protected java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Double> samples  
      protected java.util.concurrent.atomic.DoubleAdder total  
      protected java.util.concurrent.TimeUnit unit  
    • Constructor Summary

      Constructors 
      Constructor Description
      Average()  
      Average​(int capacity)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T extends Average>
      T
      add​(double num)  
      <T extends Average>
      T
      add​(long num)  
      double average()  
      void clear()  
      long count()  
      double getAverage()  
      double getTotal()  
      <T extends Average>
      T
      merge​(T other)
      Merges this average with another one
      protected int nextIndex()  
      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()  
      java.lang.String toString​(java.util.concurrent.TimeUnit u)  
      java.util.concurrent.TimeUnit unit()  
      <T extends Average>
      T
      unit​(java.util.concurrent.TimeUnit u)  
      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

      • total

        protected final java.util.concurrent.atomic.DoubleAdder total
      • samples

        protected java.util.concurrent.atomic.AtomicReferenceArray<java.lang.Double> samples
      • index

        protected int index
      • count

        protected final java.util.concurrent.atomic.LongAdder count
      • unit

        protected java.util.concurrent.TimeUnit unit
      • lock

        protected final java.util.concurrent.locks.Lock lock
    • Constructor Detail

      • Average

        public Average()
      • Average

        public Average​(int capacity)
    • Method Detail

      • add

        public <T extends Average> T add​(double num)
      • 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()
      • count

        public long count()
      • getTotal

        public double getTotal()
      • unit

        public java.util.concurrent.TimeUnit unit()
      • unit

        public <T extends Average> T unit​(java.util.concurrent.TimeUnit u)
      • average

        public double average()
      • clear

        public void clear()
      • toString

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

        public java.lang.String toString​(java.util.concurrent.TimeUnit u)
      • 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
      • nextIndex

        protected int nextIndex()