AverageInt64
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 AverageInt64 :
Producer<
double,
AverageInt64.
_>
{
[
System.
Runtime.
CompilerServices.
NullableContext(
0)]
internal sealed class _ :
Sink<
long,
double>
{
private long _sum;
private long _count;
[
System.
Runtime.
CompilerServices.
NullableContext(
1)]
public _(
IObserver<
double>
observer)
:
base(
observer)
{
_sum =
0;
_count =
0;
}
public override void OnNext(
long value)
{
checked {
try {
_sum +=
value;
_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((
double)
_sum / (
double)
_count);
ForwardOnCompleted();
}
}
}
private readonly IObservable<
long>
_source;
public AverageInt64(
IObservable<
long>
source)
{
_source =
source;
}
protected override _ CreateSink(
IObserver<
double>
observer)
{
return new _(
observer);
}
protected override void Run(
_ sink)
{
sink.
Run(
_source);
}
}
}