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

cdm.margin.schedule.functions.GetGrossInitialMarginFromStandardizedSchedule Maven / Gradle / Ivy

There is a newer version: 6.0.0-dev.89
Show newest version
package cdm.margin.schedule.functions;

import cdm.base.math.Quantity;
import cdm.base.math.Quantity.QuantityBuilder;
import cdm.margin.schedule.StandardizedSchedule;
import cdm.margin.schedule.StandardizedScheduleAssetClassEnum;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.expression.MapperMaths;
import com.rosetta.model.lib.functions.ModelObjectValidator;
import com.rosetta.model.lib.functions.RosettaFunction;
import com.rosetta.model.lib.mapper.MapperS;
import java.math.BigDecimal;
import java.util.Optional;
import javax.inject.Inject;


@ImplementedBy(GetGrossInitialMarginFromStandardizedSchedule.GetGrossInitialMarginFromStandardizedScheduleDefault.class)
public abstract class GetGrossInitialMarginFromStandardizedSchedule implements RosettaFunction {
	
	@Inject protected ModelObjectValidator objectValidator;
	
	// RosettaFunction dependencies
	//
	@Inject protected GetIMRequirement getIMRequirement;

	/**
	* @param standardizedSchedule 
	* @return grossInitialMargin 
	*/
	public Quantity evaluate(StandardizedSchedule standardizedSchedule) {
		Quantity.QuantityBuilder grossInitialMarginBuilder = doEvaluate(standardizedSchedule);
		
		final Quantity grossInitialMargin;
		if (grossInitialMarginBuilder == null) {
			grossInitialMargin = null;
		} else {
			grossInitialMargin = grossInitialMarginBuilder.build();
			objectValidator.validate(Quantity.class, grossInitialMargin);
		}
		
		return grossInitialMargin;
	}

	protected abstract Quantity.QuantityBuilder doEvaluate(StandardizedSchedule standardizedSchedule);

	protected abstract MapperS initialMarginRequirement(StandardizedSchedule standardizedSchedule);

	public static class GetGrossInitialMarginFromStandardizedScheduleDefault extends GetGrossInitialMarginFromStandardizedSchedule {
		@Override
		protected Quantity.QuantityBuilder doEvaluate(StandardizedSchedule standardizedSchedule) {
			Quantity.QuantityBuilder grossInitialMargin = Quantity.builder();
			return assignOutput(grossInitialMargin, standardizedSchedule);
		}
		
		protected Quantity.QuantityBuilder assignOutput(Quantity.QuantityBuilder grossInitialMargin, StandardizedSchedule standardizedSchedule) {
			grossInitialMargin
				.setValue(MapperMaths.multiply(MapperMaths.multiply(MapperS.of(standardizedSchedule).map("getNotional", _standardizedSchedule -> _standardizedSchedule.getNotional()), initialMarginRequirement(standardizedSchedule)), MapperS.of(new BigDecimal("0.01"))).get());
			
			grossInitialMargin
				.getOrCreateUnit()
				.setCurrencyValue(MapperS.of(standardizedSchedule).map("getNotionalCurrency", _standardizedSchedule -> _standardizedSchedule.getNotionalCurrency()).get());
			
			return Optional.ofNullable(grossInitialMargin)
				.map(o -> o.prune())
				.orElse(null);
		}
		
		@Override
		protected MapperS initialMarginRequirement(StandardizedSchedule standardizedSchedule) {
			return MapperS.of(getIMRequirement.evaluate(MapperS.of(standardizedSchedule).map("getAssetClass", _standardizedSchedule -> _standardizedSchedule.getAssetClass()).get(), MapperS.of(standardizedSchedule).map("getDurationInYears", _standardizedSchedule -> _standardizedSchedule.getDurationInYears()).get()));
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy