| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="PredicateItemsProvider.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Diagnostics.CodeAnalysis; |
| | | 10 | | using System.Runtime.CompilerServices; |
| | | 11 | | using System.Threading; |
| | | 12 | | |
| | | 13 | | namespace MyNet.Primitives.Providers; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Defines a class that implements the <see cref="IItemsProvider{T}"/> interface and provides a filtered collection of |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="provider">The underlying items provider.</param> |
| | | 19 | | /// <param name="predicate">The predicate function used to filter items.</param> |
| | | 20 | | /// <typeparam name="T">The type of items provided by the implementing class.</typeparam> |
| | | 21 | | public class PredicateItemsProvider<T>(IItemsProvider<T> provider, Func<T, bool> predicate) : IItemsProvider<T> |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="PredicateItemsProvider{T}"/> class with the specified items and pre |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="items">The collection of items to be filtered.</param> |
| | | 27 | | /// <param name="predicate">The predicate function used to filter items.</param> |
| | | 28 | | public PredicateItemsProvider(IEnumerable<T> items, Func<T, bool> predicate) |
| | 6 | 29 | | : this(new ItemsProvider<T>(items), predicate) { } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc/> |
| | | 32 | | [SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "This |
| | | 33 | | public async IAsyncEnumerable<T> GetItemsAsync([EnumeratorCancellation] CancellationToken cancellationToken = defaul |
| | | 34 | | { |
| | 78 | 35 | | await foreach (var item in provider.GetItemsAsync(cancellationToken)) |
| | | 36 | | { |
| | 27 | 37 | | if (predicate(item)) |
| | 12 | 38 | | yield return item; |
| | | 39 | | } |
| | 6 | 40 | | } |
| | | 41 | | } |
| | | 42 | | |