-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMessagingEntityPath.cs
More file actions
974 lines (877 loc) · 40.1 KB
/
MessagingEntityPath.cs
File metadata and controls
974 lines (877 loc) · 40.1 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.Core.Extensions;
using RapidField.SolidInstruments.TextEncoding;
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
namespace RapidField.SolidInstruments.Messaging
{
/// <summary>
/// Represents a textual path that defines a route to a messaging entity.
/// </summary>
/// <remarks>
/// <see cref="MessagingEntityPath" /> is the default implementation of <see cref="IMessagingEntityPath" />.
/// </remarks>
[DataContract]
public sealed class MessagingEntityPath : IMessagingEntityPath
{
/// <summary>
/// Initializes a new instance of the <see cref="MessagingEntityPath" /> class.
/// </summary>
public MessagingEntityPath()
{
LabelOneValue = null;
LabelTwoValue = null;
LabelThreeValue = null;
MessageTypeValue = null;
PrefixValue = null;
}
/// <summary>
/// Initializes a new instance of the <see cref="MessagingEntityPath" /> class.
/// </summary>
/// <param name="messageType">
/// The message type.
/// </param>
/// <param name="prefix">
/// The prefix token, or <see langword="null" /> if there is not a prefix.
/// </param>
/// <param name="labels">
/// A collection of labels, or an empty collection if there are no labels.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="labels" /> contains more than three elements -or- an exception was raised while evaluating the data
/// contract information for <paramref name="messageType" />.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="messageType" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// One or more of the specified values is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// One or more of the specified values contains invalid characters.
/// </exception>
[DebuggerHidden]
internal MessagingEntityPath(Type messageType, String prefix, params String[] labels)
: this(ExtractMessageTypeName(messageType), prefix, ExtractLabel(labels, 0), ExtractLabel(labels, 1), ExtractLabel(labels, 2))
{
if (labels.IsNullOrEmpty())
{
return;
}
else if (labels.Length > 3)
{
throw new ArgumentException("Messaging entity paths may contain a maximum of three labels.");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="MessagingEntityPath" /> class.
/// </summary>
/// <param name="messageType">
/// The message type token.
/// </param>
/// <param name="prefix">
/// The prefix token, or <see langword="null" /> if there is not a prefix.
/// </param>
/// <param name="labelOne">
/// The first label token, or <see langword="null" /> if there is not a first label.
/// </param>
/// <param name="labelTwo">
/// The second label token, or <see langword="null" /> if there is not a second label.
/// </param>
/// <param name="labelThree">
/// The third label token, or <see langword="null" /> if there is not a third label.
/// </param>
/// <exception cref="ArgumentEmptyException">
/// <paramref name="messageType" /> is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="messageType" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// One or more of the specified values is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// One or more of the specified values contains invalid characters.
/// </exception>
[DebuggerHidden]
private MessagingEntityPath(String messageType, String prefix, String labelOne, String labelTwo, String labelThree)
: this()
{
MessageType = messageType;
if (prefix.IsNullOrEmpty() == false)
{
Prefix = prefix;
}
if (labelOne.IsNullOrEmpty() == false)
{
LabelOne = labelOne;
}
if (labelTwo.IsNullOrEmpty() == false)
{
LabelTwo = labelTwo;
}
if (labelThree.IsNullOrEmpty() == false)
{
LabelThree = labelThree;
}
}
/// <summary>
/// Determines whether or not two specified <see cref="IMessagingEntityPath" /> instances are not equal.
/// </summary>
/// <param name="a">
/// The first <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <returns>
/// A value indicating whether or not the specified instances are not equal.
/// </returns>
public static Boolean operator !=(MessagingEntityPath a, IMessagingEntityPath b) => (a == b) == false;
/// <summary>
/// Determines whether or not a specified <see cref="IMessagingEntityPath" /> instance is less than another specified
/// instance.
/// </summary>
/// <param name="a">
/// The first <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <returns>
/// <see langword="true" /> if the second object is earlier than the first object, otherwise <see langword="false" />.
/// </returns>
public static Boolean operator <(MessagingEntityPath a, IMessagingEntityPath b) => a.CompareTo(b) == -1;
/// <summary>
/// Determines whether or not a specified <see cref="IMessagingEntityPath" /> instance is less than or equal to another
/// supplied instance.
/// </summary>
/// <param name="a">
/// The first <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <returns>
/// <see langword="true" /> if the second object is earlier than or equal to the first object, otherwise
/// <see langword="false" />.
/// </returns>
public static Boolean operator <=(MessagingEntityPath a, IMessagingEntityPath b) => a.CompareTo(b) < 1;
/// <summary>
/// Determines whether or not two specified <see cref="IMessagingEntityPath" /> instances are equal.
/// </summary>
/// <param name="a">
/// The first <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <returns>
/// A value indicating whether or not the specified instances are equal.
/// </returns>
public static Boolean operator ==(MessagingEntityPath a, IMessagingEntityPath b)
{
if ((Object)a is null && (Object)b is null)
{
return true;
}
else if ((Object)a is null || (Object)b is null)
{
return false;
}
return a.Equals(b);
}
/// <summary>
/// Determines whether or not a specified <see cref="IMessagingEntityPath" /> instance is greater than another specified
/// instance.
/// </summary>
/// <param name="a">
/// The first <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <returns>
/// <see langword="true" /> if the second object is later than the first object, otherwise <see langword="false" />.
/// </returns>
public static Boolean operator >(MessagingEntityPath a, IMessagingEntityPath b) => a.CompareTo(b) == 1;
/// <summary>
/// Determines whether or not a specified <see cref="IMessagingEntityPath" /> instance is greater than or equal to another
/// supplied instance.
/// </summary>
/// <param name="a">
/// The first <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <param name="b">
/// The second <see cref="IMessagingEntityPath" /> instance to compare.
/// </param>
/// <returns>
/// <see langword="true" /> if the second object is later than or equal to the first object, otherwise
/// <see langword="false" />.
/// </returns>
public static Boolean operator >=(MessagingEntityPath a, IMessagingEntityPath b) => a.CompareTo(b) > -1;
/// <summary>
/// Converts the specified <see cref="String" /> representation of a messaging entity path to its
/// <see cref="MessagingEntityPath" /> equivalent.
/// </summary>
/// <param name="input">
/// A <see cref="String" /> containing a messaging entity path to convert.
/// </param>
/// <returns>
/// A <see cref="MessagingEntityPath" /> that is equivalent to <paramref name="input" />.
/// </returns>
/// <exception cref="ArgumentEmptyException">
/// <paramref name="input" /> is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="input" /> is <see langword="null" />.
/// </exception>
/// <exception cref="FormatException">
/// <paramref name="input" /> does not contain a valid representation of a messaging entity path.
/// </exception>
public static MessagingEntityPath Parse(String input)
{
if (Parse(input, out var value, true))
{
return value;
}
return default;
}
/// <summary>
/// Converts the specified <see cref="String" /> representation of a messaging entity path to its
/// <see cref="MessagingEntityPath" /> equivalent. The method returns a value that indicates whether the conversion
/// succeeded.
/// </summary>
/// <param name="input">
/// A <see cref="String" /> containing a messaging entity path to convert.
/// </param>
/// <param name="result">
/// The parsed result if the operation is successful, otherwise the default instance.
/// </param>
/// <returns>
/// <see langword="true" /> if the parse operation is successful, otherwise <see langword="false" />.
/// </returns>
public static Boolean TryParse(String input, out MessagingEntityPath result)
{
if (Parse(input, out var value, false))
{
result = value;
return true;
}
result = default;
return false;
}
/// <summary>
/// Compares the current <see cref="MessagingEntityPath" /> to the specified object and returns an indication of their
/// relative values.
/// </summary>
/// <param name="other">
/// The <see cref="IMessagingEntityPath" /> to compare to this instance.
/// </param>
/// <returns>
/// Negative one if this instance is earlier than the specified instance; one if this instance is later than the supplied
/// instance; zero if they are equal.
/// </returns>
public Int32 CompareTo(IMessagingEntityPath other) => ToString().CompareTo(other?.ToString() ?? String.Empty);
/// <summary>
/// Determines whether or not two specified <see cref="IMessagingEntityPath" /> instances are equal.
/// </summary>
/// <param name="other">
/// The <see cref="IMessagingEntityPath" /> to compare to this instance.
/// </param>
/// <returns>
/// A value indicating whether or not the specified instances are equal.
/// </returns>
public Boolean Equals(IMessagingEntityPath other)
{
if ((Object)other is null)
{
return false;
}
else if (ToString() != other.ToString())
{
return false;
}
return true;
}
/// <summary>
/// Determines whether or not the current <see cref="MessagingEntityPath" /> is equal to the specified
/// <see cref="Object" />.
/// </summary>
/// <param name="obj">
/// The <see cref="Object" /> to compare to this instance.
/// </param>
/// <returns>
/// A value indicating whether or not the specified instances are equal.
/// </returns>
public override Boolean Equals(Object obj)
{
if (obj is null)
{
return false;
}
else if (obj is IMessagingEntityPath path)
{
return Equals(path);
}
return false;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer hash code.
/// </returns>
public override Int32 GetHashCode()
{
var hashCode = 433494437 ^ MessageType.GetHashCode();
if (Prefix.IsNullOrEmpty() == false)
{
hashCode ^= Prefix.GetHashCode();
}
if (LabelOne.IsNullOrEmpty() == false)
{
hashCode ^= LabelOne.GetHashCode();
}
if (LabelTwo.IsNullOrEmpty() == false)
{
hashCode ^= LabelTwo.GetHashCode();
}
if (LabelThree.IsNullOrEmpty() == false)
{
hashCode ^= LabelThree.GetHashCode();
}
return hashCode;
}
/// <summary>
/// Converts the value of the current <see cref="MessagingEntityPath" /> to its equivalent string representation.
/// </summary>
/// <returns>
/// A string representation of the current <see cref="MessagingEntityPath" />.
/// </returns>
public override String ToString()
{
var stringBuilder = new StringBuilder();
if (Prefix.IsNullOrEmpty() == false)
{
stringBuilder.Append($"{Prefix}{DelimitingCharacterForPrefix}");
}
if (MessageType.IsNullOrEmpty() == false)
{
stringBuilder.Append(MessageType);
}
if (LabelOne.IsNullOrEmpty() == false)
{
stringBuilder.Append($"{DelimitingCharacterForLabelToken}{LabelOne}");
}
if (LabelTwo.IsNullOrEmpty() == false)
{
stringBuilder.Append($"{DelimitingCharacterForLabelToken}{LabelTwo}");
}
if (LabelThree.IsNullOrEmpty() == false)
{
stringBuilder.Append($"{DelimitingCharacterForLabelToken}{LabelThree}");
}
return stringBuilder.ToString();
}
/// <summary>
/// Attempts to extract a label at the specified index from the specified collection of labels.
/// </summary>
/// <param name="labels">
/// A collection of labels, or an empty collection if there are no labels.
/// </param>
/// <param name="index">
/// The zero-based index of the label to extract.
/// </param>
/// <returns>
/// The extracted label, or <see langword="null" /> if no label was extracted.
/// </returns>
[DebuggerHidden]
private static String ExtractLabel(String[] labels, Int32 index)
{
if (labels.IsNullOrEmpty())
{
return null;
}
else if (index >= labels.Length)
{
return null;
}
var label = labels[index].Trim();
return label.IsNullOrEmpty() ? null : label;
}
/// <summary>
/// Determines an appropriate message type name for the specified message type.
/// </summary>
/// <param name="messageType">
/// The message type.
/// </param>
/// <returns>
/// The resulting message type name.
/// </returns>
/// <exception cref="ArgumentException">
/// An exception was raised while evaluating the data contract information for <paramref name="messageType" />.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="messageType" /> is <see langword="null" />.
/// </exception>
[DebuggerHidden]
private static String ExtractMessageTypeName(Type messageType)
{
try
{
if (messageType.RejectIf().IsNull(nameof(messageType)).TargetArgument.GetCustomAttributes(typeof(DataContractAttribute), false).FirstOrDefault() is DataContractAttribute dataContractAttribute && dataContractAttribute.Name.IsNullOrEmpty() == false)
{
return dataContractAttribute.Name.Compress();
}
}
catch (ArgumentNullException)
{
throw;
}
catch (Exception exception)
{
throw new ArgumentException($"An exception was raised while evaluating the data contract information for {messageType.FullName}. See inner exception.", nameof(messageType), exception);
}
return messageType.Name;
}
/// <summary>
/// Converts the specified <see cref="String" /> representation of a messaging entity path to its
/// <see cref="MessagingEntityPath" /> equivalent.
/// </summary>
/// <param name="input">
/// A <see cref="String" /> containing a messaging entity path to convert.
/// </param>
/// <param name="result">
/// The resulting <see cref="MessagingEntityPath" /> value, or <see langword="null" /> if the operation is unsuccessful.
/// </param>
/// <param name="raiseExceptionOnFail">
/// A value indicating whether or not an exception should be raised if the parse operation fails.
/// </param>
/// <returns>
/// <see langword="true" /> if the parse operation is successful, otherwise <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentEmptyException">
/// <paramref name="input" /> is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="input" /> is <see langword="null" />.
/// </exception>
/// <exception cref="FormatException">
/// <paramref name="input" /> does not contain a valid representation of a messaging entity path.
/// </exception>
[DebuggerHidden]
private static Boolean Parse(String input, out MessagingEntityPath result, Boolean raiseExceptionOnFail)
{
if (raiseExceptionOnFail)
{
if (input is null)
{
throw new ArgumentNullException(nameof(input));
}
else if (input.Length == 0)
{
throw new ArgumentEmptyException(nameof(input));
}
}
else if (input.IsNullOrEmpty())
{
result = default;
return false;
}
try
{
var processedString = input.Solidify();
if (processedString.Length == 0)
{
if (raiseExceptionOnFail)
{
throw new FormatException(ParseFormatExceptionMessage, new ArgumentException("The input string does not contain any path information.", nameof(input)));
}
result = default;
return false;
}
var regularExpression = new Regex(RegularExpressionPatternForCompletePath);
var matchGroups = regularExpression.IsMatch(processedString) ? regularExpression.Match(processedString).Groups : null;
if (matchGroups.IsNullOrEmpty())
{
if (raiseExceptionOnFail)
{
throw new FormatException(ParseFormatExceptionMessage, new ArgumentException("The input string is invalid.", nameof(input)));
}
result = default;
return false;
}
var messageTypeString = matchGroups.Where(group => group.Success && group.Name == PatternGroupNameForMessageTypeToken).SingleOrDefault()?.Value;
if (messageTypeString.IsNullOrEmpty())
{
if (raiseExceptionOnFail)
{
throw new FormatException(ParseFormatExceptionMessage, new ArgumentException("The message type is invalid.", nameof(input)));
}
result = default;
return false;
}
var prefixString = matchGroups.Where(group => group.Success && group.Name == PatternGroupNameForPrefixToken).SingleOrDefault()?.Value;
var labelOneString = matchGroups.Where(group => group.Success && group.Name == PatternGroupNameForLabelTokenOne).SingleOrDefault()?.Value;
var labelTwoString = matchGroups.Where(group => group.Success && group.Name == PatternGroupNameForLabelTokenTwo).SingleOrDefault()?.Value;
var labelThreeString = matchGroups.Where(group => group.Success && group.Name == PatternGroupNameForLabelTokenThree).SingleOrDefault()?.Value;
result = new MessagingEntityPath(messageTypeString, prefixString, labelOneString, labelTwoString, labelThreeString);
return true;
}
catch (Exception exception)
{
if (raiseExceptionOnFail)
{
throw new FormatException(ParseFormatExceptionMessage, exception);
}
result = default;
return false;
}
}
/// <summary>
/// Raises an <see cref="StringArgumentPatternException" /> if the specified label token is invalid.
/// </summary>
/// <param name="token">
/// The token to evaluate.
/// </param>
/// <returns>
/// The specified token.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="token" /> is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// <paramref name="token" /> is invalid.
/// </exception>
[DebuggerHidden]
private static String ValidateLabelToken(String token) => ValidateToken(token?.RejectIf().LengthIsGreaterThan(MaximumCharacterLengthForLabelToken, nameof(token)), RegularExpressionPatternForLabelToken);
/// <summary>
/// Raises an <see cref="StringArgumentPatternException" /> if the specified message type token is invalid.
/// </summary>
/// <param name="token">
/// The token to evaluate.
/// </param>
/// <returns>
/// The specified token.
/// </returns>
/// <exception cref="ArgumentEmptyException">
/// <paramref name="token" /> is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="token" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="token" /> is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// <paramref name="token" /> is invalid.
/// </exception>
[DebuggerHidden]
private static String ValidateMessageTypeToken(String token) => ValidateToken(token.RejectIf().IsNullOrEmpty(nameof(token)).OrIf().LengthIsGreaterThan(MaximumCharacterLengthForMessageTypeToken, nameof(token)), RegularExpressionPatternForMessageTypeToken);
/// <summary>
/// Raises an <see cref="StringArgumentPatternException" /> if the specified prefix token is invalid.
/// </summary>
/// <param name="token">
/// The token to evaluate.
/// </param>
/// <returns>
/// The specified token.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="token" /> is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// <paramref name="token" /> is invalid.
/// </exception>
[DebuggerHidden]
private static String ValidatePrefixToken(String token) => ValidateToken(token?.RejectIf().LengthIsGreaterThan(MaximumCharacterLengthForPrefixToken, nameof(token)), RegularExpressionPatternForPrefixToken);
/// <summary>
/// Raises an <see cref="StringArgumentPatternException" /> if the specified token is invalid.
/// </summary>
/// <param name="token">
/// The token to evaluate.
/// </param>
/// <param name="pattern">
/// The token pattern against which to compare <paramref name="token" />.
/// </param>
/// <returns>
/// The specified token.
/// </returns>
/// <exception cref="StringArgumentPatternException">
/// <paramref name="token" /> is invalid.
/// </exception>
[DebuggerHidden]
private static String ValidateToken(String token, String pattern) => token.IsNullOrEmpty() ? null : token.RejectIf().DoesNotMatchRegularExpression(pattern, nameof(token));
/// <summary>
/// Gets or sets the first label for the current <see cref="MessagingEntityPath" />, or <see langword="null" /> if there is
/// not a first label.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// The specified value is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// The specified value contains invalid characters.
/// </exception>
[DataMember]
public String LabelOne
{
get => LabelOneValue;
set => LabelOneValue = ValidateLabelToken(value);
}
/// <summary>
/// Gets or sets the third label for the current <see cref="MessagingEntityPath" />, or <see langword="null" /> if there is
/// not a third label.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// The specified value is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// The specified value contains invalid characters.
/// </exception>
[DataMember]
public String LabelThree
{
get => LabelThreeValue;
set => LabelThreeValue = ValidateLabelToken(value);
}
/// <summary>
/// Gets or sets the second label for the current <see cref="MessagingEntityPath" />, or <see langword="null" /> if there is
/// not a second label.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// The specified value is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// The specified value contains invalid characters.
/// </exception>
[DataMember]
public String LabelTwo
{
get => LabelTwoValue;
set => LabelTwoValue = ValidateLabelToken(value);
}
/// <summary>
/// Gets or sets the message type for the current <see cref="MessagingEntityPath" />.
/// </summary>
/// <exception cref="ArgumentEmptyException">
/// The specified value is empty.
/// </exception>
/// <exception cref="ArgumentNullException">
/// The specified value is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// The specified value is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// The specified value contains invalid characters.
/// </exception>
[DataMember]
public String MessageType
{
get
{
if (MessageTypeValue.IsNullOrEmpty())
{
MessageTypeValue = EnhancedReadabilityGuid.New().ToString();
}
return MessageTypeValue;
}
set => MessageTypeValue = ValidateMessageTypeToken(value);
}
/// <summary>
/// Gets or sets the prefix for the current <see cref="MessagingEntityPath" />, or <see langword="null" /> if there is not a
/// prefix.
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// The specified value is too long.
/// </exception>
/// <exception cref="StringArgumentPatternException">
/// The specified value contains invalid characters.
/// </exception>
[DataMember]
public String Prefix
{
get => PrefixValue;
set => PrefixValue = ValidatePrefixToken(value);
}
/// <summary>
/// Gets a regular expression that is used to validate the complete path.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForCompletePath => $"^{RegularExpressionPatternForPrefix}?{RegularExpressionPatternForMessageType}{RegularExpressionPatternForLabelOne}?{RegularExpressionPatternForLabelTwo}?{RegularExpressionPatternForLabelThree}?$";
/// <summary>
/// Gets a regular expression that is used to validate the first label.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForLabelOne => $"(?<{PatternGroupNameForLabelOne}>(?<{PatternGroupNameForLabelDelimiterOne}>[{DelimitingCharacterForLabelToken}])(?<{PatternGroupNameForLabelTokenOne}>{RegularExpressionPatternForLabelToken}))";
/// <summary>
/// Gets a regular expression that is used to validate the third label.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForLabelThree => $"(?<{PatternGroupNameForLabelThree}>(?<{PatternGroupNameForLabelDelimiterThree}>[{DelimitingCharacterForLabelToken}])(?<{PatternGroupNameForLabelTokenThree}>{RegularExpressionPatternForLabelToken}))";
/// <summary>
/// Gets a regular expression that is used to validate label tokens.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForLabelToken => $"[a-zA-Z0-9{DelimitingCharacterForLabelTokenSubParts}]{{1,{MaximumCharacterLengthForLabelToken}}}";
/// <summary>
/// Gets a regular expression that is used to validate the second label.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForLabelTwo => $"(?<{PatternGroupNameForLabelTwo}>(?<{PatternGroupNameForLabelDelimiterTwo}>[{DelimitingCharacterForLabelToken}])(?<{PatternGroupNameForLabelTokenTwo}>{RegularExpressionPatternForLabelToken}))";
/// <summary>
/// Gets a regular expression that is used to validate the full message type.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForMessageType => $"(?<{PatternGroupNameForMessageTypeToken}>{RegularExpressionPatternForMessageTypeToken})";
/// <summary>
/// Gets a regular expression that is used to validate the message type token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForMessageTypeToken => $"[a-zA-Z0-9]{{1,{MaximumCharacterLengthForMessageTypeToken}}}";
/// <summary>
/// Gets a regular expression that is used to validate the full prefix.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForPrefix => $"(?<{PatternGroupNameForPrefix}>(?<{PatternGroupNameForPrefixToken}>{RegularExpressionPatternForPrefixToken})(?<{PatternGroupNameForPrefixDelimiter}>[{DelimitingCharacterForPrefix}]))";
/// <summary>
/// Gets a regular expression that is used to validate the prefix token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static String RegularExpressionPatternForPrefixToken => $"[a-zA-Z0-9]{{1,{MaximumCharacterLengthForPrefixToken}}}";
/// <summary>
/// Represents the maximum number of allowed characters for a label token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public const Int32 MaximumCharacterLengthForLabelToken = 34;
/// <summary>
/// Represents the maximum number of allowed characters for a message type token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public const Int32 MaximumCharacterLengthForMessageTypeToken = 89;
/// <summary>
/// Represents the maximum number of allowed characters for a prefix token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public const Int32 MaximumCharacterLengthForPrefixToken = 21;
/// <summary>
/// Represents the delimiting character that precedes label tokens.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal const Char DelimitingCharacterForLabelToken = '_';
/// <summary>
/// Represents the delimiting character that is permitted for label token sub-parts.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal const Char DelimitingCharacterForLabelTokenSubParts = '+';
/// <summary>
/// Represents the delimiting character that follows the prefix token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal const Char DelimitingCharacterForPrefix = '-';
/// <summary>
/// Represents a message for format exceptions raised by <see cref="Parse(String)" />
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String ParseFormatExceptionMessage = "The specified string could not be parsed as a messaging entity path. See the inner exception for details.";
/// <summary>
/// Represents the group name for the first label delimiter.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelDelimiterOne = "labeldelimiterone";
/// <summary>
/// Represents the group name for the third label delimiter.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelDelimiterThree = "labeldelimiterthree";
/// <summary>
/// Represents the group name for the second label delimiter.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelDelimiterTwo = "labeldelimitertwo";
/// <summary>
/// Represents the group name for the first label.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelOne = "labelone";
/// <summary>
/// Represents the group name for the third label.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelThree = "labelthree";
/// <summary>
/// Represents the group name for the first label token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelTokenOne = "labeltokenone";
/// <summary>
/// Represents the group name for the third label token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelTokenThree = "labeltokenthree";
/// <summary>
/// Represents the group name for the second label token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelTokenTwo = "labeltokentwo";
/// <summary>
/// Represents the group name for the second label.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForLabelTwo = "labeltwo";
/// <summary>
/// Represents the group name for the message type token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForMessageTypeToken = "messagetypetoken";
/// <summary>
/// Represents the group name for the full prefix.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForPrefix = "prefix";
/// <summary>
/// Represents the group name for the prefix delimiter.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForPrefixDelimiter = "prefixdelimiter";
/// <summary>
/// Represents the group name for the prefix token.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private const String PatternGroupNameForPrefixToken = "prefixtoken";
/// <summary>
/// Represents the first label for the current <see cref="MessagingEntityPath" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private String LabelOneValue;
/// <summary>
/// Represents the third label for the current <see cref="MessagingEntityPath" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private String LabelThreeValue;
/// <summary>
/// Represents the second label for the current <see cref="MessagingEntityPath" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private String LabelTwoValue;
/// <summary>
/// Represents the message type for the current <see cref="MessagingEntityPath" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private String MessageTypeValue;
/// <summary>
/// Represents the prefix for the current <see cref="MessagingEntityPath" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private String PrefixValue;
}
}