public IAmbientStatistic GetOrAddStatistic(bool timeBased, string id, string description, bool resetIfAlreadyExists , long initialValue = 0 , AggregationTypes temporalAggregationTypes = AggregationTypes.Min | AggregationTypes.Average | AggregationTypes.Max , AggregationTypes spatialAggregationTypes = AggregationTypes.Min | AggregationTypes.Average | AggregationTypes.Max , AggregationTypes preferredTemporalAggregationType = AggregationTypes.Average , AggregationTypes preferredSpatialAggregationType = AggregationTypes.Average , MissingSampleHandling missingSampleHandling = MissingSampleHandling.LinearEstimation ) { IAmbientStatistic?statistic; if (resetIfAlreadyExists) { statistic = new Statistic(() => _statistics.TryRemove(id, out _), timeBased, id, description, initialValue, temporalAggregationTypes, spatialAggregationTypes, preferredTemporalAggregationType, preferredSpatialAggregationType, missingSampleHandling); _statistics.AddOrUpdate(id, statistic, (k, v) => statistic); } else { statistic = new Statistic(() => _statistics.TryRemove(id, out _), timeBased, id, description, initialValue, temporalAggregationTypes, spatialAggregationTypes, preferredTemporalAggregationType, preferredSpatialAggregationType, missingSampleHandling); statistic = _statistics.GetOrAdd(id, statistic) as IAmbientStatistic; // this *could* return something that is only an IAmbientStatisticReader! if (statistic == null) { throw new InvalidOperationException("The specified statistic identifier is already in use by a read-only statistic!"); } } return(statistic); }
private long _sampleValue; // interlocked public Statistic(Action removeRegistration, bool timeBased, string id, string description, long initialValue = 0 , AggregationTypes temporalAggregationTypes = AggregationTypes.Min | AggregationTypes.Average | AggregationTypes.Max , AggregationTypes spatialAggregationTypes = AggregationTypes.Min | AggregationTypes.Average | AggregationTypes.Max , AggregationTypes preferredTemporalAggregationType = AggregationTypes.Average , AggregationTypes preferredSpatialAggregationType = AggregationTypes.Average , MissingSampleHandling missingSampleHandling = MissingSampleHandling.LinearEstimation ) { _removeRegistration = removeRegistration; _timeBased = timeBased; _id = id; _description = description; _sampleValue = initialValue; TemporalAggregationTypes = temporalAggregationTypes; SpatialAggregationTypes = spatialAggregationTypes; PreferredTemporalAggregationType = preferredTemporalAggregationType; PreferredSpatialAggregationType = preferredSpatialAggregationType; MissingSampleHandling = missingSampleHandling; }