Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Notification.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System.Diagnostics;
4 using System.Globalization;
5 using System.Collections.Generic;
6 using System.Reactive.Concurrency;
7
8 #pragma warning disable 0659
9 #pragma warning disable 0661
10
11 namespace System.Reactive
12 {
13 /// <summary>
14 /// Indicates the type of a notification.
15 /// </summary>
16 public enum NotificationKind
17 {
18 /// <summary>
19 /// Represents an OnNext notification.
20 /// </summary>
21 OnNext,
22
23 /// <summary>
24 /// Represents an OnError notification.
25 /// </summary>
26 OnError,
27
28 /// <summary>
29 /// Represents an OnCompleted notification.
30 /// </summary>
31 OnCompleted
32 }
33
34 /// <summary>
35 /// Represents a notification to an observer.
36 /// </summary>
37 /// <typeparam name="T">The type of the elements received by the observer.</typeparam>
38 #if !NO_SERIALIZABLE
39 [Serializable]
40 #endif
41 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2218:OverrideGetHashCodeOnOverridingEquals", Justification = "Resembles a discriminated union with finite number of subclasses (external users shouldn't create their own subtypes), each of which does override GetHashCode itself.")]
42 public abstract class Notification<T> : IEquatable<Notification<T>>
43 {
44 /// <summary>
45 /// Default constructor used by derived types.
46 /// </summary>
47 protected internal Notification()
48 {
49 }
50
51 /// <summary>
52 /// Returns the value of an OnNext notification or throws an exception.
53 /// </summary>
54 public abstract T Value
55 {
56 get;
57 }
58
59 /// <summary>
60 /// Returns a value that indicates whether the notification has a value.
61 /// </summary>
62 public abstract bool HasValue
63 {
64 get;
65 }
66
67 /// <summary>
68 /// Returns the exception of an OnError notification or returns null.
69 /// </summary>
70 public abstract Exception Exception
71 {
72 get;
73 }
74
75 /// <summary>
76 /// Gets the kind of notification that is represented.
77 /// </summary>
78 public abstract NotificationKind Kind
79 {
80 get;
81 }
82
83 /// <summary>
84 /// Represents an OnNext notification to an observer.
85 /// </summary>
86 #if !NO_DEBUGGER_ATTRIBUTES
87 [DebuggerDisplay("OnNext({Value})")]
88 #endif
89 #if !NO_SERIALIZABLE
90 [Serializable]
91 #endif
92 internal sealed class OnNextNotification : Notification<T>
93 {
94 T value;
95
96 /// <summary>
97 /// Constructs a notification of a new value.
98 /// </summary>
99 public OnNextNotification(T value)
100 {
101 this.value = value;
102 }
103
104 /// <summary>
105 /// Returns the value of an OnNext notification.
106 /// </summary>
107 public override T Value { get { return value; } }
108
109 /// <summary>
110 /// Returns null.
111 /// </summary>
112 public override Exception Exception { get { return null; } }
113
114 /// <summary>
115 /// Returns true.
116 /// </summary>
117 public override bool HasValue { get { return true; } }
118
119 /// <summary>
120 /// Returns NotificationKind.OnNext.
121 /// </summary>
122 public override NotificationKind Kind { get { return NotificationKind.OnNext; } }
123
124 /// <summary>
125 /// Returns the hash code for this instance.
126 /// </summary>
127 public override int GetHashCode()
128 {
129 return EqualityComparer<T>.Default.GetHashCode(Value);
130 }
131
132 /// <summary>
133 /// Indicates whether this instance and a specified object are equal.
134 /// </summary>
135 public override bool Equals(Notification<T> other)
136 {
137 if (Object.ReferenceEquals(this, other))
138 return true;
139 if (Object.ReferenceEquals(other, null))
140 return false;
141 if (other.Kind != NotificationKind.OnNext)
142 return false;
143 return EqualityComparer<T>.Default.Equals(Value, other.Value);
144 }
145
146 /// <summary>
147 /// Returns a string representation of this instance.
148 /// </summary>
149 public override string ToString()
150 {
151 return String.Format(CultureInfo.CurrentCulture, "OnNext({0})", Value);
152 }
153
154 /// <summary>
155 /// Invokes the observer's method corresponding to the notification.
156 /// </summary>
157 /// <param name="observer">Observer to invoke the notification on.</param>
158 public override void Accept(IObserver<T> observer)
159 {
160 if (observer == null)
161 throw new ArgumentNullException("observer");
162
163 observer.OnNext(Value);
164 }
165
166 /// <summary>
167 /// Invokes the observer's method corresponding to the notification and returns the produced result.
168 /// </summary>
169 /// <param name="observer">Observer to invoke the notification on.</param>
170 /// <returns>Result produced by the observation.</returns>
171 public override TResult Accept<TResult>(IObserver<T, TResult> observer)
172 {
173 if (observer == null)
174 throw new ArgumentNullException("observer");
175
176 return observer.OnNext(Value);
177 }
178
179 /// <summary>
180 /// Invokes the delegate corresponding to the notification.
181 /// </summary>
182 /// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
183 /// <param name="onError">Delegate to invoke for an OnError notification.</param>
184 /// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
185 public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
186 {
187 if (onNext == null)
188 throw new ArgumentNullException("onNext");
189 if (onError == null)
190 throw new ArgumentNullException("onError");
191 if (onCompleted == null)
192 throw new ArgumentNullException("onCompleted");
193
194 onNext(Value);
195 }
196
197 /// <summary>
198 /// Invokes the delegate corresponding to the notification and returns the produced result.
199 /// </summary>
200 /// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
201 /// <param name="onError">Delegate to invoke for an OnError notification.</param>
202 /// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
203 /// <returns>Result produced by the observation.</returns>
204 public override TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
205 {
206 if (onNext == null)
207 throw new ArgumentNullException("onNext");
208 if (onError == null)
209 throw new ArgumentNullException("onError");
210 if (onCompleted == null)
211 throw new ArgumentNullException("onCompleted");
212
213 return onNext(Value);
214 }
215 }
216
217 /// <summary>
218 /// Represents an OnError notification to an observer.
219 /// </summary>
220 #if !NO_DEBUGGER_ATTRIBUTES
221 [DebuggerDisplay("OnError({Exception})")]
222 #endif
223 #if !NO_SERIALIZABLE
224 [Serializable]
225 #endif
226 internal sealed class OnErrorNotification : Notification<T>
227 {
228 Exception exception;
229
230 /// <summary>
231 /// Constructs a notification of an exception.
232 /// </summary>
233 public OnErrorNotification(Exception exception)
234 {
235 this.exception = exception;
236 }
237
238 /// <summary>
239 /// Throws the exception.
240 /// </summary>
241 public override T Value { get { exception.Throw(); return default(T); } }
242
243 /// <summary>
244 /// Returns the exception.
245 /// </summary>
246 public override Exception Exception { get { return exception; } }
247
248 /// <summary>
249 /// Returns false.
250 /// </summary>
251 public override bool HasValue { get { return false; } }
252
253 /// <summary>
254 /// Returns NotificationKind.OnError.
255 /// </summary>
256 public override NotificationKind Kind { get { return NotificationKind.OnError; } }
257
258 /// <summary>
259 /// Returns the hash code for this instance.
260 /// </summary>
261 public override int GetHashCode()
262 {
263 return Exception.GetHashCode();
264 }
265
266 /// <summary>
267 /// Indicates whether this instance and other are equal.
268 /// </summary>
269 public override bool Equals(Notification<T> other)
270 {
271 if (Object.ReferenceEquals(this, other))
272 return true;
273 if (Object.ReferenceEquals(other, null))
274 return false;
275 if (other.Kind != NotificationKind.OnError)
276 return false;
277 return Object.Equals(Exception, other.Exception);
278 }
279
280 /// <summary>
281 /// Returns a string representation of this instance.
282 /// </summary>
283 public override string ToString()
284 {
285 return String.Format(CultureInfo.CurrentCulture, "OnError({0})", Exception.GetType().FullName);
286 }
287
288 /// <summary>
289 /// Invokes the observer's method corresponding to the notification.
290 /// </summary>
291 /// <param name="observer">Observer to invoke the notification on.</param>
292 public override void Accept(IObserver<T> observer)
293 {
294 if (observer == null)
295 throw new ArgumentNullException("observer");
296
297 observer.OnError(Exception);
298 }
299
300 /// <summary>
301 /// Invokes the observer's method corresponding to the notification and returns the produced result.
302 /// </summary>
303 /// <param name="observer">Observer to invoke the notification on.</param>
304 /// <returns>Result produced by the observation.</returns>
305 public override TResult Accept<TResult>(IObserver<T, TResult> observer)
306 {
307 if (observer == null)
308 throw new ArgumentNullException("observer");
309
310 return observer.OnError(Exception);
311 }
312
313 /// <summary>
314 /// Invokes the delegate corresponding to the notification.
315 /// </summary>
316 /// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
317 /// <param name="onError">Delegate to invoke for an OnError notification.</param>
318 /// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
319 public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
320 {
321 if (onNext == null)
322 throw new ArgumentNullException("onNext");
323 if (onError == null)
324 throw new ArgumentNullException("onError");
325 if (onCompleted == null)
326 throw new ArgumentNullException("onCompleted");
327
328 onError(Exception);
329 }
330
331 /// <summary>
332 /// Invokes the delegate corresponding to the notification and returns the produced result.
333 /// </summary>
334 /// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
335 /// <param name="onError">Delegate to invoke for an OnError notification.</param>
336 /// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
337 /// <returns>Result produced by the observation.</returns>
338 public override TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
339 {
340 if (onNext == null)
341 throw new ArgumentNullException("onNext");
342 if (onError == null)
343 throw new ArgumentNullException("onError");
344 if (onCompleted == null)
345 throw new ArgumentNullException("onCompleted");
346
347 return onError(Exception);
348 }
349 }
350
351 /// <summary>
352 /// Represents an OnCompleted notification to an observer.
353 /// </summary>
354 #if !NO_DEBUGGER_ATTRIBUTES
355 [DebuggerDisplay("OnCompleted()")]
356 #endif
357 #if !NO_SERIALIZABLE
358 [Serializable]
359 #endif
360 internal sealed class OnCompletedNotification : Notification<T>
361 {
362 /// <summary>
363 /// Constructs a notification of the end of a sequence.
364 /// </summary>
365 public OnCompletedNotification()
366 {
367 }
368
369 /// <summary>
370 /// Throws an InvalidOperationException.
371 /// </summary>
372 public override T Value { get { throw new InvalidOperationException(Strings_Core.COMPLETED_NO_VALUE); } }
373
374 /// <summary>
375 /// Returns null.
376 /// </summary>
377 public override Exception Exception { get { return null; } }
378
379 /// <summary>
380 /// Returns false.
381 /// </summary>
382 public override bool HasValue { get { return false; } }
383
384 /// <summary>
385 /// Returns NotificationKind.OnCompleted.
386 /// </summary>
387 public override NotificationKind Kind { get { return NotificationKind.OnCompleted; } }
388
389 /// <summary>
390 /// Returns the hash code for this instance.
391 /// </summary>
392 public override int GetHashCode()
393 {
394 return typeof(T).GetHashCode() ^ 8510;
395 }
396
397 /// <summary>
398 /// Indicates whether this instance and other are equal.
399 /// </summary>
400 public override bool Equals(Notification<T> other)
401 {
402 if (Object.ReferenceEquals(this, other))
403 return true;
404 if (Object.ReferenceEquals(other, null))
405 return false;
406 return other.Kind == NotificationKind.OnCompleted;
407 }
408
409 /// <summary>
410 /// Returns a string representation of this instance.
411 /// </summary>
412 public override string ToString()
413 {
414 return "OnCompleted()";
415 }
416
417 /// <summary>
418 /// Invokes the observer's method corresponding to the notification.
419 /// </summary>
420 /// <param name="observer">Observer to invoke the notification on.</param>
421 public override void Accept(IObserver<T> observer)
422 {
423 if (observer == null)
424 throw new ArgumentNullException("observer");
425
426 observer.OnCompleted();
427 }
428
429 /// <summary>
430 /// Invokes the observer's method corresponding to the notification and returns the produced result.
431 /// </summary>
432 /// <param name="observer">Observer to invoke the notification on.</param>
433 /// <returns>Result produced by the observation.</returns>
434 public override TResult Accept<TResult>(IObserver<T, TResult> observer)
435 {
436 if (observer == null)
437 throw new ArgumentNullException("observer");
438
439 return observer.OnCompleted();
440 }
441
442 /// <summary>
443 /// Invokes the delegate corresponding to the notification.
444 /// </summary>
445 /// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
446 /// <param name="onError">Delegate to invoke for an OnError notification.</param>
447 /// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
448 public override void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted)
449 {
450 if (onNext == null)
451 throw new ArgumentNullException("onNext");
452 if (onError == null)
453 throw new ArgumentNullException("onError");
454 if (onCompleted == null)
455 throw new ArgumentNullException("onCompleted");
456
457 onCompleted();
458 }
459
460 /// <summary>
461 /// Invokes the delegate corresponding to the notification and returns the produced result.
462 /// </summary>
463 /// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
464 /// <param name="onError">Delegate to invoke for an OnError notification.</param>
465 /// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
466 /// <returns>Result produced by the observation.</returns>
467 public override TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted)
468 {
469 if (onNext == null)
470 throw new ArgumentNullException("onNext");
471 if (onError == null)
472 throw new ArgumentNullException("onError");
473 if (onCompleted == null)
474 throw new ArgumentNullException("onCompleted");
475
476 return onCompleted();
477 }
478 }
479
480 /// <summary>
481 /// Determines whether the current Notification&lt;T&gt; object has the same observer message payload as a specified Notification&lt;T&gt; value.
482 /// </summary>
483 /// <param name="other">An object to compare to the current Notification&lt;T&gt; object.</param>
484 /// <returns>true if both Notification&lt;T&gt; objects have the same observer message payload; otherwise, false.</returns>
485 /// <remarks>
486 /// Equality of Notification&lt;T&gt; objects is based on the equality of the observer message payload they represent, including the notification Kind and the Value or Exception (if any).
487 /// This means two Notification&lt;T&gt; objects can be equal even though they don't represent the same observer method call, but have the same Kind and have equal parameters passed to the observer method.
488 /// In case one wants to determine whether two Notification&lt;T&gt; objects represent the same observer method call, use Object.ReferenceEquals identity equality instead.
489 /// </remarks>
490 public abstract bool Equals(Notification<T> other);
491
492 /// <summary>
493 /// Determines whether the two specified Notification&lt;T&gt; objects have the same observer message payload.
494 /// </summary>
495 /// <param name="left">The first Notification&lt;T&gt; to compare, or null.</param>
496 /// <param name="right">The second Notification&lt;T&gt; to compare, or null.</param>
497 /// <returns>true if the first Notification&lt;T&gt; value has the same observer message payload as the second Notification&lt;T&gt; value; otherwise, false.</returns>
498 /// <remarks>
499 /// Equality of Notification&lt;T&gt; objects is based on the equality of the observer message payload they represent, including the notification Kind and the Value or Exception (if any).
500 /// This means two Notification&lt;T&gt; objects can be equal even though they don't represent the same observer method call, but have the same Kind and have equal parameters passed to the observer method.
501 /// In case one wants to determine whether two Notification&lt;T&gt; objects represent the same observer method call, use Object.ReferenceEquals identity equality instead.
502 /// </remarks>
503 public static bool operator ==(Notification<T> left, Notification<T> right)
504 {
505 if (object.ReferenceEquals(left, right))
506 return true;
507
508 if ((object)left == null || (object)right == null)
509 return false;
510
511 return left.Equals(right);
512 }
513
514 /// <summary>
515 /// Determines whether the two specified Notification&lt;T&gt; objects have a different observer message payload.
516 /// </summary>
517 /// <param name="left">The first Notification&lt;T&gt; to compare, or null.</param>
518 /// <param name="right">The second Notification&lt;T&gt; to compare, or null.</param>
519 /// <returns>true if the first Notification&lt;T&gt; value has a different observer message payload as the second Notification&lt;T&gt; value; otherwise, false.</returns>
520 /// <remarks>
521 /// Equality of Notification&lt;T&gt; objects is based on the equality of the observer message payload they represent, including the notification Kind and the Value or Exception (if any).
522 /// This means two Notification&lt;T&gt; objects can be equal even though they don't represent the same observer method call, but have the same Kind and have equal parameters passed to the observer method.
523 /// In case one wants to determine whether two Notification&lt;T&gt; objects represent a different observer method call, use Object.ReferenceEquals identity equality instead.
524 /// </remarks>
525 public static bool operator !=(Notification<T> left, Notification<T> right)
526 {
527 return !(left == right);
528 }
529
530 /// <summary>
531 /// Determines whether the specified System.Object is equal to the current Notification&lt;T&gt;.
532 /// </summary>
533 /// <param name="obj">The System.Object to compare with the current Notification&lt;T&gt;.</param>
534 /// <returns>true if the specified System.Object is equal to the current Notification&lt;T&gt;; otherwise, false.</returns>
535 /// <remarks>
536 /// Equality of Notification&lt;T&gt; objects is based on the equality of the observer message payload they represent, including the notification Kind and the Value or Exception (if any).
537 /// This means two Notification&lt;T&gt; objects can be equal even though they don't represent the same observer method call, but have the same Kind and have equal parameters passed to the observer method.
538 /// In case one wants to determine whether two Notification&lt;T&gt; objects represent the same observer method call, use Object.ReferenceEquals identity equality instead.
539 /// </remarks>
540 public override bool Equals(object obj)
541 {
542 return Equals(obj as Notification<T>);
543 }
544
545 /// <summary>
546 /// Invokes the observer's method corresponding to the notification.
547 /// </summary>
548 /// <param name="observer">Observer to invoke the notification on.</param>
549 public abstract void Accept(IObserver<T> observer);
550
551 /// <summary>
552 /// Invokes the observer's method corresponding to the notification and returns the produced result.
553 /// </summary>
554 /// <typeparam name="TResult">The type of the result returned from the observer's notification handlers.</typeparam>
555 /// <param name="observer">Observer to invoke the notification on.</param>
556 /// <returns>Result produced by the observation.</returns>
557 public abstract TResult Accept<TResult>(IObserver<T, TResult> observer);
558
559 /// <summary>
560 /// Invokes the delegate corresponding to the notification.
561 /// </summary>
562 /// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
563 /// <param name="onError">Delegate to invoke for an OnError notification.</param>
564 /// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
565 public abstract void Accept(Action<T> onNext, Action<Exception> onError, Action onCompleted);
566
567 /// <summary>
568 /// Invokes the delegate corresponding to the notification and returns the produced result.
569 /// </summary>
570 /// <typeparam name="TResult">The type of the result returned from the notification handler delegates.</typeparam>
571 /// <param name="onNext">Delegate to invoke for an OnNext notification.</param>
572 /// <param name="onError">Delegate to invoke for an OnError notification.</param>
573 /// <param name="onCompleted">Delegate to invoke for an OnCompleted notification.</param>
574 /// <returns>Result produced by the observation.</returns>
575 public abstract TResult Accept<TResult>(Func<T, TResult> onNext, Func<Exception, TResult> onError, Func<TResult> onCompleted);
576
577 /// <summary>
578 /// Returns an observable sequence with a single notification, using the immediate scheduler.
579 /// </summary>
580 /// <returns>The observable sequence that surfaces the behavior of the notification upon subscription.</returns>
581 public IObservable<T> ToObservable()
582 {
583 return this.ToObservable(ImmediateScheduler.Instance);
584 }
585
586 /// <summary>
587 /// Returns an observable sequence with a single notification.
588 /// </summary>
589 /// <param name="scheduler">Scheduler to send out the notification calls on.</param>
590 /// <returns>The observable sequence that surfaces the behavior of the notification upon subscription.</returns>
591 public IObservable<T> ToObservable(IScheduler scheduler)
592 {
593 if (scheduler == null)
594 throw new ArgumentNullException("scheduler");
595
596 return new AnonymousObservable<T>(observer => scheduler.Schedule(() =>
597 {
598 this.Accept(observer);
599 if (this.Kind == NotificationKind.OnNext)
600 observer.OnCompleted();
601 }));
602 }
603 }
604
605 /// <summary>
606 /// Provides a set of static methods for constructing notifications.
607 /// </summary>
608 public static class Notification
609 {
610 /// <summary>
611 /// Creates an object that represents an OnNext notification to an observer.
612 /// </summary>
613 /// <typeparam name="T">The type of the elements received by the observer. Upon dematerialization of the notifications into an observable sequence, this type is used as the element type for the sequence.</typeparam>
614 /// <param name="value">The value contained in the notification.</param>
615 /// <returns>The OnNext notification containing the value.</returns>
616 public static Notification<T> CreateOnNext<T>(T value)
617 {
618 return new Notification<T>.OnNextNotification(value);
619 }
620
621 /// <summary>
622 /// Creates an object that represents an OnError notification to an observer.
623 /// </summary>
624 /// <typeparam name="T">The type of the elements received by the observer. Upon dematerialization of the notifications into an observable sequence, this type is used as the element type for the sequence.</typeparam>
625 /// <param name="error">The exception contained in the notification.</param>
626 /// <returns>The OnError notification containing the exception.</returns>
627 /// <exception cref="ArgumentNullException"><paramref name="error"/> is null.</exception>
628 public static Notification<T> CreateOnError<T>(Exception error)
629 {
630 if (error == null)
631 throw new ArgumentNullException("error");
632
633 return new Notification<T>.OnErrorNotification(error);
634 }
635
636 /// <summary>
637 /// Creates an object that represents an OnCompleted notification to an observer.
638 /// </summary>
639 /// <typeparam name="T">The type of the elements received by the observer. Upon dematerialization of the notifications into an observable sequence, this type is used as the element type for the sequence.</typeparam>
640 /// <returns>The OnCompleted notification.</returns>
641 public static Notification<T> CreateOnCompleted<T>()
642 {
643 return new Notification<T>.OnCompletedNotification();
644 }
645 }
646 }
647
648 #pragma warning restore 0659
649 #pragma warning restore 0661