Removed overridable change notifications from MultiDictionary - accurately sending these would involve considerable overhead; added unit tests for all main interface methods of the MultiDictionary

git-svn-id: file:///srv/devel/repo-conversion/nusu@261 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-02 23:00:02 +00:00
parent b37c4a757c
commit df169e376a
4 changed files with 663 additions and 79 deletions

View file

@ -328,43 +328,17 @@ namespace Nuclex.Support.Collections {
get { return this.typedDictionary[key]; }
set {
if(value == null) {
this.typedDictionary.Remove(key);
}
ICollection<TValue> currentValues;
if(this.typedDictionary.TryGetValue(key, out currentValues)) {
ValueList currentValueList = (ValueList)currentValues;
int index = 0;
foreach(TValue addedValue in value) {
if(index < currentValueList.Count) {
TValue original = currentValueList[index];
currentValueList[index] = addedValue;
OnReplaced(
new KeyValuePair<TKey, TValue>(key, original),
new KeyValuePair<TKey, TValue>(key, addedValue)
);
} else {
currentValueList.Add(addedValue);
OnAdded(new KeyValuePair<TKey, TValue>(key, addedValue));
}
++index;
}
int count = currentValueList.Count;
while(count > index) {
--count;
TValue removedValue = currentValueList[count];
currentValueList.RemoveAt(count);
OnRemoved(new KeyValuePair<TKey, TValue>(key, removedValue));
}
RemoveKey(key);
} else {
currentValues = new ValueList(this);
this.typedDictionary.Add(key, currentValues);
ICollection<TValue> currentValues;
if(this.typedDictionary.TryGetValue(key, out currentValues)) {
currentValues.Clear();
} else {
currentValues = new ValueList(this);
this.typedDictionary.Add(key, currentValues);
}
foreach(TValue addedValue in value) {
currentValues.Add(addedValue);
OnAdded(new KeyValuePair<TKey, TValue>(key, addedValue));
}
}
}
@ -381,7 +355,6 @@ namespace Nuclex.Support.Collections {
}
values.Add(value);
OnAdded(new KeyValuePair<TKey, TValue>(key, value));
}
/// <summary>
@ -397,12 +370,9 @@ namespace Nuclex.Support.Collections {
ICollection<TValue> values;
if(this.typedDictionary.TryGetValue(key, out values)) {
values.Remove(value);
if(values.Count == 0) {
this.typedDictionary.Remove(key);
}
OnRemoved(new KeyValuePair<TKey, TValue>(key, value));
return true;
} else {
return false;
@ -418,10 +388,6 @@ namespace Nuclex.Support.Collections {
if(this.typedDictionary.TryGetValue(key, out values)) {
this.count -= values.Count;
this.typedDictionary.Remove(key);
foreach(TValue value in values) {
OnRemoved(new KeyValuePair<TKey, TValue>(key, value));
}
return values.Count;
} else {
return 0;
@ -430,33 +396,10 @@ namespace Nuclex.Support.Collections {
/// <summary>Removes all items from the Dictionary</summary>
public void Clear() {
OnClearing();
this.typedDictionary.Clear();
this.count = 0;
OnCleared();
}
/// <summary>Fires the 'ItemAdded' event</summary>
/// <param name="item">Item that has been added to the collection</param>
protected virtual void OnAdded(KeyValuePair<TKey, TValue> item) { }
/// <summary>Fires the 'ItemRemoved' event</summary>
/// <param name="item">Item that has been removed from the collection</param>
protected virtual void OnRemoved(KeyValuePair<TKey, TValue> item) { }
/// <summary>Fires the 'ItemReplaced' event</summary>
/// <param name="oldItem">Item that was replaced in the collection</param>
/// <param name="newItem">Item that the original was replaced by</param>
protected virtual void OnReplaced(
KeyValuePair<TKey, TValue> oldItem, KeyValuePair<TKey, TValue> newItem
) { }
/// <summary>Fires the 'Clearing' event</summary>
protected virtual void OnClearing() { }
/// <summary>Fires the 'Cleared' event</summary>
protected virtual void OnCleared() { }
/// <summary>The wrapped Dictionary under its type-safe interface</summary>
private IDictionary<TKey, ICollection<TValue>> typedDictionary;
/// <summary>The wrapped Dictionary under its object interface</summary>