chore: Support mutations in AggregateDictionary (optionally)
This commit is contained in:
1 parent
0a4a7693ac
commit
8ca354e5d7
1 file changed
+16
-4
@@ -7,22 +7,29 @@ namespace Spark.Core.Collections {
|
||||
|
||||
public static class AggregateDictionary {
|
||||
public static AggregateDictionary<K, V> Create<K, V>(
|
||||
IDictionary<K, V> src1, IDictionary<K, V> src2) where K : notnull =>
|
||||
new(src1, src2);
|
||||
IDictionary<K, V> src1, IDictionary<K, V> src2, bool isReadOnly = true) where K : notnull =>
|
||||
new(src1, src2, isReadOnly);
|
||||
}
|
||||
|
||||
public class AggregateDictionary<TKey, TValue> : IDictionary<TKey, TValue> where TKey : notnull {
|
||||
readonly IDictionary<TKey, TValue> src1;
|
||||
readonly IDictionary<TKey, TValue> src2;
|
||||
readonly bool isReadOnly;
|
||||
|
||||
public AggregateDictionary(IDictionary<TKey, TValue> src1, IDictionary<TKey, TValue> src2) {
|
||||
this.src1 = src1;
|
||||
this.src2 = src2;
|
||||
}
|
||||
|
||||
public AggregateDictionary(IDictionary<TKey, TValue> src1, IDictionary<TKey, TValue> src2, bool isReadOnly = true) {
|
||||
this.src1 = src1;
|
||||
this.src2 = src2;
|
||||
this.isReadOnly = isReadOnly;
|
||||
}
|
||||
|
||||
public int Count => src1.Count + src2.Count;
|
||||
|
||||
public bool IsReadOnly => true;
|
||||
public bool IsReadOnly => isReadOnly;
|
||||
|
||||
public ICollection<TKey> Keys =>
|
||||
AggregateCollection.Create(src1.Keys, src2.Keys);
|
||||
@@ -40,7 +47,12 @@ namespace Spark.Core.Collections {
|
||||
}
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
set => throw new NotSupportedException();
|
||||
set {
|
||||
if (isReadOnly) {
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
src1[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(TKey key, TValue value) =>
|
||||
|
||||
Reference in new issue
Block a user