gr.uoa.di.madgik.grs.utils
Class ProgressiveTimeoutGenerator

java.lang.Object
  extended by gr.uoa.di.madgik.grs.utils.ProgressiveTimeoutGenerator

public class ProgressiveTimeoutGenerator
extends java.lang.Object

Utility class which progressively returns waiting values whose sum amounts to a supplied timeout. Used by readers and writers in order to avoid waiting at once for the full timeout interval when their wait condition is triggered.

Author:
gerasimos.farantatos

Constructor Summary
ProgressiveTimeoutGenerator(long timeout)
          Creates a new instance
 
Method Summary
 boolean hasNext()
          Determines if there are more waiting steps for which a timeout value can be fetched
 long next()
          Returns the next timeout value based on the time fractions set.
 void setFractions(float[] fractions)
          Sets time fractions for each step.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProgressiveTimeoutGenerator

public ProgressiveTimeoutGenerator(long timeout)
Creates a new instance

Parameters:
timeout - The total time to wait
Method Detail

setFractions

public void setFractions(float[] fractions)
                  throws java.lang.Exception
Sets time fractions for each step. The total steps equal the length of the supplied array. Each wait period is computed by dividing the time left after the previous step by the corresponding fraction. Each fraction should be be greater than or equal to 1.0. If the last fraction is not equal to 1.0, its value is ignored and assumed equal to 1.0.

Parameters:
fractions - The fraction of the time to wait at each step.
Throws:
java.lang.Exception - If the next() or hasNext() method has already been called

next

public long next()
Returns the next timeout value based on the time fractions set. The invocation for the last step returns the total time remaining to reach the total timeout value, regardless of the value of the corresponding fraction.

Returns:
The timeout value for the next waiting step. 0, if the steps

hasNext

public boolean hasNext()
Determines if there are more waiting steps for which a timeout value can be fetched

Returns:
true if a timeout value can be fetched through next(), false otherwise