com.kenshoo.pl.entity.spi.FieldValueSupplier 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.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, OLD_VAL> 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, T1> field1, EntityField, T2> 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);
}
};
}
}