1
- // Copyright (C) 2009-2021 Xtensive LLC.
2
- // All rights reserved .
3
- // For conditions of distribution and use, see license .
1
+ // Copyright (C) 2009-2024 Xtensive LLC.
2
+ // This code is distributed under MIT license terms .
3
+ // See the License.txt file in the project root for more information .
4
4
// Created by: Alex Yakunin
5
5
// Created: 2009.03.26
6
6
@@ -77,31 +77,25 @@ public void Add(Hint hint)
77
77
var nodes = new List < Node > ( ) ;
78
78
foreach ( var target in targets ) {
79
79
Node node ;
80
- if ( target . Model == ModelType . Source )
80
+ if ( target . Model == ModelType . Source )
81
81
node = ( Node ) SourceModel . Resolve ( target . Path , true ) ;
82
82
else
83
83
node = ( Node ) TargetModel . Resolve ( target . Path , true ) ;
84
84
nodes . Add ( node ) ;
85
-
86
- if ( ! hintMap . ContainsKey ( node ) )
87
- hintMap . Add ( node , new Dictionary < Type , object > ( ) ) ;
88
- var nodeHintMap = hintMap [ node ] ;
85
+
86
+ var nodeHintMap = GetNodeHints ( node ) ;
89
87
var hintType = hint . GetType ( ) ;
90
-
91
- if ( ! nodeHintMap . ContainsKey ( hintType ) )
92
- nodeHintMap . Add ( hintType , null ) ;
93
-
94
- var hintOrList = nodeHintMap [ hintType ] ;
95
- if ( hintOrList == null )
96
- nodeHintMap [ hintType ] = hint ;
97
- else {
98
- var list = hintOrList as List < Hint > ;
99
- if ( list == null ) {
100
- var oldHint = ( Hint ) hintOrList ;
101
- nodeHintMap [ hintType ] = new List < Hint > ( new [ ] { oldHint , hint } ) ;
102
- }
103
- else
88
+
89
+ if ( nodeHintMap . TryGetValue ( hintType , out var hintOrList ) ) {
90
+ if ( hintOrList is List < Hint > list ) {
104
91
list . Add ( hint ) ;
92
+ }
93
+ else {
94
+ nodeHintMap [ hintType ] = new List < Hint > ( new [ ] { ( Hint ) hintOrList , hint } ) ;
95
+ }
96
+ }
97
+ else {
98
+ nodeHintMap . Add ( hintType , hint ) ;
105
99
}
106
100
}
107
101
}
@@ -126,46 +120,20 @@ public void Clear()
126
120
127
121
/// <inheritdoc/>
128
122
/// <exception cref="InvalidOperationException">Multiple hints found.</exception>
129
- public THint GetHint < THint > ( Node node )
130
- where THint : Hint
131
- {
132
- ArgumentValidator . EnsureArgumentNotNull ( node , "node" ) ;
133
-
134
- if ( ! hintMap . ContainsKey ( node ) )
135
- hintMap . Add ( node , new Dictionary < Type , object > ( ) ) ;
136
- var nodeHintMap = hintMap . GetValueOrDefault ( node ) ;
137
- if ( nodeHintMap == null )
138
- return null ;
139
- var hintType = typeof ( THint ) ;
140
- var hintOrList = nodeHintMap . GetValueOrDefault ( hintType ) ;
141
- if ( hintOrList == null )
142
- return null ;
143
- var hint = hintOrList as THint ;
144
- if ( hint != null )
145
- return hint ;
146
- throw new InvalidOperationException ( Strings . ExMultipleHintsFound ) ;
147
- }
123
+ public THint GetHint < THint > ( Node node ) where THint : Hint =>
124
+ GetNodeHints ( node ) . GetValueOrDefault ( typeof ( THint ) ) switch {
125
+ null => null ,
126
+ THint hint => hint ,
127
+ _ => throw new InvalidOperationException ( Strings . ExMultipleHintsFound )
128
+ } ;
148
129
149
130
/// <inheritdoc/>
150
- public THint [ ] GetHints < THint > ( Node node )
151
- where THint : Hint
152
- {
153
- ArgumentValidator . EnsureArgumentNotNull ( node , "node" ) ;
154
-
155
- if ( ! hintMap . ContainsKey ( node ) )
156
- hintMap . Add ( node , new Dictionary < Type , object > ( ) ) ;
157
- var nodeHintMap = hintMap . GetValueOrDefault ( node ) ;
158
- if ( nodeHintMap == null )
159
- return ArrayUtils < THint > . EmptyArray ;
160
- var hintType = typeof ( THint ) ;
161
- var hintOrList = nodeHintMap . GetValueOrDefault ( hintType ) ;
162
- if ( hintOrList == null )
163
- return ArrayUtils < THint > . EmptyArray ;
164
- var hint = hintOrList as THint ;
165
- if ( hint != null )
166
- return new [ ] { hint } ;
167
- return ( ( List < Hint > ) hintOrList ) . Cast < THint > ( ) . ToArray ( ) ;
168
- }
131
+ public THint [ ] GetHints < THint > ( Node node ) where THint : Hint =>
132
+ GetNodeHints ( node ) . GetValueOrDefault ( typeof ( THint ) ) switch {
133
+ null => Array . Empty < THint > ( ) ,
134
+ THint hint => new [ ] { hint } ,
135
+ var list => ( ( List < Hint > ) list ) . Cast < THint > ( ) . ToArray ( )
136
+ } ;
169
137
170
138
/// <summary>
171
139
/// Determines whether there are any hints associated with the specified.
@@ -175,29 +143,10 @@ public THint[] GetHints<THint>(Node node)
175
143
/// <see langword="true"/> if the specified node has associated hints;
176
144
/// otherwise, <see langword="false"/>.
177
145
/// </returns>
178
- public bool HasHints ( Node node )
179
- {
180
- ArgumentValidator . EnsureArgumentNotNull ( node , "node" ) ;
146
+ public bool HasHints ( Node node ) => GetNodeHints ( node ) . Count > 0 ;
181
147
182
- if ( ! hintMap . ContainsKey ( node ) )
183
- hintMap . Add ( node , new Dictionary < Type , object > ( ) ) ;
184
- var nodeHintMap = hintMap . GetValueOrDefault ( node ) ;
185
- if ( nodeHintMap == null )
186
- return false ;
187
-
188
- return nodeHintMap . Values . Count > 0 ;
189
- }
190
-
191
- public bool HasHints < THint > ( Node node )
192
- where THint : Hint
193
- {
194
- ArgumentValidator . EnsureArgumentNotNull ( node , "node" ) ;
195
-
196
- if ( ! hintMap . TryGetValue ( node , out var nodeHintMap ) ) {
197
- hintMap . Add ( node , nodeHintMap = new Dictionary < Type , object > ( ) ) ;
198
- }
199
- return nodeHintMap . ContainsKey ( typeof ( THint ) ) ;
200
- }
148
+ public bool HasHints < THint > ( Node node ) where THint : Hint =>
149
+ GetNodeHints ( node ) . ContainsKey ( typeof ( THint ) ) ;
201
150
202
151
#region IEnumerable<...> methods
203
152
@@ -215,6 +164,16 @@ IEnumerator IEnumerable.GetEnumerator()
215
164
216
165
#endregion
217
166
167
+ private Dictionary < Type , object > GetNodeHints ( Node node )
168
+ {
169
+ ArgumentValidator . EnsureArgumentNotNull ( node , "node" ) ;
170
+
171
+ if ( ! hintMap . TryGetValue ( node , out var nodeHintMap ) ) {
172
+ hintMap . Add ( node , nodeHintMap = new Dictionary < Type , object > ( ) ) ;
173
+ }
174
+ return nodeHintMap ;
175
+ }
176
+
218
177
#region ILockable methods
219
178
220
179
/// <inheritdoc/>
0 commit comments