forked from splunk/splunk-sdk-csharp-pcl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceName.cs
484 lines (442 loc) · 15.6 KB
/
ResourceName.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
* Copyright 2014 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
//// TODO:
//// [O] Contracts
//// [O] Documentation
namespace Splunk.Client
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
/// <summary>
/// Represents a Splunk resource name.
/// </summary>
/// <seealso cref="T:System.Collections.ObjectModel.ReadOnlyCollection{T}"/>
/// <seealso cref="T:System.IComparable"/>
/// <seealso cref="T:System.IComparable{T}"/>
/// <seealso cref="T:System.IEquatable{T}"/>
public sealed class ResourceName : ReadOnlyCollection<string>, IComparable, IComparable<ResourceName>,
IEquatable<ResourceName>
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ResourceName"/> class.
/// </summary>
/// <param name="resourceName">
/// Another resource name.
/// </param>
/// <param name="parts">
/// Names to be appended <paramref name="resourceName"/>.
/// </param>
public ResourceName(ResourceName resourceName, params string[] parts)
: this(resourceName.Concat(parts))
{
Contract.Requires<ArgumentNullException>(resourceName != null);
Contract.Requires<ArgumentNullException>(parts != null);
}
/// <summary>
/// Intializes a new instance of the <see cref="ResourceName"/> class.
/// </summary>
/// <param name="parts">
///
/// </param>
public ResourceName(params string[] parts)
: base(parts)
{
Contract.Requires<ArgumentNullException>(parts != null);
foreach (var part in this)
{
if (string.IsNullOrEmpty(part))
{
throw new ArgumentException(string.Concat("parts: ", this.ToString()));
}
}
}
/// <summary>
/// Intializes a new instance of the <see cref="ResourceName"/> class.
/// </summary>
/// <exception cref="ArgumentException">
/// Thrown when one or more arguments have unsupported or illegal values.
/// </exception>
/// <param name="parts">
///
/// </param>
public ResourceName(IEnumerable<string> parts)
: this(parts.ToArray())
{ }
#endregion
#region Properties
/// <summary>
/// Gets the collection.
/// </summary>
/// <value>
/// The collection.
/// </value>
public string Collection => this.Items.Count > 1 ? this.Items[this.Items.Count - 2] : null;
/// <summary>
/// Gets the title.
/// </summary>
/// <value>
/// The title.
/// </value>
public string Title => this.Items[this.Items.Count - 1];
#endregion
#region Methods
/// <summary>
/// Compares the current <see cref="ResourceName"/> with another object and
/// returns an integer that indicates whether the current
/// <see cref="ResourceName"/> precedes, follows, or appears in the same
/// position in the sort order as the other object.
/// </summary>
/// <param name="other">
/// The object to compare to the current <see cref="ResourceName"/>.
/// </param>
/// <returns>
/// A 32-bit signed integer that indicates whether this instance precedes,
/// follows, or appears in the same position in the sort order as
/// <paramref name="other"/>.
/// <list type="table">
/// <listheader>
/// <term>
/// Value
/// </term>
/// <description>
/// Condition
/// </description>
/// </listheader>
/// <item>
/// <term>
/// Less than zero
/// </term>
/// <description>
/// This instance precedes <paramref name="other"/>.
/// </description>
/// </item>
/// <item>
/// <term>
/// Zero
/// </term>
/// <description>
/// This instance is in the same position in the sort order as
/// <paramref name="other"/>.
/// </description>
/// </item>
/// <item>
/// <term>
/// Greater than zero
/// </term>
/// <description>
/// This instance follows <paramref name="other"/>,
/// <paramref name= "other"/> is not a <see cref="ResourceName"/>, or
/// <paramref name="other"/> is <c>null</c>.
/// </description>
/// </item>
/// </list>
/// </returns>
/// <seealso cref="M:System.IComparable.CompareTo(object)"/>
public int CompareTo(object other)
{
return this.CompareTo(other as ResourceName);
}
/// <summary>
/// Compares the current <see cref="ResourceName"/> with another one and
/// returns an integer that indicates whether the current
/// <see cref= "ResourceName"/> precedes, follows, or appears in the same
/// position in the sort order as the other one.
/// </summary>
/// <param name="other">
/// The object to compare with the current <see cref="ResourceName"/>.
/// </param>
/// <returns>
/// A 32-bit signed integer that indicates whether this instance precedes,
/// follows, or appears in the same position in the sort order as
/// <paramref name="other"/>.
/// <list type="table">
/// <listheader>
/// <term>
/// Value
/// </term>
/// <description>
/// Condition
/// </description>
/// </listheader>
/// <item>
/// <term>
/// Less than zero
/// </term>
/// <description>
/// This instance precedes <paramref name="other"/>.
/// </description>
/// </item>
/// <item>
/// <term>
/// Zero
/// </term>
/// <description>
/// This instance is in the same position in the sort order as
/// <paramref name="other"/>.
/// </description>
/// </item>
/// <item>
/// <term>
/// Greater than zero
/// </term>
/// <description>
/// This instance follows <paramref name="other"/> or
/// <paramref name="other"/> is <c>null</c>.
/// </description>
/// </item>
/// </list>
/// </returns>
public int CompareTo(ResourceName other)
{
if (other == null)
{
return 1;
}
if (object.ReferenceEquals(this, other))
{
return 0;
}
int diff = this.Items.Count - other.Items.Count;
if (diff != 0)
{
return diff;
}
var pair = this.Items
.Zip(other.Items, (p1, p2) => new { ThisPart = p1, OtherPart = p2 })
.FirstOrDefault(p => p.ThisPart != p.OtherPart);
if (pair == null)
{
return 0;
}
return string.Compare(pair.ThisPart, pair.OtherPart, StringComparison.Ordinal);
}
/// <summary>
/// Determines whether the current <see cref="ResourceName"/> and another
/// object are equal.
/// </summary>
/// <param name="other">
/// The object to compare with the current <see cref="ResourceName"/>.
/// </param>
/// <returns>
/// <c>true</c> if <paramref name="other"/> is a non <c>null</c>
/// <see cref="ResourceName"/> and is the same as the current
/// <see cref= "ResourceName"/>; otherwise, <c>false</c>.
/// </returns>
/// <seealso cref="M:System.Object.Equals(object)"/>
public override bool Equals(object other)
{
return this.Equals(other as ResourceName);
}
/// <summary>
/// Determines whether the current <see cref="ResourceName"/> and another one
/// are equal.
/// </summary>
/// <param name="other">
/// The object to compare with the current <see cref="ResourceName"/>.
/// </param>
/// <returns>
/// <c>true</c> if <paramref name="other"/> is non <c>null</c> and is the
/// same as the current <see cref="ResourceName"/>; otherwise,
/// <c>false</c>.
/// </returns>
public bool Equals(ResourceName other)
{
if ((object)other == null)
{
return false;
}
if (object.ReferenceEquals(this, other))
{
return true;
}
if (this.Items.Count != other.Items.Count)
{
return false;
}
return this.Items.SequenceEqual(other.Items);
}
/// <summary>
/// Computes the hash code for the current <see cref="ResourceName"/>.
/// </summary>
/// <returns>
/// The hash code for the current <see cref="ResourceName"/>.
/// </returns>
/// <seealso cref="M:System.Object.GetHashCode()"/>
public override int GetHashCode()
{
// TODO: Check this against the algorithm presented in Effective Java
return this.Items.Aggregate(seed: 17, func: (value, part) => (value * 23) + part.GetHashCode());
}
/// <summary>
/// Greater-than comparison operator.
/// </summary>
/// <param name="a">
/// The first <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <param name="b">
/// The second <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <returns>
/// The result of the operation.
/// </returns>
public static bool operator >(ResourceName a, ResourceName b)
{
if ((object)a == null)
{
return false;
}
return a.CompareTo(b) > 0;
}
/// <summary>
/// Greater-than-or-equal comparison operator.
/// </summary>
/// <param name="a">
/// The first <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <param name="b">
/// The second <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <returns>
/// The result of the operation.
/// </returns>
public static bool operator >=(ResourceName a, ResourceName b)
{
if ((object)a == null)
{
return (object)b == null;
}
return a.CompareTo(b) < 0;
}
/// <summary>
/// Determines whether two <see cref="ResourceName"/> instances have the same
/// value.
/// </summary>
/// <param name="a">
/// The first <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <param name="b">
/// The second <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <returns>
/// <c>true</c> if the value of <paramref name="a"/> is the same as the value
/// of <paramref name="b"/>; otherwise, <c>false</c>.
/// </returns>
public static bool operator ==(ResourceName a, ResourceName b)
{
if (object.ReferenceEquals(a, b))
{
return true;
}
if ((object)a == null || (object)b == null)
{
return false;
}
return a.Equals(b);
}
/// <summary>
/// Determines whether two <see cref="ResourceName"/> instances have
/// different values.
/// </summary>
/// <param name="a">
/// The first <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <param name="b">
/// The second <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <returns>
/// <c>true</c> if the value of <paramref name="a"/> is different than the
/// value of <paramref name="b"/>; otherwise, <c>false</c>.
/// </returns>
public static bool operator !=(ResourceName a, ResourceName b)
{
return !(a == b);
}
/// <summary>
/// Less-than comparison operator.
/// </summary>
/// <param name="a">
/// The first <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <param name="b">
/// The second <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <returns>
/// The result of the operation.
/// </returns>
public static bool operator <(ResourceName a, ResourceName b)
{
if ((object)a == null)
{
return (object)b != null;
}
return a.CompareTo(b) < 0;
}
/// <summary>
/// Less-than-or-equal comparison operator.
/// </summary>
/// <param name="a">
/// The first <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <param name="b">
/// The second <see cref="ResourceName"/> to compare or <c>null</c>.
/// </param>
/// <returns>
/// The result of the operation.
/// </returns>
public static bool operator <=(ResourceName a, ResourceName b)
{
if ((object)a == null)
{
return true;
}
return a.CompareTo(b) < 0;
}
/// <summary>
/// Converts the value of the current <see cref="Namespace"/> to its
/// equivalent string representation.
/// </summary>
/// <returns>
/// A string representation of the current <see cref="Namespace"/>
/// </returns>
/// <seealso cref="M:System.Object.ToString()"/>
public override string ToString()
{
return string.Join("/", from segment in this select segment);
}
/// <summary>
/// Converts the value of the current <see cref="Namespace"/> object to its
/// equivalent URI encoded string representation.
/// </summary>
/// <remarks>
/// The value is converted using <see cref="Uri.EscapeUriString"/>.
/// </remarks>
/// <returns>
/// A string representation of the current <see cref="Namespace"/>
/// </returns>
[SuppressMessage("Microsoft.Design", "CA1055:UriReturnValuesShouldNotBeStrings", Justification =
"This is by design")
]
public string ToUriString()
{
return string.Join("/", from segment in this select Uri.EscapeDataString(segment));
}
#endregion
}
}