AverageSingle
using System.
Runtime.
CompilerServices;
namespace System.
Reactive.
Linq.
ObservableImpl
{
[
System.
Runtime.
CompilerServices.
NullableContext(
1)]
[
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
0,
1
})]
internal sealed class AverageSingle :
Producer<
float,
AverageSingle.
_>
{
[
System.
Runtime.
CompilerServices.
NullableContext(
0)]
internal sealed class _ :
IdentitySink<
float>
{
private double _sum;
private long _count;
[
System.
Runtime.
CompilerServices.
NullableContext(
1)]
public _(
IObserver<
float>
observer)
:
base(
observer)
{
_sum =
0;
_count =
0;
}
public override void OnNext(
float value)
{
try {
_sum += (
double)
value;
checked {
_count++;
}
}
catch (
Exception error) {
ForwardOnError(
error);
}
}
public override void OnCompleted()
{
if (
_count <=
0)
try {
throw new InvalidOperationException(
Strings_Linq.
NO_ELEMENTS);
}
catch (
Exception error) {
ForwardOnError(
error);
}
else {
ForwardOnNext((
float)(
_sum / (
double)
_count));
ForwardOnCompleted();
}
}
}
private readonly IObservable<
float>
_source;
public AverageSingle(
IObservable<
float>
source)
{
_source =
source;
}
protected override _ CreateSink(
IObserver<
float>
observer)
{
return new _(
observer);
}
protected override void Run(
_ sink)
{
sink.
Run(
_source);
}
}
}