1+ using System ;
12using System . Collections . Generic ;
3+ using System . Threading ;
24using Shuttle . Core . Contract ;
35
46namespace Shuttle . Core . Threading ;
57
68public class State : IState
79{
10+ private readonly List < string > _immutableKeys = [ "Name" , "ManagedThreadId" ] ;
811 private readonly Dictionary < string , object ? > _state = new ( ) ;
912
1013 public void Clear ( )
@@ -14,29 +17,37 @@ public void Clear()
1417
1518 public void Add ( string key , object ? value )
1619 {
17- _state . Add ( Guard . AgainstNull ( key ) , value ) ;
20+ _state . Add ( Guard . AgainstNullOrEmptyString ( key ) , value ) ;
1821 }
1922
2023 public void Replace ( string key , object ? value )
2124 {
22- Guard . AgainstNull ( key ) ;
25+ if ( _immutableKeys . Contains ( Guard . AgainstNullOrEmptyString ( key ) ) )
26+ {
27+ throw new InvalidOperationException ( string . Format ( Resources . ImmutableKeyException , key ) ) ;
28+ }
2329
2430 _state . Remove ( key ) ;
2531 _state . Add ( key , value ) ;
2632 }
2733
2834 public object ? Get ( string key )
2935 {
30- return _state . TryGetValue ( Guard . AgainstNull ( key ) , out var result ) ? result : default ;
36+ return _state . TryGetValue ( Guard . AgainstNullOrEmptyString ( key ) , out var result ) ? result : default ;
3137 }
3238
3339 public bool Contains ( string key )
3440 {
35- return _state . ContainsKey ( Guard . AgainstNull ( key ) ) ;
41+ return _state . ContainsKey ( Guard . AgainstNullOrEmptyString ( key ) ) ;
3642 }
3743
3844 public bool Remove ( string key )
3945 {
46+ if ( _immutableKeys . Contains ( Guard . AgainstNullOrEmptyString ( key ) ) )
47+ {
48+ throw new InvalidOperationException ( string . Format ( Resources . ImmutableKeyException , key ) ) ;
49+ }
50+
4051 return _state . Remove ( key ) ;
4152 }
4253}
0 commit comments