| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using Microsoft.Azure.ServiceBus.Management; |
| | 8 | | using Microsoft.Azure.ServiceBus.Primitives; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Represents a description of a rule. |
| | 12 | | /// </summary> |
| | 13 | | public sealed class RuleDescription : IEquatable<RuleDescription> |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Gets the name of the default rule on the subscription. |
| | 17 | | /// </summary> |
| | 18 | | /// <remarks> |
| | 19 | | /// Whenever a new subscription is created, a default rule is always added. |
| | 20 | | /// The default rule is a <see cref="TrueFilter"/> which will enable all messages in the topic to reach subscrip |
| | 21 | | /// </remarks> |
| | 22 | | public const string DefaultRuleName = "$Default"; |
| | 23 | |
|
| | 24 | | Filter filter; |
| | 25 | | string name; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of the <see cref="RuleDescription" /> class with default values. |
| | 29 | | /// </summary> |
| | 30 | | public RuleDescription() |
| 0 | 31 | | : this(RuleDescription.DefaultRuleName, TrueFilter.Default) |
| | 32 | | { |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Initializes a new instance of the <see cref="RuleDescription" /> class with the specified name. |
| | 37 | | /// </summary> |
| | 38 | | public RuleDescription(string name) |
| 0 | 39 | | : this(name, TrueFilter.Default) |
| | 40 | | { |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Initializes a new instance of the <see cref="RuleDescription" /> class with the specified filter expression. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="filter">The filter expression used to match messages.</param> |
| | 47 | | [Obsolete("This constructor will be removed in next version, please use RuleDescription(string, Filter) instead. |
| 0 | 48 | | public RuleDescription(Filter filter) |
| | 49 | | { |
| 0 | 50 | | this.Filter = filter ?? throw Fx.Exception.ArgumentNull(nameof(filter)); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Initializes a new instance of the <see cref="RuleDescription" /> class with the specified name and filter ex |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="filter">The filter expression used to match messages.</param> |
| 0 | 57 | | public RuleDescription(string name, Filter filter) |
| | 58 | | { |
| 0 | 59 | | this.Filter = filter ?? throw Fx.Exception.ArgumentNull(nameof(filter)); |
| 0 | 60 | | this.Name = name; |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Gets or sets the filter expression used to match messages. |
| | 65 | | /// </summary> |
| | 66 | | /// <value>The filter expression used to match messages.</value> |
| | 67 | | /// <exception cref="System.ArgumentNullException">null (Nothing in Visual Basic) is assigned.</exception> |
| | 68 | | public Filter Filter |
| | 69 | | { |
| 0 | 70 | | get => this.filter; |
| | 71 | |
|
| 0 | 72 | | set => this.filter = value ?? throw Fx.Exception.ArgumentNull(nameof(this.Filter)); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Gets or sets the action to perform if the message satisfies the filtering expression. |
| | 77 | | /// </summary> |
| | 78 | | /// <value>The action to perform if the message satisfies the filtering expression.</value> |
| 0 | 79 | | public RuleAction Action { get; set; } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Gets or sets the name of the rule. |
| | 83 | | /// </summary> |
| | 84 | | /// <value>Returns a <see cref="System.String" /> Representing the name of the rule.</value> |
| | 85 | | /// <remarks>Max allowed length of rule name is 50 chars.</remarks> |
| | 86 | | public string Name |
| | 87 | | { |
| 0 | 88 | | get => this.name; |
| | 89 | |
|
| | 90 | | set |
| | 91 | | { |
| 0 | 92 | | EntityNameHelper.CheckValidRuleName(value); |
| 0 | 93 | | this.name = value; |
| 0 | 94 | | } |
| | 95 | | } |
| | 96 | |
|
| | 97 | | // TODO: Implement for AMQP |
| | 98 | | internal DateTime CreatedAt |
| | 99 | | { |
| 0 | 100 | | get; set; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | public override int GetHashCode() |
| | 104 | | { |
| 0 | 105 | | int hash = 13; |
| | 106 | | unchecked |
| | 107 | | { |
| 0 | 108 | | hash = (hash * 7) + this.filter?.GetHashCode() ?? 0; |
| 0 | 109 | | hash = (hash * 7) + this.Action?.GetHashCode() ?? 0; |
| | 110 | | } |
| 0 | 111 | | return hash; |
| | 112 | | } |
| | 113 | |
|
| | 114 | | public override bool Equals(object obj) |
| | 115 | | { |
| 0 | 116 | | var other = obj as RuleDescription; |
| 0 | 117 | | return this.Equals(other); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | public bool Equals(RuleDescription otherRule) |
| | 121 | | { |
| 0 | 122 | | if (otherRule is RuleDescription other |
| 0 | 123 | | && string.Equals(this.Name, other.Name, StringComparison.OrdinalIgnoreCase) |
| 0 | 124 | | && (this.Filter == null || this.Filter.Equals(other.Filter)) |
| 0 | 125 | | && (this.Action == null || this.Action.Equals(other.Action))) |
| | 126 | | { |
| 0 | 127 | | return true; |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | return false; |
| | 131 | | } |
| | 132 | |
|
| | 133 | | public static bool operator ==(RuleDescription o1, RuleDescription o2) |
| | 134 | | { |
| 0 | 135 | | if (ReferenceEquals(o1, o2)) |
| | 136 | | { |
| 0 | 137 | | return true; |
| | 138 | | } |
| | 139 | |
|
| 0 | 140 | | if (ReferenceEquals(o1, null) || ReferenceEquals(o2, null)) |
| | 141 | | { |
| 0 | 142 | | return false; |
| | 143 | | } |
| | 144 | |
|
| 0 | 145 | | return o1.Equals(o2); |
| | 146 | | } |
| | 147 | |
|
| | 148 | | public static bool operator !=(RuleDescription o1, RuleDescription o2) |
| | 149 | | { |
| 0 | 150 | | return !(o1 == o2); |
| | 151 | | } |
| | 152 | | } |
| | 153 | | } |