| 1 | package com.reallifedeveloper.common.infrastructure.persistence; | |
| 2 | ||
| 3 | import java.time.ZonedDateTime; | |
| 4 | import java.util.Optional; | |
| 5 | ||
| 6 | import org.checkerframework.checker.nullness.qual.Nullable; | |
| 7 | ||
| 8 | import jakarta.persistence.Column; | |
| 9 | import jakarta.persistence.MappedSuperclass; | |
| 10 | ||
| 11 | import com.reallifedeveloper.common.domain.ErrorHandling; | |
| 12 | import com.reallifedeveloper.common.domain.registry.CommonDomainRegistry; | |
| 13 | ||
| 14 | /** | |
| 15 | * A base class for JPA entities that need to keep track of when they where created and when they were last updated. | |
| 16 | * | |
| 17 | * @author RealLifeDeveloper | |
| 18 | * | |
| 19 | * @param <ID> the type of the primary key | |
| 20 | */ | |
| 21 | @MappedSuperclass | |
| 22 | public class TimestampedJpaEntity<ID> extends BaseJpaEntity<ID> { | |
| 23 | ||
| 24 | @Column(name = "created", nullable = false) | |
| 25 | private @Nullable ZonedDateTime created; | |
| 26 | ||
| 27 | @Column(name = "updated", nullable = false) | |
| 28 | private @Nullable ZonedDateTime updated; | |
| 29 | ||
| 30 | /** | |
| 31 | * Creates a new {@code AbstractTimestampedJpaEntity} with null ID and with the created and updated timestamps also set to null. | |
| 32 | * <p> | |
| 33 | * This constructor should normally only be used by Hibernate, and you are encouraged to create a no-argument package private | |
| 34 | * constructor in your entities that call this. | |
| 35 | */ | |
| 36 | protected TimestampedJpaEntity() { | |
| 37 | super(); | |
| 38 | } | |
| 39 | ||
| 40 | /** | |
| 41 | * Creates a new {@code AbstractTimestampedJpaEntity} with the given ID and with the created and updated timestamps set to the current | |
| 42 | * time. | |
| 43 | * | |
| 44 | * @param id the ID of the new entity | |
| 45 | */ | |
| 46 | protected TimestampedJpaEntity(ID id) { | |
| 47 | this(id, CommonDomainRegistry.timeService().now(), CommonDomainRegistry.timeService().now()); | |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * Creates a new {@code AbstractTimestampedJpaEntity} with the given ID and timestamps. | |
| 52 | * | |
| 53 | * @param id the ID of the new entity | |
| 54 | * @param created the time and date the entity was created | |
| 55 | * @param updated the time and date the entity was last updated | |
| 56 | * | |
| 57 | * @throws IllegalArgumentException if {@code created} or {@code updated} is {@code null} | |
| 58 | */ | |
| 59 | protected TimestampedJpaEntity(ID id, ZonedDateTime created, ZonedDateTime updated) { | |
| 60 | super(id); | |
| 61 |
1
1. <init> : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNull → KILLED |
ErrorHandling.checkNull("Arguments must not be null: id=" + id + ", created=%s, updated=%s", created, updated); |
| 62 | this.created = created; | |
| 63 | this.updated = updated; | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Gives the time and date that the entity was created. | |
| 68 | * | |
| 69 | * @return the timestamp of creation | |
| 70 | */ | |
| 71 | public Optional<ZonedDateTime> created() { | |
| 72 |
1
1. created : replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/TimestampedJpaEntity::created → KILLED |
return Optional.ofNullable(created); |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Gives the time and date that the entity was last updated. | |
| 77 | * | |
| 78 | * @return the timestamp of last update | |
| 79 | */ | |
| 80 | public Optional<ZonedDateTime> updated() { | |
| 81 |
1
1. updated : replaced return value with Optional.empty for com/reallifedeveloper/common/infrastructure/persistence/TimestampedJpaEntity::updated → KILLED |
return Optional.ofNullable(updated); |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Sets the time and date that the entity was last updated. | |
| 86 | * | |
| 87 | * @param updated the timestamp of last update | |
| 88 | * @throws IllegalArgumentException if {@code updated} is {@code null} | |
| 89 | */ | |
| 90 | public void setUpdated(ZonedDateTime updated) { | |
| 91 |
1
1. setUpdated : removed call to com/reallifedeveloper/common/domain/ErrorHandling::checkNull → KILLED |
ErrorHandling.checkNull("updated must not be null", updated); |
| 92 | this.updated = updated; | |
| 93 | } | |
| 94 | } | |
Mutations | ||
| 61 |
1.1 |
|
| 72 |
1.1 |
|
| 81 |
1.1 |
|
| 91 |
1.1 |