Appendix A: ListBase<T>
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
| #region Using Statements
using System;
using System.Collections.Generic;
#endregion
namespace AdvancedStateSystem
{
/// <summary>
/// A List class that inherits from the generic List class and implements events for item addition and removals.
/// </summary>
/// <typeparam name="T">The type of the objects in the collection.</typeparam>
class ListBase<T> : List<T>
{
#region Events
/// <summary>
/// Triggered whenever an item is added.
/// </summary>
public event EventHandler ItemAdded;
/// <summary>
/// Triggered whenever an item is removed.
/// </summary>
public event EventHandler ItemRemoved;
#endregion
#region Constructor
/// <summary>
/// Default constructor.
/// </summary>
public ListBase()
{
// Add event handlers
ItemAdded += OnItemAdded;
ItemRemoved += OnItemRemoved;
}
#endregion
#region Event Handlers
/// <summary>
/// Handles when an item is added to the collection.
/// </summary>
/// <param name="sender">The item added.</param>
/// <param name="e">Event Arguments.</param>
protected virtual void OnItemAdded(object sender, EventArgs e) { }
/// <summary>
/// Handles when an item is removed from the collection.
/// </summary>
/// <param name="sender">The item removed.</param>
/// <param name="e">Event Arguments.</param>
protected virtual void OnItemRemoved(object sender, EventArgs e) { }
#endregion
#region Adding
/// <summary>
/// Adds an item to the collection.
/// </summary>
/// <param name="item">The item to add.</param>
public new void Add(T item)
{
base.Add(item);
if (ItemAdded != null)
ItemAdded.Invoke(item, EventArgs.Empty);
}
/// <summary>
/// Adds a range of items to the collection.
/// </summary>
/// <param name="items">Items to be added.</param>
public new void AddRange(IEnumerable<T> items)
{
foreach (T item in items)
Add(item);
}
#endregion
#region Removing
/// <summary>
/// Removes a single item.
/// </summary>
/// <param name="item">The item to remove.</param>
/// <returns>True if the item was removed.</returns>
public new bool Remove(T item)
{
bool _returned = base.Remove(item);
if (_returned && ItemRemoved != null)
ItemRemoved.Invoke(item, EventArgs.Empty);
return _returned;
}
/// <summary>
/// Removes all items that are matched.
/// </summary>
/// <param name="match">The predicate that provides matching.</param>
/// <returns>The number of items removed.</returns>
public new int RemoveAll(Predicate<T> match)
{
// If the predicate isn't valid, throw an exception.
if (match == null)
throw new ArgumentNullException("match", "Predicate was null.");
int _count = 0;
for (int i = 0; i < Count; i++)
{
T item = this[i];
if (match(item))
{
if (Remove(item)) _count++;
}
}
return _count;
}
/// <summary>
/// Remove an item at a specified index.
/// </summary>
/// <param name="index">The index at which to remove an item.</param>
/// <returns>True if the item was removed.</returns>
public new bool RemoveAt(int index)
{
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException();
return Remove(this[index]);
}
/// <summary>
/// Removes a range of items from the collection.
/// </summary>
/// <param name="index">The start index for removing.</param>
/// <param name="count">The number of items to remove.</param>
public new void RemoveRange(int index, int count)
{
if (index < 0)
throw new IndexOutOfRangeException();
if (count <= 0 || (index + count - 1) >= Count)
throw new IndexOutOfRangeException();
// Start from the back of the range to avoid
// removal errors.
for (int i = count + index - 1; i >= index; i--)
Remove(this[i]);
}
/// <summary>
/// Clears the collection of all items.
/// </summary>
public new void Clear()
{
if (Count <= 0)
return;
RemoveRange(0, Count);
}
#endregion
}
} |