cdm.event.common.functions.Create_ShapingInstruction Maven / Gradle / Ivy
package cdm.event.common.functions;
import cdm.base.math.QuantityChangeDirectionEnum;
import cdm.base.staticdata.identifier.Identifier;
import cdm.base.staticdata.party.AncillaryParty;
import cdm.base.staticdata.party.Counterparty;
import cdm.base.staticdata.party.Party;
import cdm.base.staticdata.party.PartyRole;
import cdm.event.common.ContractDetails;
import cdm.event.common.ContractFormationInstruction;
import cdm.event.common.ExecutionDetails;
import cdm.event.common.ExecutionInstruction;
import cdm.event.common.PrimitiveInstruction;
import cdm.event.common.PrimitiveInstruction.PrimitiveInstructionBuilder;
import cdm.event.common.QuantityChangeInstruction;
import cdm.event.common.State;
import cdm.event.common.Trade;
import cdm.event.common.TradeIdentifier;
import cdm.event.common.TradeState;
import cdm.event.position.PositionStatusEnum;
import cdm.legaldocumentation.common.LegalAgreement;
import cdm.observable.asset.PriceQuantity;
import cdm.product.template.NonTransferableProduct;
import cdm.product.template.TradeLot;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.expression.CardinalityOperator;
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.MapperListOfLists;
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.metafields.FieldWithMetaDate;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
import static com.rosetta.model.lib.expression.ExpressionOperators.*;
@ImplementedBy(Create_ShapingInstruction.Create_ShapingInstructionDefault.class)
public abstract class Create_ShapingInstruction implements RosettaFunction {
@Inject protected ModelObjectValidator objectValidator;
// RosettaFunction dependencies
//
@Inject protected Create_ContractFormationInstruction create_ContractFormationInstruction;
@Inject protected Create_PackageExecutionDetails create_PackageExecutionDetails;
@Inject protected Create_TerminationInstruction create_TerminationInstruction;
/**
* @param tradeState The original trade to be shaped.
* @param tradeLots The shaped quantities provided as full set of trade lots with price and quantity. Each trade lot also contains an identifier to associate to the corresponding shaped trade. Shaping must result in at least 2 shaped trades.
* @param shapeIdentifier The package ID of the shaped trades.
* @return instruction
*/
public PrimitiveInstruction evaluate(TradeState tradeState, List extends TradeLot> tradeLots, Identifier shapeIdentifier) {
PrimitiveInstruction.PrimitiveInstructionBuilder instructionBuilder = doEvaluate(tradeState, tradeLots, shapeIdentifier);
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 TradeLot> tradeLots, Identifier shapeIdentifier);
protected abstract MapperC extends Identifier> componentId(TradeState tradeState, List extends TradeLot> tradeLots, Identifier shapeIdentifier);
public static class Create_ShapingInstructionDefault extends Create_ShapingInstruction {
@Override
protected PrimitiveInstruction.PrimitiveInstructionBuilder doEvaluate(TradeState tradeState, List extends TradeLot> tradeLots, Identifier shapeIdentifier) {
if (tradeLots == null) {
tradeLots = Collections.emptyList();
}
PrimitiveInstruction.PrimitiveInstructionBuilder instruction = PrimitiveInstruction.builder();
return assignOutput(instruction, tradeState, tradeLots, shapeIdentifier);
}
protected PrimitiveInstruction.PrimitiveInstructionBuilder assignOutput(PrimitiveInstruction.PrimitiveInstructionBuilder instruction, TradeState tradeState, List extends TradeLot> tradeLots, Identifier shapeIdentifier) {
instruction
.getOrCreateSplit()
.setBreakdown(MapperC.of(MapperS.of(create_TerminationInstruction.evaluate(tradeState))).getMulti());
final MapperListOfLists thenResult = MapperC.of(tradeLots)
.mapItemToList(item -> item.mapC("getPriceQuantity", tradeLot -> tradeLot.getPriceQuantity()));
instruction
.getOrCreateSplit()
.addBreakdown(thenResult
.mapListToItem(item -> {
ContractFormationInstruction ifThenElseResult = null;
if (areEqual(MapperS.of(tradeState).map("getState", _tradeState -> _tradeState.getState()).map("getPositionState", state -> state.getPositionState()), MapperS.of(PositionStatusEnum.FORMED), CardinalityOperator.All).getOrDefault(false)) {
ifThenElseResult = create_ContractFormationInstruction.evaluate(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getContractDetails", trade -> trade.getContractDetails()).mapC("getDocumentation", contractDetails -> contractDetails.getDocumentation()).getMulti());
}
final FieldWithMetaDate fieldWithMetaDate = MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getTradeDate", trade -> trade.getTradeDate()).get();
return MapperS.of(PrimitiveInstruction.builder()
.setContractFormation(ifThenElseResult)
.setExecution(ExecutionInstruction.builder()
.setProduct(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getProduct", trade -> trade.getProduct()).get())
.setPriceQuantity(MapperS.of(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getTradeLot", trade -> trade.getTradeLot()).get()).mapC("getPriceQuantity", tradeLot -> tradeLot.getPriceQuantity()).getMulti())
.setCounterparty(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getCounterparty", trade -> trade.getCounterparty()).getMulti())
.setAncillaryParty(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getAncillaryParty", trade -> trade.getAncillaryParty()).getMulti())
.setParties(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getParty", trade -> trade.getParty()).getMulti())
.setPartyRoles(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getPartyRole", trade -> trade.getPartyRole()).getMulti())
.setExecutionDetails(create_PackageExecutionDetails.evaluate(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).map("getExecutionDetails", trade -> trade.getExecutionDetails()).get(), shapeIdentifier, componentId(tradeState, tradeLots, shapeIdentifier).getMulti()))
.setTradeDateValue((fieldWithMetaDate == null ? null : fieldWithMetaDate.getValue()))
.setTradeIdentifier(MapperS.of(tradeState).map("getTrade", _tradeState -> _tradeState.getTrade()).mapC("getTradeIdentifier", trade -> trade.getTradeIdentifier()).getMulti())
.build())
.setQuantityChange(QuantityChangeInstruction.builder()
.setChange(item.getMulti())
.setDirection(QuantityChangeDirectionEnum.REPLACE)
.setLotIdentifier(Collections.emptyList())
.build())
.build());
}).getMulti());
return Optional.ofNullable(instruction)
.map(o -> o.prune())
.orElse(null);
}
@Override
protected MapperC extends Identifier> componentId(TradeState tradeState, List extends TradeLot> tradeLots, Identifier shapeIdentifier) {
return MapperC.of(tradeLots)
.mapItem(item -> MapperS.of(item.mapC("getLotIdentifier", tradeLot -> tradeLot.getLotIdentifier()).get()));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy