Methods Summary |
---|
public void | addValue(long value)Public method to add a value to the average,
the time it is added is the time this method is called.
//We get the current time factor.
long timeFactor = getEffectiveTime() / refreshRate;
//We first update the buffer.
update(timeFactor);
//And then we add our value to current element
values[(int) (timeFactor % nbElements)] += value;
|
public long | getAverage()This method can be called to get the current average value.
return( getSum() / period );
|
public long | getAverage(int average_period)
int slots = average_period<=0?(nbElements - 2):(average_period / refreshRate);
if ( slots <= 0 ){
slots = 1;
}else if ( slots > nbElements - 2 ){
slots = nbElements - 2;
}
if ( slots == 1 ){
return( getPointValue());
}
long res = getSum(slots) / ( period * slots / ( nbElements - 2 ));
return( res );
|
public double | getDoubleAverage()
return( (double)getSum() / period );
|
public java.lang.String | getDoubleAverageAsString(int precision)
return( DisplayFormatters.formatDecimal( getDoubleAverage(), precision ));
|
protected long | getEffectiveTime()
return( SystemTime.getCurrentTime());
|
public static org.gudy.azureus2.core3.util.Average | getInstance(int refreshRate, int period)The way to get a new Average Object, it does some parameter checking.
refreshRate must be greater than 100,
and period*1000 must be greater than refreshRate
if (refreshRate < 100)
return null;
if ((period * 1000) < refreshRate)
return null;
return new Average(refreshRate, period);
|
public long | getPointValue()
long timeFactor = getEffectiveTime() / refreshRate;
//We first update the buffer
update(timeFactor);
return(values[(int)((timeFactor-1)% nbElements)]);
|
protected final long | getSum()
//We get the current timeFactor
long timeFactor = getEffectiveTime() / refreshRate;
//We first update the buffer
update(timeFactor);
//The sum of all elements used for the average.
long sum = 0;
//Starting on oldest one (the one after the next one)
//Ending on last one fully updated (the one previous current one)
for (long i = timeFactor + 2; i < timeFactor + nbElements; i++) {
//Simple addition
sum += values[(int) (i % nbElements)];
}
//We return the sum divided by the period
return(sum);
|
protected final long | getSum(int slots)
//We get the current timeFactor
long timeFactor = getEffectiveTime() / refreshRate;
//We first update the buffer
update(timeFactor);
//The sum of all elements used for the average.
long sum = 0;
if ( slots < 1 ){
slots = 1;
}else if ( slots > nbElements-2 ){
slots = nbElements-2;
}
//Starting on oldest one (the one after the next one)
//Ending on last one fully updated (the one previous current one)
long end_slot = timeFactor + nbElements;
long start_slot = end_slot - slots;
for (long i = start_slot; i< end_slot;i++ ){
sum += values[(int) (i % nbElements)];
}
//We return the sum divided by the period
return(sum);
|
private synchronized void | update(long timeFactor)This method is used to update the buffer tha stores the values,
in fact it mostly does clean-up over this buffer,
erasing all values that have not been updated.
//If we have a really OLD lastUpdate, we could erase the buffer a
//huge number of time, so if it's really old, we change it so we'll only
//erase the buffer once.
if (lastUpdate < timeFactor - nbElements)
lastUpdate = timeFactor - nbElements - 1;
//For all values between lastUpdate + 1 (next value than last updated)
//and timeFactor (which is the new value insertion position)
for (long i = lastUpdate + 1; i <= timeFactor; i++) {
//We set the value to 0.
values[(int) (i % nbElements)] = 0;
}
//We also clear the next value to be inserted (so on next time change...)
values[(int) ((timeFactor + 1) % nbElements)] = 0;
//And we update lastUpdate.
lastUpdate = timeFactor;
|