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 .
44// Created by: Alex Yakunin
55// Created: 2009.03.26
66
@@ -77,31 +77,25 @@ public void Add(Hint hint)
7777 var nodes = new List < Node > ( ) ;
7878 foreach ( var target in targets ) {
7979 Node node ;
80- if ( target . Model == ModelType . Source )
80+ if ( target . Model == ModelType . Source )
8181 node = ( Node ) SourceModel . Resolve ( target . Path , true ) ;
8282 else
8383 node = ( Node ) TargetModel . Resolve ( target . Path , true ) ;
8484 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 ) ;
8987 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 ) {
10491 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 ) ;
10599 }
106100 }
107101 }
@@ -126,46 +120,20 @@ public void Clear()
126120
127121 /// <inheritdoc/>
128122 /// <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+ } ;
148129
149130 /// <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+ } ;
169137
170138 /// <summary>
171139 /// Determines whether there are any hints associated with the specified.
@@ -175,29 +143,10 @@ public THint[] GetHints<THint>(Node node)
175143 /// <see langword="true"/> if the specified node has associated hints;
176144 /// otherwise, <see langword="false"/>.
177145 /// </returns>
178- public bool HasHints ( Node node )
179- {
180- ArgumentValidator . EnsureArgumentNotNull ( node , "node" ) ;
146+ public bool HasHints ( Node node ) => GetNodeHints ( node ) . Count > 0 ;
181147
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 ) ) ;
201150
202151 #region IEnumerable<...> methods
203152
@@ -215,6 +164,16 @@ IEnumerator IEnumerable.GetEnumerator()
215164
216165 #endregion
217166
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+
218177 #region ILockable methods
219178
220179 /// <inheritdoc/>
0 commit comments