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

cdm.observable.asset.calculatedrate.functions.GenerateObservationDatesAndWeights Maven / Gradle / Ivy

There is a newer version: 6.0.0-dev.92
Show newest version
package cdm.observable.asset.calculatedrate.functions;

import cdm.base.datetime.BusinessCenterEnum;
import cdm.base.datetime.BusinessCenters;
import cdm.base.datetime.functions.GetAllBusinessCenters;
import cdm.observable.asset.calculatedrate.CalculatedRateObservationDatesAndWeights;
import cdm.observable.asset.calculatedrate.CalculatedRateObservationDatesAndWeights.CalculatedRateObservationDatesAndWeightsBuilder;
import cdm.observable.asset.calculatedrate.FloatingRateCalculationParameters;
import cdm.observable.asset.calculatedrate.ObservationPeriodDatesEnum;
import cdm.observable.asset.calculatedrate.ObservationShiftCalculation;
import cdm.observable.asset.calculatedrate.OffsetCalculation;
import cdm.product.common.schedule.CalculationPeriodBase;
import cdm.product.common.schedule.ResetDates;
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.Mapper;
import com.rosetta.model.lib.mapper.MapperC;
import com.rosetta.model.lib.mapper.MapperS;
import com.rosetta.model.lib.mapper.MapperUtils;
import com.rosetta.model.lib.records.Date;
import java.math.BigDecimal;
import java.util.Optional;
import javax.inject.Inject;

import static com.rosetta.model.lib.expression.ExpressionOperators.*;

@ImplementedBy(GenerateObservationDatesAndWeights.GenerateObservationDatesAndWeightsDefault.class)
public abstract class GenerateObservationDatesAndWeights implements RosettaFunction {
	
	@Inject protected ModelObjectValidator objectValidator;
	
	// RosettaFunction dependencies
	//
	@Inject protected ComputeCalculationPeriod computeCalculationPeriod;
	@Inject protected DetermineObservationPeriod determineObservationPeriod;
	@Inject protected GenerateObservationDates generateObservationDates;
	@Inject protected GenerateWeightings generateWeightings;
	@Inject protected GetAllBusinessCenters getAllBusinessCenters;

	/**
	* @param calculationParams Floating rate definition for the calculated rate.
	* @param resetDates Reset structure (needed only for fallback rates, otherwise will be empty.
	* @param calculationPeriod Calculation period for which we want to determine the rate.
	* @param priorCalculationPeriod The prior calculation period (needed only for set in advance observation shift rate.
	* @return results observation dates and corresponding weight.
	*/
	public CalculatedRateObservationDatesAndWeights evaluate(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
		CalculatedRateObservationDatesAndWeights.CalculatedRateObservationDatesAndWeightsBuilder resultsBuilder = doEvaluate(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod);
		
		final CalculatedRateObservationDatesAndWeights results;
		if (resultsBuilder == null) {
			results = null;
		} else {
			results = resultsBuilder.build();
			objectValidator.validate(CalculatedRateObservationDatesAndWeights.class, results);
		}
		
		return results;
	}

