4
4
5
5
using System ;
6
6
using System . Diagnostics ;
7
- using System . Diagnostics . CodeAnalysis ;
8
7
using System . Globalization ;
9
8
using System . Linq . Expressions ;
10
9
using System . Reflection ;
15
14
#if ELASTICSEARCH_SERVERLESS
16
15
namespace Elastic . Clients . Elasticsearch . Serverless ;
17
16
#else
18
-
19
17
namespace Elastic . Clients . Elasticsearch ;
20
18
#endif
21
19
@@ -25,6 +23,10 @@ public sealed class Field :
25
23
IEquatable < Field > ,
26
24
IUrlParameter
27
25
{
26
+ #if ! NET && ! NETSTANDARD2_1_OR_GREATER
27
+ private static readonly char [ ] BoostSplit = [ '^' ] ;
28
+ #endif
29
+
28
30
private readonly object _comparisonValue ;
29
31
private readonly Type ? _type ;
30
32
@@ -67,22 +69,10 @@ public sealed class Field :
67
69
68
70
#region Constructors
69
71
70
- public Field ( string name ) : this ( name , null , null )
71
- {
72
- }
73
-
74
- public Field ( string name , double boost ) : this ( name , boost , null )
75
- {
76
- }
77
-
78
- public Field ( string name , string format ) : this ( name , null , format )
79
- {
80
- }
81
-
82
- public Field ( string name , double ? boost , string ? format )
72
+ public Field ( string name , double ? boost = null , string ? format = null )
83
73
{
84
74
if ( string . IsNullOrEmpty ( name ) )
85
- throw new ArgumentException ( $ " { name } can not be null or empty.", nameof ( name ) ) ;
75
+ throw new ArgumentException ( "Field name can not be null or empty.", nameof ( name ) ) ;
86
76
87
77
Name = ParseFieldName ( name , out var b ) ;
88
78
Boost = b ?? boost ;
@@ -91,6 +81,16 @@ public Field(string name, double? boost, string? format)
91
81
_comparisonValue = Name ;
92
82
}
93
83
84
+ public Field ( string name , double boost ) :
85
+ this ( name , boost , null )
86
+ {
87
+ }
88
+
89
+ public Field ( string name , string format ) :
90
+ this ( name , null , format )
91
+ {
92
+ }
93
+
94
94
public Field ( Expression expression , double ? boost = null , string ? format = null )
95
95
{
96
96
Expression = expression ?? throw new ArgumentNullException ( nameof ( expression ) ) ;
@@ -117,21 +117,21 @@ public Field(PropertyInfo property, double? boost = null, string? format = null)
117
117
118
118
#region Factory Methods
119
119
120
- public static Field ? FromString ( string ? name ) => string . IsNullOrEmpty ( name ) ? null : new Field ( name ) ;
120
+ public static Field FromString ( string name ) => new ( name ) ;
121
121
122
- public static Field ? FromExpression ( Expression ? expression ) => expression is null ? null : new Field ( expression ) ;
122
+ public static Field FromExpression ( Expression expression ) => new ( expression ) ;
123
123
124
- public static Field ? FromProperty ( PropertyInfo ? property ) => property is null ? null : new Field ( property ) ;
124
+ public static Field FromProperty ( PropertyInfo property ) => new ( property ) ;
125
125
126
126
#endregion Factory Methods
127
127
128
128
#region Conversion Operators
129
129
130
- public static implicit operator Field ? ( string ? name ) => FromString ( name ) ;
130
+ public static implicit operator Field ( string name ) => FromString ( name ) ;
131
131
132
- public static implicit operator Field ? ( Expression ? expression ) => FromExpression ( expression ) ;
132
+ public static implicit operator Field ( Expression expression ) => FromExpression ( expression ) ;
133
133
134
- public static implicit operator Field ? ( PropertyInfo ? property ) => FromProperty ( property ) ;
134
+ public static implicit operator Field ( PropertyInfo property ) => FromProperty ( property ) ;
135
135
136
136
#endregion Conversion Operators
137
137
@@ -203,15 +203,7 @@ public override bool Equals(object? obj) =>
203
203
_ => false
204
204
} ;
205
205
206
- public override int GetHashCode ( )
207
- {
208
- unchecked
209
- {
210
- var hashCode = _comparisonValue ? . GetHashCode ( ) ?? 0 ;
211
- hashCode = ( hashCode * 397 ) ^ ( _type ? . GetHashCode ( ) ?? 0 ) ;
212
- return hashCode ;
213
- }
214
- }
206
+ public override int GetHashCode ( ) => HashCode . Combine ( _comparisonValue , _type ) ;
215
207
216
208
#endregion Equality
217
209
@@ -243,20 +235,29 @@ public override string ToString() =>
243
235
244
236
#endregion Debugging
245
237
246
- [ return : NotNullIfNotNull ( nameof ( name ) ) ]
247
- private static string ? ParseFieldName ( string ? name , out double ? boost )
238
+ private static string ParseFieldName ( string name , out double ? boost )
248
239
{
249
240
boost = null ;
250
- if ( name is null )
251
- return null ;
252
241
253
- var caretIndex = name . IndexOf ( '^' ) ;
254
- if ( caretIndex == - 1 )
242
+ #if NET || NETSTANDARD2_1_OR_GREATER
243
+ if ( ! name . Contains ( '^' , StringComparison . Ordinal ) )
255
244
return name ;
245
+ #else
246
+ if ( name . IndexOf ( '^' ) == - 1 )
247
+ return name ;
248
+ #endif
256
249
257
- var parts = name . Split ( new [ ] { '^' } , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
250
+ #if NET || NETSTANDARD2_1_OR_GREATER
251
+ var parts = name . Split ( '^' , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
252
+ #else
253
+ var parts = name . Split ( BoostSplit , 2 , StringSplitOptions . RemoveEmptyEntries ) ;
254
+ #endif
258
255
name = parts [ 0 ] ;
259
256
boost = double . Parse ( parts [ 1 ] , CultureInfo . InvariantCulture ) ;
257
+
258
+ if ( string . IsNullOrWhiteSpace ( name ) )
259
+ throw new ArgumentException ( "Field name can not be null or empty." , nameof ( name ) ) ;
260
+
260
261
return name ;
261
262
}
262
263
}
0 commit comments