Class Expiring<Type>
- Type Parameters:
Type
- The class of the value that will expire.
-
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 thecreationTime
.private @NonNull LocalDateTime
The absolute point in time after which the value is considered expired.private Type
The value that will expire. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new expiring object with the given value and expiration duration.Expiring
(Type value, @NonNull LocalDateTime expirationTime) Creates a new expiring object with the given value and expiration time.Expiring
(Type value, @NonNull LocalDateTime expirationTime, @NonNull Duration expirationDuration) Creates a new expiring object with the given value,creationTime
and expiration duration. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Extends both the absoluteexpirationTime
and the relativeexpirationDuration
by the given duration, even if the value has already expired.boolean
extendIfExpired
(@NonNull Duration duration) Extends theexpirationTime
and theexpirationDuration
by the given duration, only if the value has already expired.boolean
extendIfNotExpired
(@NonNull Duration duration) Extends theexpirationTime
and theexpirationDuration
by the given duration, only if the value has not expired.get()
boolean
Determines whether thevalue
has expired, which is the case ifvoid
void
updateExpirationTime
(@NonNull LocalDateTime expirationTime) Updates the absoluteexpirationTime
to the given time and recalculates the relativeexpirationDuration
.boolean
updateExpirationTimeIfExpired
(@NonNull LocalDateTime expirationTime) Updates the relativeexpirationDuration
to the given duration and recalculates the absoluteexpirationTime
, only if the value has already expired.boolean
updateExpirationTimeIfNotExpired
(@NonNull LocalDateTime expirationTime) Updates the absoluteexpirationTime
to the given time and recalculates the relativeexpirationDuration
,boolean
updateIfExpired
(Type value) Updates thevalue
only if it has already expired.boolean
updateIfNotExpired
(Type value) Updates thevalue
only if it has not yet expired.
-
Field Details
-
value
The value that will expire. -
creationTime
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 thecreationTime
.
-
-
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 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
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
expirationTime
or - the current time is after the
creationTime
plus theexpirationDuration
.
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.
- the current time is after the
-
getEvenIfExpired
- Returns:
- The stored
value
, even if ithasExpired()
.
-
hasExpired
@Contract(pure=true) public boolean hasExpired()Determines whether the
value
has expired, which is the case if- the current time is after the
expirationTime
or - the current time is after the
creationTime
plus theexpirationDuration
.
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.
- the current time is after the
-
update
- Parameters:
value
- The new value.
-
updateIfExpired
Updates thevalue
only if it has already expired.- Parameters:
value
- The new value.- Returns:
- True if the value has been updated, false otherwise.
-
updateIfNotExpired
Updates thevalue
only if it has not yet expired.- Parameters:
value
- The new value.- Returns:
- True if the value has been updated, false otherwise.
-
updateExpirationTime
Updates the absoluteexpirationTime
to the given time and recalculates the relativeexpirationDuration
.- Parameters:
expirationTime
- The new absolute expiration time.
-
updateExpirationTimeIfExpired
Updates the relativeexpirationDuration
to the given duration and recalculates the absoluteexpirationTime
, 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
Updates the absoluteexpirationTime
to the given time and recalculates the relativeexpirationDuration
, 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
Extends both the absoluteexpirationTime
and the relativeexpirationDuration
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
Extends theexpirationTime
and theexpirationDuration
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
Extends theexpirationTime
and theexpirationDuration
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:
-