Class Expiring<Type>
- Type Parameters:
Type- The class of the value that will expire.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final @NonNull LocalDateTimeThe absolute point in time at which the value was created.private @NonNull DurationThe relative (initial) duration after which the value is considered expired, counting from thecreationTime.private @NonNull LocalDateTimeThe absolute point in time after which the value is considered expired.private TypeThe 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,creationTimeand expiration duration. -
Method Summary
Modifier and TypeMethodDescriptionvoidExtends both the absoluteexpirationTimeand the relativeexpirationDurationby the given duration, even if the value has already expired.booleanextendIfExpired(@NonNull Duration duration) Extends theexpirationTimeand theexpirationDurationby the given duration, only if the value has already expired.booleanextendIfNotExpired(@NonNull Duration duration) Extends theexpirationTimeand theexpirationDurationby the given duration, only if the value has not expired.get()booleanDetermines whether thevaluehas expired, which is the case ifvoidvoidupdateExpirationTime(@NonNull LocalDateTime expirationTime) Updates the absoluteexpirationTimeto the given time and recalculates the relativeexpirationDuration.booleanupdateExpirationTimeIfExpired(@NonNull LocalDateTime expirationTime) Updates the relativeexpirationDurationto the given duration and recalculates the absoluteexpirationTime, only if the value has already expired.booleanupdateExpirationTimeIfNotExpired(@NonNull LocalDateTime expirationTime) Updates the absoluteexpirationTimeto the given time and recalculates the relativeexpirationDuration,booleanupdateIfExpired(Type value) Updates thevalueonly if it has already expired.booleanupdateIfNotExpired(Type value) Updates thevalueonly 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 (initial) 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,
creationTimeand expiration duration.If
expirationTimeandexpirationDurationdiffer, 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
valueif it has not expired yet, otherwise an emptyOptional. The value is considered expired if- the current time is after the
expirationTimeor - the current time is after the
creationTimeplus 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
-
getRemainingDuration
Determines the remainingDurationbetween now and theexpirationTime, i.e. the remaining time (from now) until thevalueexpires.- Returns:
- The remaining duration until expiration. Empty if the value has already expired.
-
getEvenIfExpired
- Returns:
- The stored
value, even if ithasExpired().
-
hasExpired
@Contract(pure=true) public boolean hasExpired()Determines whether the
valuehas expired, which is the case if- the current time is after the
expirationTimeor - the current time is after the
creationTimeplus 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 thevalueonly if it has already expired.- Parameters:
value- The new value.- Returns:
- True if the value has been updated, false otherwise.
-
updateIfNotExpired
Updates thevalueonly if it has not yet expired.- Parameters:
value- The new value.- Returns:
- True if the value has been updated, false otherwise.
-
updateExpirationTime
Updates the absoluteexpirationTimeto the given time and recalculates the relativeexpirationDuration.- Parameters:
expirationTime- The new absolute expiration time.
-
updateExpirationTimeIfExpired
Updates the relativeexpirationDurationto 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 absoluteexpirationTimeto 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 absoluteexpirationTimeand the relativeexpirationDurationby 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 theexpirationTimeand theexpirationDurationby 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 theexpirationTimeand theexpirationDurationby 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:
-