	protected abstract CalculatedRateObservationDatesAndWeights.CalculatedRateObservationDatesAndWeightsBuilder doEvaluate(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper obsShift(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper lockout(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper specifiedLockout(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper lockoutDays(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper businessDays(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper calculateRelative(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper adjustedCalculationPeriod(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper observationPeriod(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	protected abstract Mapper observationDates(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod);

	public static class GenerateObservationDatesAndWeightsDefault extends GenerateObservationDatesAndWeights {
		@Override
		protected CalculatedRateObservationDatesAndWeights.CalculatedRateObservationDatesAndWeightsBuilder doEvaluate(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			CalculatedRateObservationDatesAndWeights.CalculatedRateObservationDatesAndWeightsBuilder results = CalculatedRateObservationDatesAndWeights.builder();
			return assignOutput(results, calculationParams, resetDates, calculationPeriod, priorCalculationPeriod);
		}
		
		protected CalculatedRateObservationDatesAndWeights.CalculatedRateObservationDatesAndWeightsBuilder assignOutput(CalculatedRateObservationDatesAndWeights.CalculatedRateObservationDatesAndWeightsBuilder results, FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			results
				.addObservationDates(MapperC.of(observationDates(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).getMulti()).getMulti());
			
			results
				.addWeights(MapperC.of(generateWeightings.evaluate(MapperS.of(calculationParams).get(), MapperS.of(results).mapC("getObservationDates", calculatedRateObservationDatesAndWeights -> calculatedRateObservationDatesAndWeights.getObservationDates()).getMulti(), MapperS.of(observationPeriod(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).get(), MapperS.of(adjustedCalculationPeriod(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).get(), MapperS.of(lockoutDays(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).get())).getMulti());
			
			return Optional.ofNullable(results)
				.map(o -> o.prune())
				.orElse(null);
		}
		
		@Override
		protected Mapper obsShift(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperS.of(calculationParams).map("getObservationShiftCalculation", floatingRateCalculationParameters -> floatingRateCalculationParameters.getObservationShiftCalculation());
		}
		
		@Override
		protected Mapper lockout(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperS.of(calculationParams).map("getLockoutCalculation", floatingRateCalculationParameters -> floatingRateCalculationParameters.getLockoutCalculation());
		}
		
		@Override
		protected Mapper specifiedLockout(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperUtils.runSingle(() -> {
				if (exists(MapperS.of(lockout(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).map("getOffsetDays", offsetCalculation -> offsetCalculation.getOffsetDays())).getOrDefault(false)) {
					return MapperS.of(lockout(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).map("getOffsetDays", offsetCalculation -> offsetCalculation.getOffsetDays());
				}
				else {
					return MapperS.of(Integer.valueOf(5));
				}
			});
		}
		
		@Override
		protected Mapper lockoutDays(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperUtils.runSingle(() -> {
				if (exists(MapperS.of(lockout(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get())).getOrDefault(false)) {
					return MapperS.of(specifiedLockout(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get());
				}
				else {
					return MapperS.of(Integer.valueOf(0));
				}
			});
		}
		
		@Override
		protected Mapper businessDays(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperC.of(getAllBusinessCenters.evaluate(MapperS.of(calculationParams).map("getApplicableBusinessDays", floatingRateCalculationParameters -> floatingRateCalculationParameters.getApplicableBusinessDays()).get()));
		}
		
		@Override
		protected Mapper calculateRelative(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperUtils.runSingle(() -> {
				if (exists(MapperS.of(obsShift(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).map("getCalculationBase", observationShiftCalculation -> observationShiftCalculation.getCalculationBase())).getOrDefault(false)) {
					return MapperS.of(obsShift(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).map("getCalculationBase", observationShiftCalculation -> observationShiftCalculation.getCalculationBase());
				}
				else {
					return MapperS.of(ObservationPeriodDatesEnum.STANDARD);
				}
			});
		}
		
		@Override
		protected Mapper adjustedCalculationPeriod(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperS.of(computeCalculationPeriod.evaluate(MapperS.of(calculationPeriod).get(), MapperS.of(priorCalculationPeriod).get(), MapperS.of(calculateRelative(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).get(), MapperS.of(resetDates).get()));
		}
		
		@Override
		protected Mapper observationPeriod(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperS.of(determineObservationPeriod.evaluate(MapperS.of(adjustedCalculationPeriod(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).get(), MapperS.of(calculationParams).get()));
		}
		
		@Override
		protected Mapper observationDates(FloatingRateCalculationParameters calculationParams, ResetDates resetDates, CalculationPeriodBase calculationPeriod, CalculationPeriodBase priorCalculationPeriod) {
			return MapperC.of(generateObservationDates.evaluate(MapperS.of(observationPeriod(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).get(), MapperC.of(businessDays(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).getMulti()).getMulti(), MapperS.of(lockoutDays(calculationParams, resetDates, calculationPeriod, priorCalculationPeriod).get()).get()));
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy