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

cdm.observable.event.functions.ResolveObservationAverage Maven / Gradle / Ivy

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

import cdm.base.math.UnitType;
import cdm.observable.asset.Price;
import cdm.observable.asset.Price.PriceBuilder;
import cdm.observable.asset.PriceExpressionEnum;
import cdm.observable.asset.PriceTypeEnum;
import cdm.observable.event.Observation;
import com.google.inject.ImplementedBy;
import com.rosetta.model.lib.expression.CardinalityOperator;
import com.rosetta.model.lib.expression.MapperMaths;
import com.rosetta.model.lib.functions.ConditionValidator;
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.math.BigDecimal;
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(ResolveObservationAverage.ResolveObservationAverageDefault.class)
public abstract class ResolveObservationAverage implements RosettaFunction {
	
	@Inject protected ConditionValidator conditionValidator;
	
	@Inject protected ModelObjectValidator objectValidator;

	/**
	* @param observations 
	* @return resetValue 
	*/
	public Price evaluate(List observations) {
		// pre-conditions
		conditionValidator.validate(() -> areEqual(MapperC.of(observations).map("getObservedValue", observation -> observation.getObservedValue()).map("getUnit", price -> price.getUnit()), firstObservedValue(observations).map("getUnit", price -> price.getUnit()), CardinalityOperator.All),
			"");
		
		Price.PriceBuilder resetValueBuilder = doEvaluate(observations);
		
		final Price resetValue;
		if (resetValueBuilder == null) {
			resetValue = null;
		} else {
			resetValue = resetValueBuilder.build();
			objectValidator.validate(Price.class, resetValue);
		}
		
		return resetValue;
	}

	protected abstract Price.PriceBuilder doEvaluate(List observations);

	protected abstract MapperS firstObservedValue(List observations);

	public static class ResolveObservationAverageDefault extends ResolveObservationAverage {
		@Override
		protected Price.PriceBuilder doEvaluate(List observations) {
			if (observations == null) {
				observations = Collections.emptyList();
			}
			Price.PriceBuilder resetValue = Price.builder();
			return assignOutput(resetValue, observations);
		}
		
		protected Price.PriceBuilder assignOutput(Price.PriceBuilder resetValue, List observations) {
			resetValue
				.setValue(MapperMaths.divide(MapperC.of(observations).map("getObservedValue", observation -> observation.getObservedValue()).map("getValue", price -> price.getValue())
					.sumBigDecimal(), MapperS.of(BigDecimal.valueOf(MapperC.of(observations).resultCount()))).get());
			
			resetValue
				.setUnit(firstObservedValue(observations).map("getUnit", price -> price.getUnit()).get());
			
			resetValue
				.setPerUnitOf(firstObservedValue(observations).map("getPerUnitOf", price -> price.getPerUnitOf()).get());
			
			resetValue
				.setPriceExpression(firstObservedValue(observations).map("getPriceExpression", price -> price.getPriceExpression()).get());
			
			resetValue
				.setPriceType(firstObservedValue(observations).map("getPriceType", price -> price.getPriceType()).get());
			
			return Optional.ofNullable(resetValue)
				.map(o -> o.prune())
				.orElse(null);
		}
		
		@Override
		protected MapperS firstObservedValue(List observations) {
			return MapperC.of(observations).map("getObservedValue", observation -> observation.getObservedValue())
				.first();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy