com.kenshoo.pl.entity.internal.audit.AuditFieldChangeGenerator 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.audit;
import com.kenshoo.pl.entity.CurrentEntityState;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.FinalEntityState;
import com.kenshoo.pl.entity.audit.FieldAuditRecord;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
public class AuditFieldChangeGenerator {
> Optional> generate(final CurrentEntityState currentState,
final FinalEntityState finalState,
final EntityField field) {
requireNonNull(currentState, "A current state is required");
requireNonNull(finalState, "A final state is required");
requireNonNull(field, "A field is required");
return Optional.of(field)
.filter(f -> fieldWasChanged(currentState, finalState, f))
.map(f -> buildFieldRecord(currentState, finalState, f));
}
private , T> boolean fieldWasChanged(final CurrentEntityState currentState,
final FinalEntityState finalState,
final EntityField field) {
return !fieldStayedTheSame(currentState, finalState, field);
}
private , T> boolean fieldStayedTheSame(final CurrentEntityState currentState,
final FinalEntityState finalState,
final EntityField field) {
return currentState.safeGet(field).equals(finalState.safeGet(field), field::valuesEqual);
}
private > FieldAuditRecord buildFieldRecord(final CurrentEntityState currentState,
final FinalEntityState finalState,
final EntityField field) {
final FieldAuditRecord.Builder fieldRecordBuilder = FieldAuditRecord.builder(field);
currentState.safeGet(field).ifNotNull(fieldRecordBuilder::oldValue);
finalState.safeGet(field).ifNotNull(fieldRecordBuilder::newValue);
return fieldRecordBuilder.build();
}
}