Commit 536830bf authored by Ben Lambell's avatar Ben Lambell

Delete OnChangeDictionary

parent a62daf02
using System;
using System.Collections;
using System.Collections.Generic;
using Recurse.Common.Serialization;
namespace Recurse.Common.Collections
{
[Serializable]
public abstract class OnChangeDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private readonly IDictionary<TKey, TValue> _implementation
= new SerializableDictionary<TKey, TValue>();
public TValue this[TKey key]
{
get { return _implementation[key]; }
set
{
_implementation[key] = value;
}
}
#region Read Operations
public ICollection<TKey> Keys => _implementation.Keys;
public ICollection<TValue> Values => _implementation.Values;
public int Count => _implementation.Count;
public bool IsReadOnly => ((IDictionary)_implementation).IsReadOnly;
public bool Contains(KeyValuePair<TKey, TValue> item) => ((IDictionary<TKey, TValue>)_implementation).Contains(item);
public bool ContainsKey(TKey key) => _implementation.ContainsKey(key);
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
=> ((IDictionary<TKey, TValue>)_implementation).CopyTo(array, arrayIndex);
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => _implementation.GetEnumerator();
public bool TryGetValue(TKey key, out TValue value) => _implementation.TryGetValue(key, out value);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
#region Write Operations
public void Add(TKey key, TValue value)
{
_implementation.Add(key, value);
OnChange();
}
public void Add(KeyValuePair<TKey, TValue> item)
{
((IDictionary<TKey, TValue>)_implementation).Add(item);
OnChange();
}
public void Clear()
{
if (Count != 0)
{
_implementation.Clear();
OnChange();
}
}
public bool Remove(TKey key)
{
if (_implementation.Remove(key))
{
OnChange();
return true;
}
return false;
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
if (((IDictionary<TKey, TValue>)_implementation).Remove(item))
{
OnChange();
return true;
}
return false;
}
#endregion
protected abstract void OnChange();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment