com.github.nagyesta.lowkeyvault.service.common.impl.BaseLifetimePolicy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lowkey-vault-app Show documentation
Show all versions of lowkey-vault-app Show documentation
Assembled application of Lowkey Vault.
package com.github.nagyesta.lowkeyvault.service.common.impl;
import com.github.nagyesta.lowkeyvault.service.EntityId;
import com.github.nagyesta.lowkeyvault.service.common.TimeAware;
import lombok.Data;
import lombok.NonNull;
import org.springframework.util.Assert;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
@Data
public class BaseLifetimePolicy implements TimeAware {
private final E id;
@NonNull
private OffsetDateTime createdOn;
@NonNull
private OffsetDateTime updatedOn;
protected BaseLifetimePolicy(@NonNull final E id) {
this.id = id;
this.createdOn = OffsetDateTime.now();
this.updatedOn = OffsetDateTime.now();
}
@Override
public void timeShift(final int offsetSeconds) {
Assert.isTrue(offsetSeconds > 0, "Offset must be positive.");
createdOn = createdOn.minusSeconds(offsetSeconds);
updatedOn = updatedOn.minusSeconds(offsetSeconds);
}
protected void markUpdate() {
updatedOn = OffsetDateTime.now();
}
protected List collectMissedTriggerDays(
final Function triggerAfterDaysFunction,
final OffsetDateTime startPoint) {
final OffsetDateTime now = OffsetDateTime.now(ZoneOffset.UTC);
final List rotationTimes = new ArrayList<>();
OffsetDateTime latestDay = startPoint;
while (latestDay.plusDays(triggerAfterDaysFunction.apply(latestDay)).isBefore(now)) {
latestDay = latestDay.plusDays(triggerAfterDaysFunction.apply(latestDay));
rotationTimes.add(latestDay);
}
return List.copyOf(rotationTimes);
}
protected OffsetDateTime findTriggerTimeOffset(
final OffsetDateTime entityCreation,
final Function triggerAfterDaysFunction) {
final OffsetDateTime relativeToLifetimeActionPolicy = createdOn.minusDays(triggerAfterDaysFunction.apply(createdOn));
OffsetDateTime startPoint = entityCreation;
if (entityCreation.isBefore(relativeToLifetimeActionPolicy)) {
startPoint = relativeToLifetimeActionPolicy;
}
return startPoint;
}
}