Package org.jgroups.util
Class Average
- java.lang.Object
-
- org.jgroups.util.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 (>= 0) values in a window and adding new values at random indices.
This class is lock-less, at the expense of incorrect reads and writes (by multiple threads), but the noise of these errors should be neglectable. Note that a bigger capacity (window size) reduces this noise.- Since:
- 3.4, 5.4
- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
all_filled
protected static int
DEFAULT_CAPACITY
protected long[]
samples
protected java.util.concurrent.TimeUnit
unit
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends Average>
Tadd(long num)
double
average()
Returns the average of all valid samples divided by the number of valid samplesint
capacity()
void
clear()
int
count()
Returns the number of valid samples (>= 0)double
getAverage()
boolean
isEmpty()
Returns true if there are no valid samples (>= 0), false otherwise<T extends Average>
Tmerge(T other)
Merges this average with another onevoid
readFrom(java.io.DataInput in)
Read the state of the current object (including superclasses) from instream Note that the input stream must not be closedjava.lang.String
toString()
java.lang.String
toString(java.util.concurrent.TimeUnit u)
long
total()
Returns the total of all valid (>= 0) valuesjava.util.concurrent.TimeUnit
unit()
<T extends Average>
Tunit(java.util.concurrent.TimeUnit u)
void
writeTo(java.io.DataOutput out)
Write the entire state of the current object (including superclasses) to outstream.
-
-
-
Field Detail
-
samples
protected long[] samples
-
unit
protected java.util.concurrent.TimeUnit unit
-
all_filled
protected volatile boolean all_filled
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY
- See Also:
- Constant Field Values
-
-
Method Detail
-
capacity
public int capacity()
-
unit
public java.util.concurrent.TimeUnit unit()
-
unit
public <T extends Average> T unit(java.util.concurrent.TimeUnit u)
-
getAverage
public double getAverage()
-
add
public <T extends Average> T add(long num)
-
merge
public <T extends Average> T merge(T other)
Merges this average with another one
-
total
public long total()
Returns the total of all valid (>= 0) values
-
count
public int count()
Returns the number of valid samples (>= 0)
-
isEmpty
public boolean isEmpty()
Returns true if there are no valid samples (>= 0), false otherwise
-
average
public double average()
Returns the average of all valid samples divided by the number of valid samples
-
clear
public void clear()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.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 interfaceStreamable
- 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 interfaceStreamable
- Throws:
java.io.IOException
-
-