using System;
using System.Collections.Generic;
using Chernobyl.Event;
namespace Chernobyl.Collections.Generic.Event
{
///
/// Type that is designed to make the implementation of new
/// easier to create.
///
/// The type of the key.
/// The type of the value.
public abstract class EventDictionary :
EventCollection>, IEventDictionary
{
///
/// Adds an element with the provided key and value to the
/// .
///
/// The object to use as the key of the element to add.
/// The object to use as the value of the element to add.
/// The key provided is null.
/// An element with the same key
/// already exists in the .
/// The
/// is read-only.
public void Add(TKey key, TValue value)
{
var keyValue = new KeyValuePair(key, value);
DictionaryDecorated.Add(keyValue);
if(ItemsAddedHandler != null)
ItemsAddedHandler(this, new ItemsEventArgs>(
keyValue));
}
///
/// Determines whether the
/// contains an element with the specified key.
///
/// The key to locate in the
/// .
///
/// True if the contains an
/// element with the key; otherwise, false.
///
/// The key provided is null.
public bool ContainsKey(TKey key)
{
return DictionaryDecorated.ContainsKey(key);
}
///
/// Gets an containing the keys of the
/// .
///
public ICollection Keys
{
get { return DictionaryDecorated.Keys; }
}
///
/// Removes the element with the specified key from the
/// .
///
/// The key of the element to remove.
///
/// true if the element is successfully removed; otherwise, false. This
/// method also returns false if was not found
/// in the original .
///
/// The key provided is null.
/// The
/// is read-only.
public bool Remove(TKey key)
{
bool removed = false;
TValue valueRemoved;
if (DictionaryDecorated.TryGetValue(key, out valueRemoved))
{
removed = DictionaryDecorated.Remove(key);
if (removed && ItemsRemovedHandler != null)
ItemsRemovedHandler(this, new ItemsEventArgs>(
new KeyValuePair(key, valueRemoved)));
}
return removed;
}
///
/// Gets the value associated with the specified key.
///
/// The key whose value to get.
/// When this method returns, the value associated
/// with the specified key, if the key is found; otherwise, the default
/// value for the type of the parameter. This
/// parameter is passed uninitialized.
///
/// True if the object that implements
/// contains an element with the specified key; otherwise, false.
///
/// The key provided is null.
public bool TryGetValue(TKey key, out TValue value)
{
return DictionaryDecorated.TryGetValue(key, out value);
}
///
/// Gets an containing the values in the
/// .
///
public ICollection Values
{
get { return DictionaryDecorated.Values; }
}
///
/// Gets or sets the element with the specified key.
///
/// The key of the element to get or set.
/// The element with the specified key.
/// The key provided is null.
/// The property is retrieved and
/// key is not found.
/// The property is set and the
/// is read-only.
public TValue this[TKey key]
{
get { return DictionaryDecorated[key]; }
set
{
// Store the old item but only if we need to since the search
// for that item could be expensive.
ItemsEventArgs> itemRemoved = null;
if (ItemsRemovedHandler != null)
itemRemoved = new ItemsEventArgs>(
new KeyValuePair(key, DictionaryDecorated[key]));
DictionaryDecorated[key] = value;
if (ItemsRemovedHandler != null)
ItemsRemovedHandler(this, itemRemoved);
if (ItemsAddedHandler != null)
ItemsAddedHandler(this, new ItemsEventArgs>(
new KeyValuePair(key, value)));
}
}
///
/// The instance that is being decorated with event capabilities.
///
protected abstract IDictionary DictionaryDecorated { get; }
///
/// The being decorated or extended with
/// event capabilities.
///
protected override ICollection> CollectionDecorated
{
get { return DictionaryDecorated; }
}
}
}