@@ -103,19 +103,14 @@ public void Add(IEntity entity, string parameterName, string expression, bool be
103103 throw new ArgumentNullException ( nameof ( expression ) ) ;
104104 }
105105
106- if ( ! SimulationSpecificUpdates . ContainsKey ( simulation ) )
107- {
108- SimulationSpecificUpdates [ simulation ] = new Dictionary < IEntity , EntityUpdate > ( ) ;
109- }
110-
111- if ( ! SimulationSpecificUpdates [ simulation ] . ContainsKey ( entity ) )
112- {
113- SimulationSpecificUpdates [ simulation ] [ entity ] = new EntityUpdate ( ) ;
114- }
106+ var simulationUpdates = SimulationSpecificUpdates . GetOrAdd ( simulation , _ => new Dictionary < IEntity , EntityUpdate > ( ) ) ;
107+ var entityUpdate = simulationUpdates . ContainsKey ( entity )
108+ ? simulationUpdates [ entity ]
109+ : ( simulationUpdates [ entity ] = new EntityUpdate ( ) ) ;
115110
116111 if ( beforeTemperature )
117112 {
118- SimulationSpecificUpdates [ simulation ] [ entity ] . ParameterUpdatesBeforeTemperature . Add ( new EntityParameterExpressionValueUpdate ( )
113+ entityUpdate . ParameterUpdatesBeforeTemperature . Add ( new EntityParameterExpressionValueUpdate ( )
119114 {
120115 Expression = new DynamicExpression ( expression ) ,
121116 ParameterName = parameterName ,
@@ -140,14 +135,11 @@ public void Add(IEntity entity, string parameterName, string expression, bool be
140135 throw new ArgumentNullException ( nameof ( expression ) ) ;
141136 }
142137
143- if ( ! CommonUpdates . ContainsKey ( entity ) )
144- {
145- CommonUpdates [ entity ] = new EntityUpdate ( ) ;
146- }
138+ var entityUpdate = CommonUpdates . GetOrAdd ( entity , _ => new EntityUpdate ( ) ) ;
147139
148140 if ( beforeTemperature )
149141 {
150- CommonUpdates [ entity ] . ParameterUpdatesBeforeTemperature . Add ( new EntityParameterExpressionValueUpdate ( )
142+ entityUpdate . ParameterUpdatesBeforeTemperature . Add ( new EntityParameterExpressionValueUpdate ( )
151143 {
152144 Expression = new DynamicExpression ( expression ) ,
153145 ParameterName = parameterName ,
@@ -167,14 +159,11 @@ public void Add(IEntity entity, string parameterName, double value, bool beforeT
167159 throw new ArgumentNullException ( nameof ( parameterName ) ) ;
168160 }
169161
170- if ( ! CommonUpdates . ContainsKey ( entity ) )
171- {
172- CommonUpdates [ entity ] = new EntityUpdate ( ) ;
173- }
162+ var entityUpdate = CommonUpdates . GetOrAdd ( entity , _ => new EntityUpdate ( ) ) ;
174163
175164 if ( beforeTemperature )
176165 {
177- CommonUpdates [ entity ] . ParameterUpdatesBeforeTemperature . Add (
166+ entityUpdate . ParameterUpdatesBeforeTemperature . Add (
178167 new EntityParameterDoubleValueUpdate ( ) { ParameterName = parameterName , Value = value } ) ;
179168 }
180169 }
@@ -196,19 +185,14 @@ public void Add(IEntity entity, string parameterName, double value, bool beforeT
196185 throw new ArgumentNullException ( nameof ( parameterName ) ) ;
197186 }
198187
199- if ( ! SimulationSpecificUpdates . ContainsKey ( simulation ) )
200- {
201- SimulationSpecificUpdates [ simulation ] = new Dictionary < IEntity , EntityUpdate > ( ) ;
202- }
203-
204- if ( ! SimulationSpecificUpdates [ simulation ] . ContainsKey ( entity ) )
205- {
206- SimulationSpecificUpdates [ simulation ] [ entity ] = new EntityUpdate ( ) ;
207- }
188+ var simulationUpdates = SimulationSpecificUpdates . GetOrAdd ( simulation , _ => new Dictionary < IEntity , EntityUpdate > ( ) ) ;
189+ var entityUpdate = simulationUpdates . ContainsKey ( entity )
190+ ? simulationUpdates [ entity ]
191+ : ( simulationUpdates [ entity ] = new EntityUpdate ( ) ) ;
208192
209193 if ( beforeTemperature )
210194 {
211- SimulationSpecificUpdates [ simulation ] [ entity ] . ParameterUpdatesBeforeTemperature . Add ( new EntityParameterDoubleValueUpdate { ParameterName = parameterName , Value = value } ) ;
195+ entityUpdate . ParameterUpdatesBeforeTemperature . Add ( new EntityParameterDoubleValueUpdate { ParameterName = parameterName , Value = value } ) ;
212196 }
213197 }
214198
0 commit comments