cdm.event.common.functions.Create_PartialDeliveryPrimitiveInstruction Maven / Gradle / Ivy
package cdm.event.common.functions;
import cdm.base.math.QuantityChangeDirectionEnum;
import cdm.base.staticdata.identifier.Identifier;
import cdm.event.common.PrimitiveInstruction;
import cdm.event.common.PrimitiveInstruction.PrimitiveInstructionBuilder;
import cdm.event.common.QuantityChangeInstruction;
import cdm.event.common.Trade;
import cdm.event.common.TradeState;
import cdm.product.common.settlement.PriceQuantity;
import cdm.product.template.ContractualProduct;
import cdm.product.template.Product;
import cdm.product.template.TradableProduct;
import cdm.product.template.TradeLot;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.functions.ConditionValidator;
import com.rosetta.model.lib.functions.ModelObjectValidator;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.mapper.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
import static com.rosetta.model.lib.expression.ExpressionOperators.*;
@ImplementedBy(Create_PartialDeliveryPrimitiveInstruction.Create_PartialDeliveryPrimitiveInstructionDefault.class)
public abstract class Create_PartialDeliveryPrimitiveInstruction implements RosettaFunction {
@Inject protected ConditionValidator conditionValidator;
@Inject protected ModelObjectValidator objectValidator;
// RosettaFunction dependencies
//
@Inject protected Create_TerminationInstruction create_TerminationInstruction;
/**
* @param tradeState The original trade to be closed.
* @param deliveredPriceQuantity The price and quantity of the delivered amount.
* @return instruction
*/
public PrimitiveInstruction evaluate(TradeState tradeState, List extends PriceQuantity> deliveredPriceQuantity) {
// pre-conditions
conditionValidator.validate(() ->
exists(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getTradableProduct", trade -> trade.getTradableProduct()).map("getProduct", tradableProduct -> tradableProduct.getProduct()).map("getContractualProduct", product -> product.getContractualProduct())),
"This function applies to contractual products only.");
PrimitiveInstruction.PrimitiveInstructionBuilder instructionBuilder = doEvaluate(tradeState, deliveredPriceQuantity);
final PrimitiveInstruction instruction;
if (instructionBuilder == null) {
instruction = null;
} else {
instruction = instructionBuilder.build();
objectValidator.validate(PrimitiveInstruction.class, instruction);
}
return instruction;
}
protected abstract PrimitiveInstruction.PrimitiveInstructionBuilder doEvaluate(TradeState tradeState, List extends PriceQuantity> deliveredPriceQuantity);
public static class Create_PartialDeliveryPrimitiveInstructionDefault extends Create_PartialDeliveryPrimitiveInstruction {
@Override
protected PrimitiveInstruction.PrimitiveInstructionBuilder doEvaluate(TradeState tradeState, List extends PriceQuantity> deliveredPriceQuantity) {
PrimitiveInstruction.PrimitiveInstructionBuilder instruction = PrimitiveInstruction.builder();
return assignOutput(instruction, tradeState, deliveredPriceQuantity);
}
protected PrimitiveInstruction.PrimitiveInstructionBuilder assignOutput(PrimitiveInstruction.PrimitiveInstructionBuilder instruction, TradeState tradeState, List extends PriceQuantity> deliveredPriceQuantity) {
instruction
.getOrCreateSplit()
.setBreakdown(MapperC.of(MapperS.of(create_TerminationInstruction.evaluate(MapperS.of(tradeState).get()))).getMulti());
instruction
.getOrCreateSplit()
.setBreakdown(MapperC.of(MapperS.of(PrimitiveInstruction.builder()
.setQuantityChange(MapperS.of(QuantityChangeInstruction.builder()
.setChange(MapperC.of(deliveredPriceQuantity).getMulti())
.setDirection(MapperS.of(QuantityChangeDirectionEnum.REPLACE).get())
.setLotIdentifier(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getTradableProduct", trade -> trade.getTradableProduct()).mapC("getTradeLot", tradableProduct -> tradableProduct.getTradeLot()).mapC("getLotIdentifier", tradeLot -> tradeLot.getLotIdentifier()).getMulti())
.build())
.get())
.build())
).getMulti());
return Optional.ofNullable(instruction)
.map(o -> o.prune())
.orElse(null);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy