Skip to content

Commit

Permalink
Add IsEmpty / NotEmpty overloads to CountEx (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
modplug committed Apr 28, 2020
1 parent 225c64f commit 533d41a
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/DynamicData/Aggregation/CountEx.cs
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for full license information.

using System;
using System.Reactive.Linq;

namespace DynamicData.Aggregation
{
Expand Down Expand Up @@ -57,5 +58,67 @@ public static IObservable<int> Count<TObject>(this IObservable<IDistinctChangeSe
{
return source.ForAggregation().Count();
}

/// <summary>
/// Counts the total number of items in the underlying data source
/// and return true if the number of items == 0
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
public static IObservable<bool> IsEmpty<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source)
{
return source.ForAggregation()
.Count()
.StartWith(0)
.Select(count => count == 0);
}

/// <summary>
/// Counts the total number of items in the underlying data source
/// and returns true if the number of items is greater than 0
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
public static IObservable<bool> NotEmpty<TObject, TKey>(this IObservable<IChangeSet<TObject, TKey>> source)
{
return source.ForAggregation()
.Count()
.StartWith(0)
.Select(count => count > 0);
}

/// <summary>
/// Counts the total number of items in the underlying data source
/// and return true if the number of items == 0
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
public static IObservable<bool> IsEmpty<TObject>(this IObservable<IChangeSet<TObject>> source)
{
return source.ForAggregation()
.Count()
.StartWith(0)
.Select(count => count == 0);
}

/// <summary>
/// Counts the total number of items in the underlying data source
/// and returns true if the number of items is greater than 0
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <param name="source">The source.</param>
/// <returns></returns>
public static IObservable<bool> NotEmpty<TObject>(this IObservable<IChangeSet<TObject>> source)
{
return source.ForAggregation()
.Count()
.StartWith(0)
.Select(count => count > 0);
}
}
}

0 comments on commit 533d41a

Please sign in to comment.