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

DomCR / ACadSharp / 12905761471

22 Jan 2025 10:03AM UTC coverage: 2.0% (-74.0%) from 75.963%
12905761471

push

github

DomCR
version 1.0.6

104 of 7676 branches covered (1.35%)

Branch coverage included in aggregate %.

590 of 27024 relevant lines covered (2.18%)

5.13 hits per line

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

0.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
                /// <inheritdoc/>
20
                public event EventHandler<CollectionChangedEventArgs> OnRemove;
21

22
                /// <summary>
23
                /// Owner of the collection.
24
                /// </summary>
25
                public CadObject Owner { get; }
×
26

27
                /// <summary>
28
                /// Gets the number of elements that are contained in the collection.
29
                /// </summary>
30
                public int Count { get { return this._entries.Count; } }
×
31

32
                public T this[int index]
33
                {
34
                        get
35
                        {
×
36
                                return this._entries.ElementAtOrDefault(index);
×
37
                        }
×
38
                }
39

40
                protected readonly HashSet<T> _entries = new HashSet<T>();
×
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)
×
47
                {
×
48
                        this.Owner = owner;
×
49
                }
×
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
                {
×
59
                        if (item is null) throw new ArgumentNullException(nameof(item));
×
60

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

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

67
                        this._entries.Add(item);
×
68
                        item.Owner = this.Owner;
×
69

70
                        OnAdd?.Invoke(this, new CollectionChangedEventArgs(item));
×
71
                }
×
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
                {
×
79
                        foreach (var item in items)
×
80
                        {
×
81
                                this.Add(item);
×
82
                        }
×
83
                }
×
84

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

97
                /// <summary>
98
                /// Removes a <see cref="CadObject"/> from the collection, this method triggers <see cref="OnRemove"/>.
99
                /// </summary>
100
                /// <param name="item"></param>
101
                /// <returns>The removed <see cref="CadObject"/></returns>
102
                public virtual T Remove(T item)
103
                {
×
104
                        if (!this._entries.Remove(item))
×
105
                                return null;
×
106

107
                        item.Owner = null;
×
108

109
                        OnRemove?.Invoke(this, new CollectionChangedEventArgs(item));
×
110

111
                        return item;
×
112
                }
×
113

114
                /// <inheritdoc/>
115
                public IEnumerator<T> GetEnumerator()
116
                {
×
117
                        return this._entries.GetEnumerator();
×
118
                }
×
119

120
                /// <inheritdoc/>
121
                IEnumerator IEnumerable.GetEnumerator()
122
                {
×
123
                        return this._entries.GetEnumerator();
×
124
                }
×
125
        }
126
}
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

© 2025 Coveralls, Inc