Class Expiring<Type>

java.lang.Object
de.gustavblass.commons.Expiring<Type>
Type Parameters:
Type - The class of the value that will expire.

public class Expiring<Type> extends Object
A class that holds a value that will expire after a certain point in time or after a certain duration.
  • Field Details

    • value

      @NonNull private Type value
      The value that will expire.
    • creationTime

      @NonNull private final @NonNull LocalDateTime creationTime
      The absolute point in time at which the value was created.
    • expirationTime

      @NonNull private @NonNull LocalDateTime expirationTime
      The absolute point in time after which the value is considered expired.
    • expirationDuration

      @NonNull private @NonNull Duration expirationDuration
      The relative duration after which the value is considered expired, counting from the creationTime.
  • Constructor Details

    • Expiring

      public Expiring(@NonNull Type value, @NonNull @NonNull Duration expirationDuration)
      Creates a new expiring object with the given value and expiration duration.
      Parameters:
      value - The value that will expire.
      expirationDuration - The relative duration after which the value is considered expired, counting from the creation time (now).
    • Expiring

      public Expiring(@NonNull Type value, @NonNull @NonNull LocalDateTime expirationTime)
      Creates a new expiring object with the given value and expiration time.
      Parameters:
      value - The value that will expire.
      expirationTime - The absolute point in time after which the value is considered expired.
    • Expiring

      public Expiring(@NonNull Type value, @NonNull @NonNull LocalDateTime expirationTime, @NonNull @NonNull Duration expirationDuration)

      Creates a new expiring object with the given value, creationTime and expiration duration.

      If expirationTime and expirationDuration differ, the minimum of both will be used. That means, get() will return Optional.empty() if at least one of the two expiration conditions is met.

      Parameters:
      value - The value that will expire.
      expirationTime - The absolute point in time when the value is considered expired.
      expirationDuration - The relative duration after which the value is considered expired, counting from the creation time (now).
  • Method Details

    • get

      @Contract(pure=true) @NonNull public @NonNull Optional<Type> get()

      Returns the value if it has not expired yet, otherwise an empty Optional. The value is considered expired if

      Note that the expiration time and the expiration duration are independent of each other. If at least one of the two conditions is met, the value is considered expired.

      Returns:
      The value if it has not expired yet, otherwise an empty Optional.
    • getEvenIfExpired

      @Contract(pure=true) @NonNull public Type getEvenIfExpired()
      Returns:
      The stored value, even if it hasExpired().
    • hasExpired

      @Contract(pure=true) public boolean hasExpired()

      Determines whether the value has expired, which is the case if

      Note that the expiration time and the expiration duration are independent of each other. If at least one of the two conditions is met, the value is considered expired.

      Returns:
      True if the value has expired, false otherwise.
    • update

      public void update(@NonNull Type value)
      Updates the value without modifying the expirationTime or expirationDuration.
      Parameters:
      value - The new value.
    • updateIfExpired

      public boolean updateIfExpired(@NonNull Type value)
      Updates the value only if it has already expired.
      Parameters:
      value - The new value.
      Returns:
      True if the value has been updated, false otherwise.
    • updateIfNotExpired

      public boolean updateIfNotExpired(@NonNull Type value)
      Updates the value only if it has not yet expired.
      Parameters:
      value - The new value.
      Returns:
      True if the value has been updated, false otherwise.
    • updateExpirationTime

      public void updateExpirationTime(@NonNull @NonNull LocalDateTime expirationTime)
      Updates the absolute expirationTime to the given time and recalculates the relative expirationDuration.
      Parameters:
      expirationTime - The new absolute expiration time.
    • updateExpirationTimeIfExpired

      public boolean updateExpirationTimeIfExpired(@NonNull @NonNull LocalDateTime expirationTime)
      Updates the relative expirationDuration to the given duration and recalculates the absolute expirationTime, only if the value has already expired.
      Parameters:
      expirationTime - The new absolute expiration time.
      Returns:
      True if the expiration time and duration have been updated, false otherwise.
    • updateExpirationTimeIfNotExpired

      public boolean updateExpirationTimeIfNotExpired(@NonNull @NonNull LocalDateTime expirationTime)
      Updates the absolute expirationTime to the given time and recalculates the relative expirationDuration, only if the value has not yet expired.
      Parameters:
      expirationTime - The new absolute expiration time.
      Returns:
      True if the expiration time and duration have been updated, false otherwise.
    • extendBy

      public void extendBy(@NonNull @NonNull Duration duration)
      Extends both the absolute expirationTime and the relative expirationDuration by the given duration, even if the value has already expired.
      Parameters:
      duration - The duration by which to extend the expiration time and the expiration duration.
      See Also:
    • extendIfExpired

      public boolean extendIfExpired(@NonNull @NonNull Duration duration)
      Extends the expirationTime and the expirationDuration by the given duration, only if the value has already expired.
      Parameters:
      duration - The duration by which to extend the expiration time and the expiration duration.
      Returns:
      True if the value has been extended, false otherwise.
      See Also:
    • extendIfNotExpired

      public boolean extendIfNotExpired(@NonNull @NonNull Duration duration)
      Extends the expirationTime and the expirationDuration by the given duration, only if the value has not expired.
      Parameters:
      duration - The duration by which to extend the expiration time and the expiration duration.
      Returns:
      True if the value has been extended, false otherwise.
      See Also: