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 LocalDateTime
The absolute point in time at which the value was created.private @NonNull Duration
The relative duration after which the value is considered expired, counting from theCREATION_TIME
.private @NonNull LocalDateTime
The absolute point in time after which the value is considered expired.private T
The 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 TypeMethodDescriptionvoid
Extends both the absolute expiration time and the relative expiration duration by the given duration, even if the value has already expired.boolean
extendIfExpired
(@NonNull Duration duration) Extends the expiration time and the expiration duration by the given duration, only if the value has already expired.boolean
extendIfNotExpired
(@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
.boolean
void
Updates thevalue
without modifying the expiration time or duration.void
updateExpirationTime
(@NonNull LocalDateTime expirationTime) Updates the absolute expiration time to the given time and recalculates the relative expiration duration.boolean
updateExpirationTimeIfExpired
(@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.boolean
updateExpirationTimeIfNotExpired
(@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.boolean
updateIfExpired
(T value) Updates thevalue
only if it has already expired.boolean
updateIfNotExpired
(T value) Updates thevalue
only 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. IfexpirationTime
andexpirationDuration
differ, 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:
true
if the absoluteexpirationTime
or the relativeexpirationDuration
has been reached,false
if 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:
true
if the expiration time and duration have been updated,false
otherwise.
-
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:
true
if the expiration time and duration have been updated,false
otherwise.
-
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 theexpirationTime
and 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 theexpirationTime
and theexpirationDuration
.- Returns:
true
if the value has been extended,false
otherwise.- 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 theexpirationTime
and theexpirationDuration
.- Returns:
true
if the value has been extended,false
otherwise.- See Also:
-