com.kenshoo.pl.entity.internal.EntityIdExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence-layer Show documentation
Show all versions of persistence-layer Show documentation
A Java persistence layer based on JOOQ for high performance and business flow support.
package com.kenshoo.pl.entity.internal;
import com.kenshoo.pl.entity.*;
import java.util.Optional;
import static com.kenshoo.pl.entity.internal.Triptionals.firstPresent;
import static java.util.Objects.requireNonNull;
public class EntityIdExtractor {
public static final EntityIdExtractor INSTANCE = new EntityIdExtractor();
public > Optional extract(final EntityChange entityChange,
final CurrentEntityState currentState) {
requireNonNull(entityChange, "entityChange is required");
requireNonNull(currentState, "currentState is required");
return entityChange.getEntityType()
.getIdField()
.flatMap(idField -> extract(entityChange, currentState, idField));
}
private , T> Optional extract(final EntityChange entityChange,
final CurrentEntityState currentState,
final EntityField idField) {
return firstPresent(() -> extractFromIdentifier(entityChange, idField),
() -> new FinalEntityState(currentState, entityChange).safeGet(idField))
.mapToOptional(String::valueOf);
}
private , T> Triptional extractFromIdentifier(final EntityChange entityChange,
final EntityField idField) {
return Triptional.of(entityChange.getIdentifier())
.flatMap(identifier -> identifier.safeGet(idField), Triptional::absent);
}
private EntityIdExtractor() {
// singleton
}
}