• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DomCR / ACadSharp / 25813535773

13 May 2026 04:52PM UTC coverage: 76.83% (+0.07%) from 76.763%
25813535773

Pull #1068

github

web-flow
Merge dec987d29 into 683107226
Pull Request #1068: Add ActiveStatus to Viewport

8600 of 12131 branches covered (70.89%)

Branch coverage included in aggregate %.

949 of 1082 new or added lines in 9 files covered. (87.71%)

2 existing lines in 2 files now uncovered.

30834 of 39195 relevant lines covered (78.67%)

157446.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.0
/src/ACadSharp/CadObjectCollection.cs
1
using CSUtilities.Extensions;
2
using System;
3
using System.Collections;
4
using System.Collections.Generic;
5
using System.Linq;
6

7
namespace ACadSharp;
8

9
/// <summary>
10
/// Collection formed by <see cref="CadObject"/> managed by an owner.
11
/// </summary>
12
/// <typeparam name="T"></typeparam>
13
public class CadObjectCollection<T> : IObservableCadCollection<T>
14
        where T : CadObject
15
{
16
        /// <inheritdoc/>
17
        public event EventHandler<CollectionChangedEventArgs> OnAdd;
18

19
        /// <summary>
20
        /// Occurs before an item is removed from the collection.
21
        /// </summary>
22
        /// <remarks>Subscribers can use this event to perform actions or validation before the removal operation
23
        /// completes. To cancel the removal, handle the event and set the appropriate property on the event arguments if
24
        /// supported.</remarks>
25
        public event EventHandler<CollectionChangedEventArgs> OnBeforeRemove;
26

27
        /// <inheritdoc/>
28
        public event EventHandler<CollectionChangedEventArgs> OnRemove;
29

30
        /// <summary>
31
        /// Gets the number of elements that are contained in the collection.
32
        /// </summary>
33
        public int Count { get { return this._entries.Count; } }
462✔
34

35
        /// <summary>
36
        /// Owner of the collection.
37
        /// </summary>
38
        public CadObject Owner { get; }
790,966✔
39

40
        protected readonly HashSet<T> _entries = new HashSet<T>();
57,693✔
41

42
        /// <summary>
43
        /// Default constructor for a <see cref="CadObjectCollection{T}"/> with it's owner assigned.
44
        /// </summary>
45
        /// <param name="owner">Owner of the collection.</param>
46
        public CadObjectCollection(CadObject owner)
57,693✔
47
        {
57,693✔
48
                this.Owner = owner;
57,693✔
49
        }
57,693✔
50

51
        /// <summary>
52
        /// Add a <see cref="CadObject"/> to the collection, this method triggers <see cref="OnAdd"/>.
53
        /// </summary>
54
        /// <param name="item"></param>
55
        /// <exception cref="ArgumentException"></exception>
56
        /// <exception cref="ArgumentNullException"></exception>
57
        public virtual void Add(T item)
58
        {
779,288✔
59
                if (item is null) throw new ArgumentNullException(nameof(item));
779,288!
60

61
                if (item.Owner != null)
779,288✔
62
                        throw new ArgumentException($"Item {item.GetType().FullName} already has an owner", nameof(item));
2✔
63

64
                if (this._entries.Contains(item))
779,286!
NEW
65
                        throw new ArgumentException($"Item {item.GetType().FullName} is already in the collection", nameof(item));
×
66

67
                this._entries.Add(item);
779,286✔
68
                item.Owner = this.Owner;
779,286✔
69

70
                OnAdd?.Invoke(this, new CollectionChangedEventArgs(item));
779,286✔
71
        }
779,286✔
72

73
        /// <summary>
74
        /// Add multiple <see cref="CadObject"/> to the collection, this method triggers <see cref="OnAdd"/>.
75
        /// </summary>
76
        /// <param name="items"></param>
77
        public void AddRange(IEnumerable<T> items)
78
        {
40,058✔
79
                foreach (var item in items)
217,434✔
80
                {
48,630✔
81
                        this.Add(item);
48,630✔
82
                }
48,630✔
83
        }
40,058✔
84

85
        /// <summary>
86
        /// Removes all elements from the Collection.
87
        /// </summary>
88
        public void Clear()
89
        {
132✔
90
                Queue<T> q = new(this._entries.ToList());
132✔
91
                while (q.TryDequeue(out T entry))
154✔
92
                {
22✔
93
                        this.Remove(entry);
22✔
94
                }
22✔
95
        }
132✔
96

97
        /// <inheritdoc/>
98
        public IEnumerator<T> GetEnumerator()
99
        {
71,918✔
100
                return this._entries.GetEnumerator();
71,918✔
101
        }
71,918✔
102

103
        /// <inheritdoc/>
104
        IEnumerator IEnumerable.GetEnumerator()
105
        {
11,013✔
106
                return this._entries.GetEnumerator();
11,013✔
107
        }
11,013✔
108

109
        /// <summary>
110
        /// Removes the specified item from the collection.
111
        /// </summary>
112
        /// <remarks>If an event handler for the removal is registered and cancels the operation, the item will not be
113
        /// removed. After successful removal, the item's owner is set to null and a removal event is raised.</remarks>
114
        /// <param name="item">The item to remove from the collection.</param>
115
        /// <returns>true if the item was successfully removed; otherwise, false.</returns>
116
        public virtual bool Remove(T item)
117
        {
3,451✔
118
                if (this.OnBeforeRemove != null)
3,451✔
119
                {
169✔
120
                        CollectionChangedEventArgs args = new(item);
169✔
121
                        this.OnBeforeRemove.Invoke(this, args);
169✔
122
                        if (args.Cancel)
169✔
123
                        {
1✔
124
                                return false;
1✔
125
                        }
126
                }
168✔
127

128
                if (!this._entries.Remove(item))
3,450!
UNCOV
129
                {
×
NEW
130
                        return false;
×
131
                }
132

133
                item.Owner = null;
3,450✔
134

135
                OnRemove?.Invoke(this, new CollectionChangedEventArgs(item));
3,450✔
136

137
                return true;
3,450✔
138
        }
3,451✔
139

140
        public T this[int index]
141
        {
142
                get
143
                {
55✔
144
                        return this._entries.ElementAtOrDefault(index);
55✔
145
                }
55✔
146
        }
147
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc