Innovenergy_trunk/csharp/lib/Protocols/DBus/Daemon/ObservableDictionary.cs

52 lines
1.6 KiB
C#

using System.Collections.Immutable;
namespace InnovEnergy.Lib.Protocols.DBus.Daemon;
// TODO
#pragma warning disable CS8714
public record ObservableDictionary<K, V>
(
IReadOnlyList<KeyValuePair<K, V>> Added,
IReadOnlyList<KeyValuePair<K, V>> Removed,
IReadOnlyList<KeyValuePair<K, V>> Changed,
ImmutableDictionary<K, V> Items
)
{
private static KeyValuePair<K, V>[] Nothing { get; } = Array.Empty<KeyValuePair<K, V>>();
public static ObservableDictionary<K, V> Empty { get; } = new ObservableDictionary<K, V>(Nothing, Nothing, Nothing, ImmutableDictionary<K, V>.Empty);
public ObservableDictionary<K, V> Add(K key, V value)
{
var added = new[] { new KeyValuePair<K, V>(key, value) };
return new ObservableDictionary<K, V>(added, Nothing, Nothing, Items.Add(key, value));
}
public ObservableDictionary<K, V> AddRange(params KeyValuePair<K, V>[] added)
{
return new ObservableDictionary<K, V>(added, Nothing, Nothing, Items.AddRange(added));
}
public ObservableDictionary<K, V> SetItem(K key, V value)
{
var added = new[] { new KeyValuePair<K, V>(key, value) };
return new ObservableDictionary<K, V>(added, Nothing, Nothing, Items.SetItem(key, value));
}
public ObservableDictionary<K, V> SetItems(params KeyValuePair<K, V>[] items)
{
return new ObservableDictionary<K, V>(items, Nothing, Nothing, Items.SetItems(items));
}
// public ObservableDictionary<K, V> Remove(params K[] removed)
// {
// return new ObservableDictionary<K, V>(Nothing, removed, Nothing, Items.RemoveRange(removed));
// }
};