@@ -38,20 +38,22 @@ public sealed class BalancerAttributes : IDictionary<string, object?>, IReadOnly
38
38
/// <summary>
39
39
/// Gets a read-only collection of metadata attributes.
40
40
/// </summary>
41
- public static readonly BalancerAttributes Empty = new BalancerAttributes ( new ReadOnlyDictionary < string , object ? > ( new Dictionary < string , object ? > ( ) ) ) ;
41
+ public static readonly BalancerAttributes Empty = new BalancerAttributes ( new Dictionary < string , object ? > ( ) , readOnly : true ) ;
42
42
43
- private readonly IDictionary < string , object ? > _attributes ;
43
+ private readonly Dictionary < string , object ? > _attributes ;
44
+ private readonly bool _readOnly ;
44
45
45
46
/// <summary>
46
47
/// Initializes a new instance of the <see cref="BalancerAttributes"/> class.
47
48
/// </summary>
48
- public BalancerAttributes ( ) : this ( new Dictionary < string , object ? > ( ) )
49
+ public BalancerAttributes ( ) : this ( new Dictionary < string , object ? > ( ) , readOnly : false )
49
50
{
50
51
}
51
52
52
- private BalancerAttributes ( IDictionary < string , object ? > attributes )
53
+ private BalancerAttributes ( Dictionary < string , object ? > attributes , bool readOnly )
53
54
{
54
55
_attributes = attributes ;
56
+ _readOnly = readOnly ;
55
57
}
56
58
57
59
object ? IDictionary < string , object ? > . this [ string key ]
@@ -62,28 +64,49 @@ private BalancerAttributes(IDictionary<string, object?> attributes)
62
64
}
63
65
set
64
66
{
67
+ ValidateReadOnly ( ) ;
65
68
_attributes [ key ] = value ;
66
69
}
67
70
}
68
71
69
72
ICollection < string > IDictionary < string , object ? > . Keys => _attributes . Keys ;
70
73
ICollection < object ? > IDictionary < string , object ? > . Values => _attributes . Values ;
71
74
int ICollection < KeyValuePair < string , object ? > > . Count => _attributes . Count ;
72
- bool ICollection < KeyValuePair < string , object ? > > . IsReadOnly => _attributes . IsReadOnly ;
75
+ bool ICollection < KeyValuePair < string , object ? > > . IsReadOnly => _readOnly || ( ( ICollection < KeyValuePair < string , object ? > > ) _attributes ) . IsReadOnly ;
73
76
IEnumerable < string > IReadOnlyDictionary < string , object ? > . Keys => _attributes . Keys ;
74
77
IEnumerable < object ? > IReadOnlyDictionary < string , object ? > . Values => _attributes . Values ;
75
78
int IReadOnlyCollection < KeyValuePair < string , object ? > > . Count => _attributes . Count ;
76
79
object ? IReadOnlyDictionary < string , object ? > . this [ string key ] => _attributes [ key ] ;
77
- void IDictionary < string , object ? > . Add ( string key , object ? value ) => _attributes . Add ( key , value ) ;
78
- void ICollection < KeyValuePair < string , object ? > > . Add ( KeyValuePair < string , object ? > item ) => _attributes . Add ( item ) ;
79
- void ICollection < KeyValuePair < string , object ? > > . Clear ( ) => _attributes . Clear ( ) ;
80
+ void IDictionary < string , object ? > . Add ( string key , object ? value )
81
+ {
82
+ ValidateReadOnly ( ) ;
83
+ _attributes . Add ( key , value ) ;
84
+ }
85
+ void ICollection < KeyValuePair < string , object ? > > . Add ( KeyValuePair < string , object ? > item )
86
+ {
87
+ ValidateReadOnly ( ) ;
88
+ ( ( ICollection < KeyValuePair < string , object ? > > ) _attributes ) . Add ( item ) ;
89
+ }
90
+ void ICollection < KeyValuePair < string , object ? > > . Clear ( )
91
+ {
92
+ ValidateReadOnly ( ) ;
93
+ _attributes . Clear ( ) ;
94
+ }
80
95
bool ICollection < KeyValuePair < string , object ? > > . Contains ( KeyValuePair < string , object ? > item ) => _attributes . Contains ( item ) ;
81
96
bool IDictionary < string , object ? > . ContainsKey ( string key ) => _attributes . ContainsKey ( key ) ;
82
- void ICollection < KeyValuePair < string , object ? > > . CopyTo ( KeyValuePair < string , object ? > [ ] array , int arrayIndex ) => _attributes . CopyTo ( array , arrayIndex ) ;
97
+ void ICollection < KeyValuePair < string , object ? > > . CopyTo ( KeyValuePair < string , object ? > [ ] array , int arrayIndex ) => ( ( ICollection < KeyValuePair < string , object ? > > ) _attributes ) . CopyTo ( array , arrayIndex ) ;
83
98
IEnumerator < KeyValuePair < string , object ? > > IEnumerable < KeyValuePair < string , object ? > > . GetEnumerator ( ) => _attributes . GetEnumerator ( ) ;
84
99
IEnumerator System . Collections . IEnumerable . GetEnumerator ( ) => ( ( System . Collections . IEnumerable ) _attributes ) . GetEnumerator ( ) ;
85
- bool IDictionary < string , object ? > . Remove ( string key ) => _attributes . Remove ( key ) ;
86
- bool ICollection < KeyValuePair < string , object ? > > . Remove ( KeyValuePair < string , object ? > item ) => _attributes . Remove ( item ) ;
100
+ bool IDictionary < string , object ? > . Remove ( string key )
101
+ {
102
+ ValidateReadOnly ( ) ;
103
+ return _attributes . Remove ( key ) ;
104
+ }
105
+ bool ICollection < KeyValuePair < string , object ? > > . Remove ( KeyValuePair < string , object ? > item )
106
+ {
107
+ ValidateReadOnly ( ) ;
108
+ return ( ( ICollection < KeyValuePair < string , object ? > > ) _attributes ) . Remove ( item ) ;
109
+ }
87
110
bool IDictionary < string , object ? > . TryGetValue ( string key , out object ? value ) => _attributes . TryGetValue ( key , out value ) ;
88
111
bool IReadOnlyDictionary < string , object ? > . ContainsKey ( string key ) => _attributes . ContainsKey ( key ) ;
89
112
bool IReadOnlyDictionary < string , object ? > . TryGetValue ( string key , out object ? value ) => _attributes . TryGetValue ( key , out value ) ;
@@ -121,6 +144,7 @@ public bool TryGetValue<TValue>(BalancerAttributesKey<TValue> key, [MaybeNullWhe
121
144
/// <param name="value">The value.</param>
122
145
public void Set < TValue > ( BalancerAttributesKey < TValue > key , TValue value )
123
146
{
147
+ ValidateReadOnly ( ) ;
124
148
_attributes [ key . Key ] = value ;
125
149
}
126
150
@@ -135,10 +159,55 @@ public void Set<TValue>(BalancerAttributesKey<TValue> key, TValue value)
135
159
/// </returns>
136
160
public bool Remove < TValue > ( BalancerAttributesKey < TValue > key )
137
161
{
162
+ ValidateReadOnly ( ) ;
138
163
return _attributes . Remove ( key . Key ) ;
139
164
}
140
165
141
- internal string DebuggerToString ( )
166
+ private void ValidateReadOnly ( )
167
+ {
168
+ if ( _readOnly )
169
+ {
170
+ throw new NotSupportedException ( "Collection is read-only." ) ;
171
+ }
172
+ }
173
+
174
+ internal static bool DeepEquals ( BalancerAttributes ? x , BalancerAttributes ? y )
175
+ {
176
+ var xValues = x ? . _attributes ;
177
+ var yValues = y ? . _attributes ;
178
+
179
+ if ( ReferenceEquals ( xValues , yValues ) )
180
+ {
181
+ return true ;
182
+ }
183
+
184
+ if ( xValues == null || yValues == null )
185
+ {
186
+ return false ;
187
+ }
188
+
189
+ if ( xValues . Count != yValues . Count )
190
+ {
191
+ return false ;
192
+ }
193
+
194
+ foreach ( var kvp in xValues )
195
+ {
196
+ if ( ! yValues . TryGetValue ( kvp . Key , out var value ) )
197
+ {
198
+ return false ;
199
+ }
200
+
201
+ if ( ! Equals ( kvp . Value , value ) )
202
+ {
203
+ return false ;
204
+ }
205
+ }
206
+
207
+ return true ;
208
+ }
209
+
210
+ private string DebuggerToString ( )
142
211
{
143
212
return $ "Count = { _attributes . Count } ";
144
213
}
0 commit comments