| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using Azure.Core; |
| | | 6 | | |
| | | 7 | | namespace Azure.Messaging.ServiceBus.Management |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents the properties of a rule. |
| | | 11 | | /// </summary> |
| | | 12 | | public sealed class RuleProperties : IEquatable<RuleProperties> |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets the name of the default rule on the subscription. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// Whenever a new subscription is created, a default rule is always added. |
| | | 19 | | /// The default rule is a <see cref="TrueRuleFilter"/> which will enable all messages in the topic to reach subs |
| | | 20 | | /// </remarks> |
| | | 21 | | public const string DefaultRuleName = "$Default"; |
| | | 22 | | private RuleFilter _filter; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="RuleProperties" /> class with default values. |
| | | 26 | | /// </summary> |
| | | 27 | | public RuleProperties() |
| | 48 | 28 | | : this(DefaultRuleName, TrueRuleFilter.Default) |
| | | 29 | | { |
| | 48 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Initializes a new instance of the <see cref="RuleProperties" /> class with the specified name. |
| | | 34 | | /// </summary> |
| | | 35 | | public RuleProperties(string name) |
| | 0 | 36 | | : this(name, TrueRuleFilter.Default) |
| | | 37 | | { |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the <see cref="RuleProperties" /> class with the specified name and filter exp |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="name"></param> |
| | | 44 | | /// <param name="filter">The filter expression used to match messages.</param> |
| | 48 | 45 | | public RuleProperties(string name, RuleFilter filter) |
| | | 46 | | { |
| | 48 | 47 | | Argument.AssertNotNull(filter, nameof(filter)); |
| | 48 | 48 | | Filter = filter; |
| | 48 | 49 | | Name = name; |
| | 48 | 50 | | } |
| | | 51 | | |
| | 44 | 52 | | internal RuleProperties(CreateRuleOptions options) |
| | | 53 | | { |
| | 44 | 54 | | Filter = options.Filter; |
| | 44 | 55 | | Action = options.Action; |
| | 44 | 56 | | Name = options.Name; |
| | 44 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Gets or sets the filter expression used to match messages. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <value>The filter expression used to match messages.</value> |
| | | 63 | | /// <exception cref="System.ArgumentNullException">null (Nothing in Visual Basic) is assigned.</exception> |
| | | 64 | | public RuleFilter Filter |
| | | 65 | | { |
| | 96 | 66 | | get => _filter; |
| | | 67 | | |
| | | 68 | | set |
| | | 69 | | { |
| | 140 | 70 | | Argument.AssertNotNull(value, nameof(Filter)); |
| | 140 | 71 | | _filter = value; |
| | 140 | 72 | | } |
| | | 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> |
| | 168 | 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> |
| | 240 | 86 | | public string Name { get; internal set; } |
| | | 87 | | |
| | | 88 | | // TODO: Implement for AMQP |
| | 0 | 89 | | internal DateTime CreatedAt { get; set; } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc/> |
| | | 92 | | public override int GetHashCode() |
| | | 93 | | { |
| | 0 | 94 | | int hash = 13; |
| | | 95 | | unchecked |
| | | 96 | | { |
| | 0 | 97 | | hash = (hash * 7) + _filter?.GetHashCode() ?? 0; |
| | 0 | 98 | | hash = (hash * 7) + Action?.GetHashCode() ?? 0; |
| | | 99 | | } |
| | 0 | 100 | | return hash; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <inheritdoc/> |
| | | 104 | | public override bool Equals(object obj) |
| | | 105 | | { |
| | 0 | 106 | | var other = obj as RuleProperties; |
| | 0 | 107 | | return Equals(other); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc/> |
| | | 111 | | public bool Equals(RuleProperties other) |
| | | 112 | | { |
| | 4 | 113 | | if (other is RuleProperties otherProperties |
| | 4 | 114 | | && string.Equals(Name, otherProperties.Name, StringComparison.OrdinalIgnoreCase) |
| | 4 | 115 | | && (Filter == null || Filter.Equals(otherProperties.Filter)) |
| | 4 | 116 | | && (Action == null || Action.Equals(otherProperties.Action))) |
| | | 117 | | { |
| | 4 | 118 | | return true; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | return false; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="left"></param> |
| | | 128 | | /// <param name="right"></param> |
| | | 129 | | /// <returns></returns> |
| | | 130 | | public static bool operator ==(RuleProperties left, RuleProperties right) |
| | | 131 | | { |
| | 36 | 132 | | if (ReferenceEquals(left, right)) |
| | | 133 | | { |
| | 8 | 134 | | return true; |
| | | 135 | | } |
| | | 136 | | |
| | 28 | 137 | | if (ReferenceEquals(left, null) || ReferenceEquals(right, null)) |
| | | 138 | | { |
| | 28 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | return left.Equals(right); |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="left"></param> |
| | | 149 | | /// <param name="right"></param> |
| | | 150 | | /// <returns></returns> |
| | | 151 | | public static bool operator !=(RuleProperties left, RuleProperties right) |
| | | 152 | | { |
| | 36 | 153 | | return !(left == right); |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | } |