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.observable.asset.PriceQuantity;
import cdm.product.template.TradeLot;
import com.google.inject.ImplementedBy;
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
@ImplementedBy(Create_PartialDeliveryPrimitiveInstruction.Create_PartialDeliveryPrimitiveInstructionDefault.class)
public abstract class Create_PartialDeliveryPrimitiveInstruction implements RosettaFunction {
@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) {
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) {
if (deliveredPriceQuantity == null) {
deliveredPriceQuantity = Collections.emptyList();
}
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(tradeState))).getMulti());
instruction
.getOrCreateSplit()
.setBreakdown(MapperC.of(MapperS.of(PrimitiveInstruction.builder()
.setQuantityChange(QuantityChangeInstruction.builder()
.setChange(new ArrayList(deliveredPriceQuantity))
.setDirection(QuantityChangeDirectionEnum.REPLACE)
.setLotIdentifier(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getTradeLot", trade -> trade.getTradeLot()).mapC("getLotIdentifier", tradeLot -> tradeLot.getLotIdentifier()).getMulti())
.build())
.build())).getMulti());
return Optional.ofNullable(instruction)
.map(o -> o.prune())
.orElse(null);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy