-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadOnlyBag.cs
172 lines (138 loc) · 3.75 KB
/
ReadOnlyBag.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace Grammophone.GenericContentModel
{
/// <summary>
/// An implementation of <see cref="IReadOnlyBag{E}"/>,
/// an immmutable collection of elements.
/// </summary>
/// <typeparam name="E">The type of the elements.</typeparam>
[Serializable]
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(DebuggerProxies.BagDebuggerProxy<>))]
public class ReadOnlyBag<E> : IReadOnlyBag<E>, ICollection<E>, IInitializable<E>
{
#region Protected fields
/// <summary>
/// The set supporting this collection.
/// </summary>
protected ISet<E> collection;
#endregion
#region Construction
/// <summary>
/// Create an empty collection.
/// </summary>
public ReadOnlyBag()
: this(null)
{
}
/// <summary>
/// Create.
/// </summary>
/// <param name="elements">The optional set of elements to be contained, else empty set if null.</param>
public ReadOnlyBag(IEnumerable<E> elements)
{
this.collection = new HashSet<E>();
if (elements != null)
{
foreach (var element in elements)
{
AddItem(element);
}
}
}
#endregion
#region IReadOnlyBag<C> Members
/// <summary>
/// The number of items in the collection.
/// </summary>
public int Count
{
get { return this.collection.Count; }
}
#endregion
#region IEnumerable<E> Members
public IEnumerator<E> GetEnumerator()
{
return this.collection.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.collection.GetEnumerator();
}
#endregion
#region Internal methods
/// <summary>
/// Called when an item is added to the collection.
/// </summary>
/// <param name="element">The added element.</param>
/// <returns>Returns true if the element was not already in the collection and added successfully, else false.</returns>
internal protected virtual bool AddItem(E element)
{
return collection.Add(element);
}
/// <summary>
/// Called when an item is removed from the collection.
/// </summary>
/// <param name="element">The element to be removed.</param>
/// <returns>Returns true if the lement was found and removed.</returns>
internal protected virtual bool RemoveItem(E element)
{
return collection.Remove(element);
}
#endregion
#region ICollection<E> Members
void ICollection<E>.Add(E item)
{
AddItem(item);
}
void ICollection<E>.Clear()
{
throw new NotImplementedException();
}
/// <summary>
/// Checks whether an item exists in the collection.
/// </summary>
/// <param name="item">The item to check.</param>
/// <returns>Returns true if the item is found in the collection.</returns>
public bool Contains(E item)
{
return collection.Contains(item);
}
/// <summary>
/// Copy the elements of the collection to an array.
/// </summary>
/// <param name="array">The array to copy the elements to.</param>
/// <param name="arrayIndex">The starting index of the copy.</param>
public void CopyTo(E[] array, int arrayIndex)
{
if (array == null) throw new ArgumentNullException("array");
if (arrayIndex < 0 || array.Length - arrayIndex < this.Count) throw new ArgumentException("arrayIndex is out of bounds.", "arrayIndex");
int currentIndex = arrayIndex;
foreach (var item in this)
{
array[currentIndex++] = item;
}
}
bool ICollection<E>.IsReadOnly
{
get { return true; }
}
bool ICollection<E>.Remove(E item)
{
throw new NotImplementedException();
}
#endregion
#region IInitializable<E> Members
bool IInitializable<E>.AddItem(E element)
{
return this.AddItem(element);
}
#endregion
}
}