Class TimeToLive

  • All Implemented Interfaces:
    java.io.Serializable, oracle.kv.impl.util.FastExternalizable

    public final class TimeToLive
    extends java.lang.Object
    implements java.io.Serializable, oracle.kv.impl.util.FastExternalizable
    TimeToLive is a utility class that represents a period of time, similar to Java 8's java.time.Duration, but specialized to the needs of Oracle NoSQL Database.

    This class is restricted to durations of days and hours. It is only used as input related to time to live (TTL) for Row instances, set by using Row.setTTL(oracle.kv.table.TimeToLive). Construction allows only day and hour durations for efficiency reasons. Durations of days are recommended as they result in the least amount of storage consumed in a store.

    Only positive durations are allowed on input, although negative durations can be returned from fromExpirationTime(long, long) if the expirationTime is in the past relative to the referenceTime.

    Since:
    4.0
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.concurrent.TimeUnit convertTimeToLiveUnit​(int ttlVal, byte unitOrdinal)
      Converts an ordinal value for a TTL TimeUnit into the TimeUnit.
      boolean equals​(java.lang.Object other)
      Equality compares the duration only if the units used for construction are the same.
      static TimeToLive fromExpirationTime​(long expirationTime, long referenceTime)
      Returns an instance of TimeToLive based on an absolute expiration time and a reference time.
      static java.util.concurrent.TimeUnit getTTLUnit​(TimeToLive ttl)
      Returns the TimeUnit associated with a TimeToLive, or {TimeUnit#DAYS} if the ttl is null.
      static int getTTLValue​(TimeToLive ttl)
      Returns the numeric duration value associated with a TimeToLive, or 0 if the ttl is null.
      int hashCode()  
      static TimeToLive ofDays​(long days)
      Creates a duration using a period of 24 hour days.
      static TimeToLive ofHours​(long hours)
      Creates a duration using a period of hours.
      long toDays()
      Returns the number of days in this duration, which may be negative.
      long toExpirationTime​(long referenceTime)
      Returns an absolute time representing the duration plus the absolute time reference parameter.
      long toHours()
      Returns the number of hours in this duration, which may be negative.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface oracle.kv.impl.util.FastExternalizable

        deserializedForm
    • Method Detail

      • ofHours

        public static TimeToLive ofHours​(long hours)
        Creates a duration using a period of hours.
        Parameters:
        hours - the number of hours in the duration, must be a non-negative number
        Returns:
        the duration
        Throws:
        java.lang.IllegalArgumentException - if a negative value is provided
      • ofDays

        public static TimeToLive ofDays​(long days)
        Creates a duration using a period of 24 hour days.
        Parameters:
        days - the number of days in the duration, must be a non-negative number
        Returns:
        the duration
        Throws:
        java.lang.IllegalArgumentException - if a negative value is provided
      • toDays

        public long toDays()
        Returns the number of days in this duration, which may be negative.
        Returns:
        the number of days
      • toHours

        public long toHours()
        Returns the number of hours in this duration, which may be negative.
        Returns:
        the number of hours
      • toExpirationTime

        public long toExpirationTime​(long referenceTime)
        Returns an absolute time representing the duration plus the absolute time reference parameter. If an expiration time from the current time is desired the parameter should be System.currentTimeMillis(). If the duration of this object is 0 (DO_NOT_EXPIRE), indicating no expiration time, this method will return 0, regardless of the reference time.
        Parameters:
        referenceTime - an absolute time in milliseconds since January 1, 1970.
        Returns:
        time in milliseconds, 0 if this object's duration is 0
      • fromExpirationTime

        public static TimeToLive fromExpirationTime​(long expirationTime,
                                                    long referenceTime)
        Returns an instance of TimeToLive based on an absolute expiration time and a reference time. If a duration relative to the current time is desired the referenceTime should be System.currentTimeMillis(). If the expirationTime is 0, the referenceTime is ignored and a TimeToLive of duration 0 is created, indicating no expiration.

        Days will be use as the primary unit of duration if the expiration time is evenly divisible into days, otherwise hours are used.

        Parameters:
        expirationTime - an absolute time in milliseconds since January 1, 1970
        referenceTime - an absolute time in milliseconds since January 1, 1970.
        Returns:
        a TimeToLive instance
      • equals

        public boolean equals​(java.lang.Object other)
        Equality compares the duration only if the units used for construction are the same. If the units (ofHours vs ofDays) are different two instances will not be equal even if their absolute durations are the same unless both values are 0, which means no expiration.
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • convertTimeToLiveUnit

        public static java.util.concurrent.TimeUnit convertTimeToLiveUnit​(int ttlVal,
                                                                          byte unitOrdinal)
        Converts an ordinal value for a TTL TimeUnit into the TimeUnit.
      • getTTLValue

        public static int getTTLValue​(TimeToLive ttl)
        Returns the numeric duration value associated with a TimeToLive, or 0 if the ttl is null.
        Parameters:
        ttl - the ttl or null
        Returns:
        the numeric duration value
      • getTTLUnit

        public static java.util.concurrent.TimeUnit getTTLUnit​(TimeToLive ttl)
        Returns the TimeUnit associated with a TimeToLive, or {TimeUnit#DAYS} if the ttl is null.
        Parameters:
        ttl - the ttl or null
        Returns:
        the time unit