< Summary

Information
Class: MyNet.Observable.Collections.Grouping.GroupingEngine<T>
Assembly: MyNet.Observable
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Grouping/GroupingEngine.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 84
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
ComputeGroups(...)100%66100%
get_Grouping()100%11100%
Set(...)100%11100%
Clear()100%11100%
Dispose()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Observable/Collections/Grouping/GroupingEngine.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="GroupingEngine.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10using System.Reactive.Subjects;
 11
 12namespace MyNet.Observable.Collections.Grouping;
 13
 14/// <summary>
 15/// Manages the grouping configuration for a reactive collection pipeline.
 16/// Exposes an observable that emits the current grouping properties whenever they change,
 17/// allowing downstream consumers (e.g. <see cref="MyNet.Observable.Collections.ExtendedCollection{T}"/>)
 18/// to reactively recompute grouped representations.
 19/// </summary>
 20/// <typeparam name="T">The type of items to be grouped.</typeparam>
 21public sealed class GroupingEngine<T> : IDisposable
 22    where T : notnull
 23{
 19224    private readonly BehaviorSubject<IGroupingProperty<T>[]> _subject = new([]);
 25
 26    /// <summary>
 27    /// Computes a flat list of groups from a snapshot of items and a set of grouping properties.
 28    /// Groups are identified by a composite key built by concatenating the values of all grouping properties.
 29    /// </summary>
 30    /// <param name="items">The items to group.</param>
 31    /// <param name="groupingProperties">The active grouping properties.</param>
 32    /// <returns>A read-only list of <see cref="CollectionGroup{T}"/>. Returns an empty list if no grouping properties a
 33    public static IReadOnlyList<CollectionGroup<T>> ComputeGroups(IReadOnlyList<T> items, IGroupingProperty<T>[] groupin
 34    {
 10835        if (groupingProperties.Length == 0)
 9636            return [];
 37
 1238        var keySelectors = groupingProperties
 1239            .Select(g => g.ProvideExpression().Compile())
 1240            .ToArray();
 41
 1242        return
 1243        [
 1244            .. items
 1245                .GroupBy(buildKey)
 1246                .Select(g => new CollectionGroup<T>(g.Key, [.. g]))
 1247        ];
 48
 49        string buildKey(T item) => string.Join("|", keySelectors.Select(s => s(item)?.ToString() ?? "<null>"));
 50    }
 51
 52    /// <summary>
 53    /// Gets the currently active grouping properties.
 54    /// </summary>
 19255    public IGroupingProperty<T>[] Current { get; private set; } = [];
 56
 57    /// <summary>
 58    /// Gets an observable that emits the current grouping properties whenever they change.
 59    /// </summary>
 19260    public IObservable<IGroupingProperty<T>[]> Grouping => _subject;
 61
 62    /// <summary>
 63    /// Sets the active grouping properties and notifies subscribers.
 64    /// </summary>
 65    /// <param name="grouping">The new set of grouping properties to apply.</param>
 66    public void Set(IEnumerable<IGroupingProperty<T>> grouping)
 67    {
 1268        Current = [.. grouping];
 1269        _subject.OnNext(Current);
 1270    }
 71
 72    /// <summary>
 73    /// Clears all active grouping properties and notifies subscribers.
 74    /// </summary>
 75    public void Clear()
 76    {
 8177        Current = [];
 8178        _subject.OnNext(Current);
 8179    }
 80
 81    /// <inheritdoc/>
 16282    public void Dispose() => _subject.Dispose();
 83}
 84