ca.ibodrov.mica.api.model.Entity Maven / Gradle / Ivy
package ca.ibodrov.mica.api.model;
import ca.ibodrov.mica.api.validation.ValidName;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import javax.validation.constraints.NotNull;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* @implNote the changes in fields and annotations here must be synchronized
* with {@link PartialEntity}
*/
@JsonInclude(JsonInclude.Include.NON_ABSENT)
public record Entity(@NotNull EntityId id,
@ValidName String name,
@ValidName String kind,
@NotNull Instant createdAt,
@NotNull Instant updatedAt,
@JsonProperty("__data") @JsonAnySetter @JsonAnyGetter @NotNull Map data)
implements EntityLike {
public Entity {
data = new HashMap<>(data != null ? data : Map.of()); // has to be mutable to support @JsonAnySetter
}
@JsonAnyGetter
public JsonNode getProperty(String name) {
return data.get(name);
}
@JsonAnySetter
public void setProperty(String name, JsonNode value) {
data.put(name, value);
}
public EntityLike asEntityLike() {
return this;
}
@Override
public EntityLike withName(String name) {
return new Entity(id, name, kind, createdAt, updatedAt, data);
}
@Override
public Optional versionAndName() {
return Optional.of(new EntityVersionAndName(id, updatedAt, name));
}
}