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

com.kenshoo.pl.entity.spi.FieldValueSupplier 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.spi;

import com.kenshoo.pl.entity.ChangeOperation;
import com.kenshoo.pl.entity.CurrentEntityState;
import com.kenshoo.pl.entity.EntityField;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * A "delayed" supplier of the new value for a field that decides on the value given the current state. For example,
 * a logic to increment the current value by 10% can be implemented by implementing this interface. The {@link #supply(CurrentEntityState)}
 * method is going to be called after the current state has been fetched from the database.
 *
 * @param  type of the field whose value is being supplied
 * @see com.kenshoo.pl.entity.ChangeEntityCommand#set(EntityField, FieldValueSupplier)
 */
public interface FieldValueSupplier extends FetchEntityFields {

    /**
     * Returns the new value for a field given an existing entity
     *
     * @param currentState entity before the change
     * @return new field value
     * @throws ValidationException if the supposed change is invalid
     * @throws NotSuppliedException if the supplier doesn't want to change the current value
     */
    T supply(CurrentEntityState currentState) throws ValidationException, NotSuppliedException;

    static  FieldValueSupplier fromOldValue(EntityField field, Function func) {
        return new FieldValueSupplier() {
            @Override
            public NEW_VAL supply(CurrentEntityState oldState) throws ValidationException, NotSuppliedException {
                return func.apply(oldState.get(field));
            }
            @Override
            public Stream> fetchFields(ChangeOperation changeOperation) {
                return Stream.of(field);
            }
        };
    }

    static  FieldValueSupplier fromValues(EntityField field1, EntityField field2, BiFunction func) {
        return new FieldValueSupplier() {
            @Override
            public RES supply(CurrentEntityState oldState) throws ValidationException, NotSuppliedException {
                return func.apply(oldState.get(field1), oldState.get(field2));
            }
            @Override
            public Stream> fetchFields(ChangeOperation changeOperation) {
                return Stream.of(field1, field2);
            }
        };
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy