Created
March 16, 2017 13:09
-
-
Save GeorgeTsiokos/a142cc7fec171726fa3cb7520278d6f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary>Filters the elements of an observable sequence based on a nullability.</summary> | |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> | |
/// <param name="source">An observable sequence whose elements to filter.</param> | |
/// <returns>An observable sequence that contains elements from the input sequence that are not <c>null</c>.</returns> | |
/// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> | |
[NotNull] | |
[ContractAnnotation("source:null => halt")] | |
public static IObservable<TSource> WhereNotNull<TSource>([NotNull] this IObservable<TSource> source) where TSource : class | |
{ | |
if (source == null) | |
throw new ArgumentNullException(nameof(source)); | |
// ReSharper disable once ExceptionNotDocumentedOptional | |
return source.Where(WhereNotNullPredicate); | |
} | |
/// <summary>Filters the elements of an observable sequence based on a nullability.</summary> | |
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam> | |
/// <param name="source">An observable sequence whose elements to filter.</param> | |
/// <returns>An observable sequence that contains elements from the input sequence that are not <c>null</c>.</returns> | |
/// <exception cref="T:System.ArgumentNullException"><paramref name="source" /> is null.</exception> | |
[NotNull] | |
[ContractAnnotation("source:null => halt")] | |
public static IObservable<TSource> WhereNotNull<TSource>([NotNull] this IObservable<TSource?> source) where TSource : struct | |
{ | |
if (source == null) | |
throw new ArgumentNullException(nameof(source)); | |
// ReSharper disable once ExceptionNotDocumentedOptional | |
return source.Where(WhereNotNullPredicate).Select(StructureValue); | |
} | |
static bool WhereNotNullPredicate<T>([CanBeNull] T item) | |
{ | |
return null != item; | |
} | |
static bool WhereNotNullPredicate<T>([CanBeNull] T? item) where T: struct | |
{ | |
return null != item; | |
} | |
static T StructureValue<T>([CanBeNull] T? item) where T : struct | |
{ | |
// ReSharper disable once PossibleInvalidOperationException | |
return item.Value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment