java.lang.Object
page.codeberg.friedolyn.util.Expiring<T>
- Type Parameters:
T- The type of the value that will expire.
A class that holds a value that will expire after a certain point in time or after a certain duration.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final @NonNull LocalDateTimeThe absolute point in time at which the value was created.private @NonNull DurationThe relative duration after which the value is considered expired, counting from theCREATION_TIME.private @NonNull LocalDateTimeThe absolute point in time after which the value is considered expired.private TThe value that will expire. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new expiring object with the given value and expiration duration.Expiring(T value, @NonNull LocalDateTime expirationTime) Creates a new expiring object with the given value and expiration time.Expiring(T value, @NonNull LocalDateTime expirationTime, @NonNull Duration expirationDuration) Creates a new expiring object with the given value, creation time and expiration duration. -
Method Summary
Modifier and TypeMethodDescriptionvoidExtends both the absolute expiration time and the relative expiration duration by the given duration, even if the value has already expired.booleanextendIfExpired(@NonNull Duration duration) Extends the expiration time and the expiration duration by the given duration, only if the value has already expired.booleanextendIfNotExpired(@NonNull Duration duration) Extends the expiration time and the expiration duration by the given duration, only if the value has not expired.get()Returns the value if it has not expired yet, otherwise an emptyOptional.booleanvoidUpdates thevaluewithout modifying the expiration time or duration.voidupdateExpirationTime(@NonNull LocalDateTime expirationTime) Updates the absolute expiration time to the given time and recalculates the relative expiration duration.booleanupdateExpirationTimeIfExpired(@NonNull LocalDateTime expirationTime) Updates the relative expiration duration to the given duration and recalculates the absolute expiration time, only if the value has already expired.booleanupdateExpirationTimeIfNotExpired(@NonNull LocalDateTime expirationTime) Updates the absolute expiration time to the given time and recalculates the relative expiration duration, only if the value has not yet expired.booleanupdateIfExpired(T value) Updates thevalueonly if it has already expired.booleanupdateIfNotExpired(T value) Updates thevalueonly if it has not yet expired.
-
Field Details
-
value
The value that will expire. -
CREATION_TIME
The absolute point in time at which the value was created. -
expirationTime
The absolute point in time after which the value is considered expired. -
expirationDuration
The relative duration after which the value is considered expired, counting from theCREATION_TIME.
-
-
Constructor Details
-
Expiring
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
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 T value, @NonNull @NonNull LocalDateTime expirationTime, @NonNull @NonNull Duration expirationDuration) Creates a new expiring object with the given value, creation time and expiration duration. IfexpirationTimeandexpirationDurationdiffer, the minimum of both will be used. That means,get()will returnOptional.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
Returns the value if it has not expired yet, otherwise an emptyOptional. The value is considered expired if- the current time is after the expiration time or
- the current time is after the creation time plus the expiration duration
- Returns:
- The value if it has not expired yet, otherwise an empty
Optional.
-
getEvenIfExpired
- Returns:
- The stored
value, even if ithasExpired().
-
hasExpired
public boolean hasExpired()- Returns:
trueif the absoluteexpirationTimeor the relativeexpirationDurationhas been reached,falseif neither has been reached yet.
-
update
-
updateIfExpired
-
updateIfNotExpired
-
updateExpirationTime
Updates the absolute expiration time to the given time and recalculates the relative expiration duration.- Parameters:
expirationTime- The new absoluteexpirationTime
-
updateExpirationTimeIfExpired
Updates the relative expiration duration to the given duration and recalculates the absolute expiration time, only if the value has already expired.- Parameters:
expirationTime- The new absoluteexpirationTime.- Returns:
trueif the expiration time and duration have been updated,falseotherwise.
-
updateExpirationTimeIfNotExpired
Updates the absolute expiration time to the given time and recalculates the relative expiration duration, only if the value has not yet expired.- Parameters:
expirationTime- The new absoluteexpirationTime.- Returns:
trueif the expiration time and duration have been updated,falseotherwise.
-
extendBy
Extends both the absolute expiration time and the relative expiration duration by the given duration, even if the value has already expired.- Parameters:
duration- The duration by which to extend theexpirationTimeand theexpirationDuration.- See Also:
-
extendIfExpired
Extends the expiration time and the expiration duration by the given duration, only if the value has already expired.- Parameters:
duration- The duration by which to extend theexpirationTimeand theexpirationDuration.- Returns:
trueif the value has been extended,falseotherwise.- See Also:
-
extendIfNotExpired
Extends the expiration time and the expiration duration by the given duration, only if the value has not expired.- Parameters:
duration- The duration by which to extend theexpirationTimeand theexpirationDuration.- Returns:
trueif the value has been extended,falseotherwise.- See Also:
-