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

cdm.event.common.functions.ResolvePerformanceObservationIdentifiers Maven / Gradle / Ivy

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

import cdm.base.datetime.AdjustableOrRelativeDate;
import cdm.base.datetime.BusinessCenterTime;
import cdm.base.datetime.functions.ResolveAdjustableDate;
import cdm.base.staticdata.asset.common.Asset;
import cdm.base.staticdata.asset.common.AssetIdentifier;
import cdm.base.staticdata.asset.common.util.AssetDeepPathUtil;
import cdm.observable.asset.FxSpotRateSource;
import cdm.observable.asset.InformationSource;
import cdm.observable.asset.Observable;
import cdm.observable.asset.PerformanceValuationDates;
import cdm.observable.asset.ValuationDates;
import cdm.observable.asset.metafields.ReferenceWithMetaObservable;
import cdm.observable.common.DeterminationMethodEnum;
import cdm.observable.common.TimeTypeEnum;
import cdm.observable.event.ObservationIdentifier;
import cdm.observable.event.ObservationIdentifier.ObservationIdentifierBuilder;
import cdm.product.common.schedule.ObservationTerms;
import cdm.product.template.PerformancePayout;
import cdm.product.template.Underlier;
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.MapperS;
import com.rosetta.model.lib.records.Date;
import java.util.Optional;
import javax.inject.Inject;

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

@ImplementedBy(ResolvePerformanceObservationIdentifiers.ResolvePerformanceObservationIdentifiersDefault.class)
public abstract class ResolvePerformanceObservationIdentifiers implements RosettaFunction {
	
	@Inject protected ModelObjectValidator objectValidator;
	
	// RosettaFunction dependencies
	//
	@Inject protected AdjustedValuationDates adjustedValuationDates;
	@Inject protected AssetDeepPathUtil assetDeepPathUtil;
	@Inject protected ResolveAdjustableDate resolveAdjustableDate;
	@Inject protected ResolvePerformanceValuationTime resolvePerformanceValuationTime;

	/**
	* @param payout 
	* @param adjustedDate 
	* @return identifiers 
	*/
	public ObservationIdentifier evaluate(PerformancePayout payout, Date adjustedDate) {
		ObservationIdentifier.ObservationIdentifierBuilder identifiersBuilder = doEvaluate(payout, adjustedDate);
		
		final ObservationIdentifier identifiers;
		if (identifiersBuilder == null) {
			identifiers = null;
		} else {
			identifiers = identifiersBuilder.build();
			objectValidator.validate(ObservationIdentifier.class, identifiers);
		}
		
		return identifiers;
	}

	protected abstract ObservationIdentifier.ObservationIdentifierBuilder doEvaluate(PerformancePayout payout, Date adjustedDate);

	protected abstract MapperS adjustedFinalValuationDate(PerformancePayout payout, Date adjustedDate);

	protected abstract MapperS valuationDates(PerformancePayout payout, Date adjustedDate);

	public static class ResolvePerformanceObservationIdentifiersDefault extends ResolvePerformanceObservationIdentifiers {
		@Override
		protected ObservationIdentifier.ObservationIdentifierBuilder doEvaluate(PerformancePayout payout, Date adjustedDate) {
			ObservationIdentifier.ObservationIdentifierBuilder identifiers = ObservationIdentifier.builder();
			return assignOutput(identifiers, payout, adjustedDate);
		}
		
		protected ObservationIdentifier.ObservationIdentifierBuilder assignOutput(ObservationIdentifier.ObservationIdentifierBuilder identifiers, PerformancePayout payout, Date adjustedDate) {
			final ReferenceWithMetaObservable referenceWithMetaObservable = MapperS.of(payout).map("getUnderlier", performancePayout -> performancePayout.getUnderlier()).map("getObservable", underlier -> underlier.getObservable()).get();
			identifiers
				.setObservable((referenceWithMetaObservable == null ? null : referenceWithMetaObservable.getValue()));
			
			final MapperC thenResult = MapperC.of(adjustedValuationDates.evaluate(MapperS.of(payout).map("getValuationDates", performancePayout -> performancePayout.getValuationDates()).get()))
				.filterItemNullSafe(item -> lessThanEquals(item, MapperS.of(adjustedDate), CardinalityOperator.All).get());
			identifiers
				.setObservationDate(thenResult
					.last().get());
			
			identifiers
				.setObservationTime(resolvePerformanceValuationTime.evaluate(valuationDates(payout, adjustedDate).map("getValuationTime", performanceValuationDates -> performanceValuationDates.getValuationTime()).get(), valuationDates(payout, adjustedDate).map("getValuationTimeType", performanceValuationDates -> performanceValuationDates.getValuationTimeType()).get(), MapperS.of(identifiers).map("getObservable", observationIdentifier -> observationIdentifier.getObservable()).map("getAsset", observable -> observable.getAsset()).mapC("chooseIdentifier", asset -> assetDeepPathUtil.chooseIdentifier(asset)).get(), valuationDates(payout, adjustedDate).map("getDeterminationMethod", performanceValuationDates -> performanceValuationDates.getDeterminationMethod()).get()));
			
			identifiers
				.setInformationSource(MapperS.of(payout).map("getObservationTerms", performancePayout -> performancePayout.getObservationTerms()).map("getInformationSource", observationTerms -> observationTerms.getInformationSource()).map("getPrimarySource", fxSpotRateSource -> fxSpotRateSource.getPrimarySource()).get());
			
			identifiers
				.getOrCreateDeterminationMethodology()
				.setDeterminationMethod(valuationDates(payout, adjustedDate).map("getDeterminationMethod", performanceValuationDates -> performanceValuationDates.getDeterminationMethod()).get());
			
			return Optional.ofNullable(identifiers)
				.map(o -> o.prune())
				.orElse(null);
		}
		
		@Override
		protected MapperS adjustedFinalValuationDate(PerformancePayout payout, Date adjustedDate) {
			return MapperS.of(resolveAdjustableDate.evaluate(MapperS.of(payout).map("getValuationDates", performancePayout -> performancePayout.getValuationDates()).map("getFinalValuationDate", _valuationDates -> _valuationDates.getFinalValuationDate()).map("getValuationDate", performanceValuationDates -> performanceValuationDates.getValuationDate()).get()));
		}
		
		@Override
		protected MapperS valuationDates(PerformancePayout payout, Date adjustedDate) {
			if (lessThan(MapperS.of(adjustedDate), adjustedFinalValuationDate(payout, adjustedDate), CardinalityOperator.All).getOrDefault(false)) {
				return MapperS.of(payout).map("getValuationDates", performancePayout -> performancePayout.getValuationDates()).map("getInterimValuationDate", _valuationDates -> _valuationDates.getInterimValuationDate());
			}
			return MapperS.of(payout).map("getValuationDates", performancePayout -> performancePayout.getValuationDates()).map("getFinalValuationDate", _valuationDates -> _valuationDates.getFinalValuationDate());
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy