1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
3
4
using System . Globalization ;
4
5
using System . Linq ;
@@ -15,13 +16,13 @@ namespace Nest
15
16
internal static class Extensions
16
17
{
17
18
internal static TReturn InvokeOrDefault < T , TReturn > ( this Func < T , TReturn > func , T @default )
18
- where T : class , TReturn where TReturn : class =>
19
+ where T : class , TReturn where TReturn : class =>
19
20
func ? . Invoke ( @default ) ?? @default ;
20
-
21
+
21
22
internal static TReturn InvokeOrDefault < T1 , T2 , TReturn > ( this Func < T1 , T2 , TReturn > func , T1 @default , T2 param2 )
22
- where T1 : class , TReturn where TReturn : class =>
23
+ where T1 : class , TReturn where TReturn : class =>
23
24
func ? . Invoke ( @default , param2 ) ?? @default ;
24
-
25
+
25
26
internal static QueryContainer InvokeQuery < T > (
26
27
this Func < QueryContainerDescriptor < T > , QueryContainer > f ,
27
28
QueryContainerDescriptor < T > container )
@@ -33,7 +34,7 @@ internal static QueryContainer InvokeQuery<T>(
33
34
return c ;
34
35
35
36
//query is conditionless but the container is marked as strict, throw exception
36
- if ( c != null && c . IsStrict )
37
+ if ( c != null && c . IsStrict )
37
38
throw new ArgumentException ( "Query is conditionless but strict is turned on" ) ;
38
39
39
40
//query is conditionless return an empty container that can later be rewritten
@@ -53,7 +54,7 @@ internal static string GetStringValue(this Enum enumValue)
53
54
54
55
return da != null ? da . Value : Enum . GetName ( enumValue . GetType ( ) , enumValue ) ;
55
56
}
56
-
57
+
57
58
internal static readonly JsonConverter dateConverter = new IsoDateTimeConverter { Culture = CultureInfo . InvariantCulture } ;
58
59
internal static readonly JsonSerializer serializer = new JsonSerializer ( ) ;
59
60
internal static string ToJsonNetString ( this DateTime date )
@@ -70,41 +71,40 @@ internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Fu
70
71
return items . GroupBy ( property ) . Select ( x => x . First ( ) ) ;
71
72
}
72
73
73
- internal static Dictionary < string , object > _enumCache = new Dictionary < string , object > ( ) ;
74
+ internal static ConcurrentDictionary < string , object > _enumCache = new ConcurrentDictionary < string , object > ( ) ;
74
75
internal static T ? ToEnum < T > ( this string str ) where T : struct
75
76
{
76
77
var enumType = typeof ( T ) ;
77
78
var key = $ "{ enumType . Name } .{ str } ";
78
- if ( _enumCache . ContainsKey ( key ) )
79
- return ( T ) _enumCache [ key ] ;
79
+ object value ;
80
+ if ( _enumCache . TryGetValue ( key , out value ) )
81
+ return ( T ) value ;
80
82
81
- foreach ( var name in Enum . GetNames ( enumType ) )
83
+ foreach ( var name in Enum . GetNames ( enumType ) )
82
84
{
83
- if ( name . Equals ( str , StringComparison . OrdinalIgnoreCase ) ) return ( T ) Enum . Parse ( enumType , name , true ) ;
85
+ if ( name . Equals ( str , StringComparison . OrdinalIgnoreCase ) )
86
+ {
87
+ var v = ( T ) Enum . Parse ( enumType , name , true ) ;
88
+ _enumCache . TryAdd ( key , v ) ;
89
+ return v ;
90
+ }
84
91
85
92
var enumFieldInfo = enumType . GetField ( name ) ;
86
93
var enumMemberAttribute = enumFieldInfo . GetCustomAttribute < EnumMemberAttribute > ( ) ;
87
-
88
- if ( enumMemberAttribute != null )
94
+ if ( enumMemberAttribute ? . Value == str )
89
95
{
90
- if ( enumMemberAttribute . Value == str )
91
- {
92
- var value = ( T ) Enum . Parse ( enumType , name ) ;
93
- _enumCache . Add ( key , value ) ;
94
- return value ;
95
- }
96
+ var v = ( T ) Enum . Parse ( enumType , name ) ;
97
+ _enumCache . TryAdd ( key , v ) ;
98
+ return v ;
96
99
}
97
100
98
101
var alternativeEnumMemberAttribute = enumFieldInfo . GetCustomAttribute < AlternativeEnumMemberAttribute > ( ) ;
99
102
100
- if ( alternativeEnumMemberAttribute != null )
103
+ if ( alternativeEnumMemberAttribute ? . Value == str )
101
104
{
102
- if ( alternativeEnumMemberAttribute . Value == str )
103
- {
104
- var value = ( T ) Enum . Parse ( enumType , name ) ;
105
- _enumCache . Add ( key , value ) ;
106
- return value ;
107
- }
105
+ var v = ( T ) Enum . Parse ( enumType , name ) ;
106
+ _enumCache . TryAdd ( key , v ) ;
107
+ return v ;
108
108
}
109
109
}
110
110
0 commit comments