< Summary

Information
Class: SwiftCollections.Observable.SwiftObservableList<T>
Assembly: SwiftCollections
File(s): /home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Observable/SwiftObservableList.cs
Line coverage
100%
Covered lines: 87
Uncovered lines: 0
Coverable lines: 87
Total lines: 231
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
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%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
get_Item(...)100%11100%
set_Item(...)100%22100%
Add(...)100%11100%
AddRange(...)100%22100%
AddRange(...)100%11100%
AddRange(...)100%22100%
Remove(...)100%22100%
RemoveAt(...)100%11100%
RemoveAll(...)100%22100%
Insert(...)100%11100%
Clear()100%22100%
OnPropertyChanged(...)100%22100%
OnCollectionChanged(...)100%22100%
OnCollectionChanged(...)100%11100%
OnCollectionChanged(...)100%11100%

File(s)

/home/runner/work/SwiftCollections/SwiftCollections/src/SwiftCollections/Observable/SwiftObservableList.cs

#LineLine coverage
 1using MemoryPack;
 2using System;
 3using System.Collections.Generic;
 4using System.Collections.Specialized;
 5using System.ComponentModel;
 6using System.Text.Json.Serialization;
 7
 8namespace SwiftCollections.Observable;
 9
 10/// <summary>
 11/// Represents an observable extension of the high-performance <see cref="SwiftList{T}"/>.
 12/// Notifies listeners of changes to its items and structure for reactive programming scenarios.
 13/// </summary>
 14/// <typeparam name="T">The type of elements in the list.</typeparam>
 15[Serializable]
 16[JsonConverter(typeof(SwiftStateJsonConverterFactory))]
 17[MemoryPackable]
 18public partial class SwiftObservableList<T> : SwiftList<T>, INotifyPropertyChanged, INotifyCollectionChanged
 19{
 20    #region Events
 21
 22    /// <summary>
 23    /// Raised when a property on the list changes.
 24    /// </summary>
 25    public event PropertyChangedEventHandler PropertyChanged;
 26
 27    /// <summary>
 28    /// Raised when the list's collection is modified.
 29    /// </summary>
 30    public event NotifyCollectionChangedEventHandler CollectionChanged;
 31
 32    #endregion
 33
 34    #region Constructors
 35
 36    /// <summary>
 37    /// Initializes a new instance of the <see cref="SwiftObservableList{T}"/> class.
 38    /// </summary>
 9639    public SwiftObservableList() : base() { }
 40
 41    /// <summary>
 42    /// Initializes a new instance of the <see cref="SwiftObservableList{T}"/> class with the specified initial capacity
 43    /// </summary>
 344    public SwiftObservableList(int capacity) : base(capacity) { }
 45
 46    /// <summary>
 47    /// Initializes a new instance of the <see cref="SwiftObservableList{T}"/> class that contains elements copied from 
 48    /// </summary>
 1849    public SwiftObservableList(IEnumerable<T> collection) : base(collection) { }
 50
 51    ///  <summary>
 52    ///  Initializes a new instance of the <see cref="SwiftObservableList{T}"/> class with the specified <see cref="Swif
 53    ///  </summary>
 54    ///  <param name="state">The state containing the internal array, count, offset, and version for initialization.</pa
 55    [MemoryPackConstructor]
 1856    public SwiftObservableList(SwiftArrayState<T> state) : base(state) { }
 57
 58    #endregion
 59
 60    #region Properties
 61
 62    /// <summary>
 63    /// Sets or gets the element at the specified index.
 64    /// Raises collection change notifications on modification.
 65    /// </summary>
 66    [JsonIgnore]
 67    [MemoryPackIgnore]
 68    public new T this[int index]
 69    {
 50670        get => base[index];
 71        set
 150672        {
 150673            T oldValue = base[index];
 150474            if (!Equals(oldValue, value))
 150175            {
 150176                base[index] = value;
 150177                OnCollectionChanged(NotifyCollectionChangedAction.Replace, oldValue, value, index);
 150178                OnPropertyChanged("InnerArray[]");
 150179            }
 150480        }
 81    }
 82
 83    #endregion
 84
 85    #region Methods
 86
 87    /// <summary>
 88    /// Adds an element to the end of the list.
 89    /// Raises collection and property change notifications.
 90    /// </summary>
 91    public override void Add(T item)
 1105192    {
 1105193        base.Add(item);
 1105194        OnCollectionChanged(NotifyCollectionChangedAction.Add, item, _count - 1);
 1105195        OnPropertyChanged(nameof(Count));
 1105196    }
 97
 98    public override void AddRange(IEnumerable<T> items)
 199    {
 1100        SwiftThrowHelper.ThrowIfNull(items, nameof(items));
 101
 9102        foreach (T item in items)
 3103            Add(item);
 1104    }
 105
 106    /// <summary>
 107    /// Adds the elements of the specified array to the end of the list.
 108    /// Raises collection and property change notifications for each added item.
 109    /// </summary>
 110    public override void AddRange(T[] items)
 2111    {
 2112        SwiftThrowHelper.ThrowIfNull(items, nameof(items));
 1113        AddRange(items.AsSpan());
 1114    }
 115
 116    /// <summary>
 117    /// Adds the elements of the specified span to the end of the list.
 118    /// Raises collection and property change notifications for each added item.
 119    /// </summary>
 120    public override void AddRange(ReadOnlySpan<T> items)
 2121    {
 16122        for (int i = 0; i < items.Length; i++)
 6123            Add(items[i]);
 2124    }
 125
 126    /// <summary>
 127    /// Removes the first occurrence of a specific object from the list.
 128    /// Raises collection and property change notifications.
 129    /// </summary>
 130    public override bool Remove(T item)
 503131    {
 503132        int index = IndexOf(item);
 503133        if (index >= 0)
 502134        {
 502135            var removedItem = this[index];
 502136            base.RemoveAt(index);
 502137            OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index);
 502138            OnPropertyChanged(nameof(Count));
 502139            return true;
 140        }
 141
 1142        return false;
 503143    }
 144
 145    /// <summary>
 146    /// Removes the element at the specified index.
 147    /// Raises collection and property change notifications.
 148    /// </summary>
 149    public override void RemoveAt(int index)
 3150    {
 3151        var removedItem = this[index];
 1152        base.RemoveAt(index);
 1153        OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index);
 1154        OnPropertyChanged(nameof(Count));
 1155    }
 156
 157    public override int RemoveAll(Predicate<T> match)
 6158    {
 6159        int removedCount = base.RemoveAll(match);
 160
 5161        if (removedCount > 0)
 3162        {
 3163            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
 3164            OnPropertyChanged(nameof(Count));
 3165        }
 166
 5167        return removedCount;
 5168    }
 169
 170    /// <summary>
 171    /// Inserts an element into the list at the specified index.
 172    /// Raises collection and property change notifications.
 173    /// </summary>
 174    public override void Insert(int index, T item)
 1175    {
 1176        base.Insert(index, item);
 1177        OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
 1178        OnPropertyChanged(nameof(Count));
 1179    }
 180
 181    /// <summary>
 182    /// Clears all elements from the list.
 183    /// Raises a reset collection change notification.
 184    /// </summary>
 185    public override void Clear()
 1186    {
 1187        if (_count > 0)
 1188        {
 1189            base.Clear();
 1190            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
 1191            OnPropertyChanged(nameof(Count));
 1192        }
 1193    }
 194
 195    #endregion
 196
 197    #region Notifications
 198
 199    /// <summary>
 200    /// Raises the <see cref="PropertyChanged"/> event.
 201    /// </summary>
 202    /// <param name="propertyName">The name of the property that changed.</param>
 203    protected virtual void OnPropertyChanged(string propertyName)
 13060204    {
 13060205        var handler = PropertyChanged;
 13060206        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
 13060207    }
 208
 209    /// <summary>
 210    /// Raises the <see cref="CollectionChanged"/> event for the specified action and items.
 211    /// </summary>
 212    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
 13060213    {
 13060214        var handler = CollectionChanged;
 13060215        handler?.Invoke(this, args);
 13060216    }
 217
 218    private void OnCollectionChanged(NotifyCollectionChangedAction action, T item, int index)
 11555219    {
 11555220        var args = new NotifyCollectionChangedEventArgs(action, item, index);
 11555221        OnCollectionChanged(args);
 11555222    }
 223
 224    private void OnCollectionChanged(NotifyCollectionChangedAction action, T oldItem, T newItem, int index)
 1501225    {
 1501226        var args = new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index);
 1501227        OnCollectionChanged(args);
 1501228    }
 229
 230    #endregion
 231}