Class Expiration<T>

  • All Implemented Interfaces:
    java.lang.Comparable<java.util.concurrent.Delayed>, java.util.concurrent.Delayed, Contract<T>

    class Expiration<T>
    extends java.lang.Object
    implements Contract<T>
    A Expiration is used to represent the expiration for a lease. This contains all relevant information for the the lease, such as the keyed resource that has been leased and the duration of the lease. Durations for the contract can be measured in any TimeUnit for convenience.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private T key
      This is the key representing the resource being lease.
      private long time
      This is the expiration time in nanoseconds for this.
    • Constructor Summary

      Constructors 
      Constructor Description
      Expiration​(T key, long lease, java.util.concurrent.TimeUnit scale)
      Constructor for the Expiration object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(java.util.concurrent.Delayed other)
      This is used to compare the specified delay to this delay.
      private int compareTo​(Expiration value)
      This is used to compare the specified delay to this delay.
      long getDelay​(java.util.concurrent.TimeUnit unit)
      This method will return the number of TimeUnit seconds that remain in the contract.
      T getKey()
      This returns the key for the resource this represents.
      private long getTime()
      This method returns the current time in nanoseconds.
      void setDelay​(long delay, java.util.concurrent.TimeUnit unit)
      This method is used to set the number of TimeUnit seconds that should remain within the contract.
      java.lang.String toString()
      This is used to provide a description of the contract that the instance represents.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • time

        private volatile long time
        This is the expiration time in nanoseconds for this.
      • key

        private T key
        This is the key representing the resource being lease.
    • Constructor Detail

      • Expiration

        public Expiration​(T key,
                          long lease,
                          java.util.concurrent.TimeUnit scale)
        Constructor for the Expiration object. This is used to create a contract with an initial expiry period. Once this is created the time is taken and the contract can be issued.
        Parameters:
        key - this is the key that this contract represents
        lease - this is the initial lease duration to be used
        scale - this is the time unit scale that is to be used
    • Method Detail

      • getKey

        public T getKey()
        This returns the key for the resource this represents. This is used when the contract has expired to clean resources associated with the lease. It is passed in to the cleaner as an parameter to the callback. The cleaner is then responsible for cleaning any resources associated with the lease.
        Specified by:
        getKey in interface Contract<T>
        Returns:
        returns the resource key that this represents
      • getDelay

        public long getDelay​(java.util.concurrent.TimeUnit unit)
        This method will return the number of TimeUnit seconds that remain in the contract. If the value returned is less than or equal to zero then it should be assumed that the lease has expired, if greater than zero the lease is active.
        Specified by:
        getDelay in interface Contract<T>
        Specified by:
        getDelay in interface java.util.concurrent.Delayed
        Returns:
        returns the duration in the time unit remaining
      • setDelay

        public void setDelay​(long delay,
                             java.util.concurrent.TimeUnit unit)
        This method is used to set the number of TimeUnit seconds that should remain within the contract. This is used when the contract is to be reissued. Once a new duration has been set the contract for the lease has been changed and the previous expiry time is ignores, so only one clean is called.
        Specified by:
        setDelay in interface Contract<T>
        Parameters:
        delay - this is the delay to be used for this contract
        unit - this is the time unit measurment for the delay
      • getTime

        private long getTime()
        This method returns the current time in nanoseconds. This is used to allow the duration of the lease to be calculated with any given time unit which allows flexibility in setting and getting the current delay for the contract.
        Returns:
        returns the current time in nanoseconds remaining
      • compareTo

        public int compareTo​(java.util.concurrent.Delayed other)
        This is used to compare the specified delay to this delay. The result of this operation is used to prioritize contracts in order of first to expire. Contracts that expire first reach the top of the contract queue and are taken off for cleaning.
        Specified by:
        compareTo in interface java.lang.Comparable<T>
        Parameters:
        other - this is the delay to be compared with this
        Returns:
        this returns zero if equal otherwise the difference
      • compareTo

        private int compareTo​(Expiration value)
        This is used to compare the specified delay to this delay. The result of this operation is used to prioritize contracts in order of first to expire. Contracts that expire first reach the top of the contract queue and are taken off for cleaning.
        Parameters:
        value - this is the expiration to be compared with this
        Returns:
        this returns zero if equal otherwise the difference
      • toString

        public java.lang.String toString()
        This is used to provide a description of the contract that the instance represents. A description well contain the key owned by the contract as well as the expiry time expected for it. This is used to provide descriptive messages in the exceptions.
        Specified by:
        toString in interface Contract<T>
        Overrides:
        toString in class java.lang.Object
        Returns:
        a descriptive message describing the contract object