All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.pl.entity.internal.SingleFieldEnricher Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
package com.kenshoo.pl.entity.internal;

import com.kenshoo.pl.entity.*;
import com.kenshoo.pl.entity.spi.PostFetchCommandEnricher;

import java.util.Collection;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;

abstract public class SingleFieldEnricher, T> implements PostFetchCommandEnricher {

    @Override
    final public void enrich(Collection> changeEntityCommands, ChangeOperation changeOperation, ChangeContext changeContext) {
        changeEntityCommands.stream()
                .filter(this::shouldRunForCommand)
                .filter(additionalCommandFilter())
                .filter(command-> additionalPostFetchCommandFilter().test(command, changeContext.getEntity(command)))
                .forEach(command -> command.set(enrichedField(), enrichedValue(command, changeContext.getEntity(command))));
    }

    @Override
    final public Stream> fieldsToEnrich() {
        return Stream.of(enrichedField());
    }

    @Override
    final public boolean shouldRun(Collection> commands) {
        return commands.stream().filter(additionalCommandFilter()).anyMatch(this::shouldRunForCommand);
    }

    abstract protected EntityField enrichedField();

    abstract protected T enrichedValue(EntityChange entityChange, Entity entity);

    protected Predicate> additionalCommandFilter() {
        return entityChange -> true;
    }

    protected BiPredicate, Entity> additionalPostFetchCommandFilter() {
        return (entityChange, entity) -> true;
    }

    protected boolean shouldRunForCommand(EntityChange entityChange) {
        return enrichedFieldIsMissing(entityChange);
    }

    private boolean enrichedFieldIsMissing(EntityChange entityChange) {
        return !entityChange.isFieldChanged(enrichedField());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy