org.curjent.impl.agent
Class ExpirationsTimer

java.lang.Object
  extended by org.curjent.impl.agent.ExpirationsTimer
All Implemented Interfaces:
Comparable<ExpirationsTimer>

final class ExpirationsTimer
extends Object
implements Comparable<ExpirationsTimer>

Expiration timer. Holds the message's expiration state. The cleanup and thread-safety strategy is to declare state values as final and create a new timer when the configuration changes. When a timer is no longer valid, its message attribute is cleared. This is preferable to removing the timer from its queue due to the high overhead of this operation as implemented by PriorityQueue.remove(Object). It also helps minimize the number of times the agent is locked.

The calculation of the expiration time is customized, so the logic and utility methods for these calculations are embedded in this class, too.


Field Summary
private static long BASE
          Relative system nanoseconds at the time this class is initialized.
private  long expires
          Time in nanoseconds when this message expires.
(package private)  Message message
          Owning message configured for expiration.
private  long sequence
          Ordered sequence value for this timer.
 
Constructor Summary
ExpirationsTimer(Message message, long expires, long sequence)
          Constructs a new timer with the given fixed values.
 
Method Summary
 int compareTo(ExpirationsTimer other)
          Returns -1 if this timer expires before the other timer, 1 if after, or 0 if they expire at the same time.
(package private) static long expires(long timeout)
          Computes the future expiration time in nanoseconds relative to the "current" time.
(package private) static long expires(long timeout, TimeUnit unit)
          Converts the given timeout value to nanoseconds and returns the future expiration time in nanoseconds relative to the "current" time.
(package private) static long now()
          Returns the "current" time in nanoseconds.
(package private)  long timeout()
          Computes the timeout value in nanoseconds for this timer's expiration time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

message

volatile Message message
Owning message configured for expiration. Cleared when this timer is no longer valid, either because the configuration has changed or the message has finished.


expires

private final long expires
Time in nanoseconds when this message expires. Expiration times range from Long.MIN_VALUE to Long.MAX_VALUE.


sequence

private final long sequence
Ordered sequence value for this timer. This is used for breaking ties between two messages that have the same expires value. Each expiration timer receives a strictly increasing sequence number for each agent. Values range from Long.MIN_VALUE to Long.MAX_VALUE.


BASE

private static final long BASE
Relative system nanoseconds at the time this class is initialized. This may have any negative or positive value from Long.MIN_VALUE to Long.MAX_VALUE (-9223372036854775808 to +9223372036854775807). This value is used to anchor future System.nanoTime() values from a known starting point such that expiration calculations do not overflow.

See Also:
System.nanoTime(), now()
Constructor Detail

ExpirationsTimer

ExpirationsTimer(Message message,
                 long expires,
                 long sequence)
Constructs a new timer with the given fixed values. The expires and sequence attributes are pre-computed prior to constuction in order to enable computations and error checking prior to locking the agent.

Method Detail

now

static long now()
Returns the "current" time in nanoseconds. This value starts at Long.MIN_VALUE when this class is first initialized. This method is for computing future values relative to this initial starting value. This allows for large timeouts of up to Long.MAX_VALUE without overflow (computations of past values, in contrast, may underflow).

See Also:
expires(long, TimeUnit)

expires

static long expires(long timeout)
Computes the future expiration time in nanoseconds relative to the "current" time. Return values may range from Long.MIN_VALUE to Long.MAX_VALUE depending on the current time and the given timeout value. For example, if the current time is -1000 and the timeout value is 100, the expiration time is -900. If the timeout value is 5000, the expiration time is 4000 (in other words, the expiration time will have been reached once the value returned from now() is 4000 or higher).

See Also:
now()

expires

static long expires(long timeout,
                    TimeUnit unit)
Converts the given timeout value to nanoseconds and returns the future expiration time in nanoseconds relative to the "current" time.

Throws:
IllegalArgumentException - timeout is negative.
NullPointerException - unit is required but null. unit is optional and may be null if timeout is 0 or Long.MAX_VALUE.
See Also:
expires(long)

timeout

long timeout()
Computes the timeout value in nanoseconds for this timer's expiration time. Returns 0 if the timer has expired.


compareTo

public int compareTo(ExpirationsTimer other)
Returns -1 if this timer expires before the other timer, 1 if after, or 0 if they expire at the same time.

Specified by:
compareTo in interface Comparable<ExpirationsTimer>


Copyright 2009-2011 Tom Landon
Apache License 2.0