| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Xml.Linq; |
| | 8 | |
|
| | 9 | | namespace Azure.Messaging.ServiceBus.Management |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Specifies the authorization rules. |
| | 13 | | /// </summary> |
| | 14 | | public class AuthorizationRules : List<AuthorizationRule>, IEquatable<AuthorizationRules> |
| | 15 | | { |
| | 16 | | internal XElement Serialize() |
| | 17 | | { |
| 80 | 18 | | var rules = new XElement( |
| 80 | 19 | | XName.Get("AuthorizationRules", ManagementClientConstants.ServiceBusNamespace), |
| 96 | 20 | | this.Select(rule => rule.Serialize())); |
| | 21 | |
|
| 80 | 22 | | return rules; |
| | 23 | | } |
| | 24 | |
|
| | 25 | | internal AuthorizationRules Clone() |
| | 26 | | { |
| 28 | 27 | | var rules = new AuthorizationRules(); |
| 96 | 28 | | foreach (AuthorizationRule rule in this) |
| | 29 | | { |
| 20 | 30 | | rules.Add(rule.Clone()); |
| | 31 | | } |
| 28 | 32 | | return rules; |
| | 33 | | } |
| | 34 | |
|
| 780 | 35 | | internal AuthorizationRules() { } |
| | 36 | |
|
| | 37 | | internal static AuthorizationRules ParseFromXElement(XElement xElement) |
| | 38 | | { |
| 92 | 39 | | var rules = new AuthorizationRules(); |
| 92 | 40 | | var xRules = xElement.Elements(XName.Get("AuthorizationRule", ManagementClientConstants.ServiceBusNamespace) |
| 132 | 41 | | rules.AddRange(xRules.Select(rule => AuthorizationRule.ParseFromXElement(rule))); |
| 92 | 42 | | return rules; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Returns a hash code for this instance. |
| | 47 | | /// </summary> |
| | 48 | | public override int GetHashCode() |
| | 49 | | { |
| 0 | 50 | | int hash = 7; |
| | 51 | | unchecked |
| | 52 | | { |
| 0 | 53 | | foreach (AuthorizationRule rule in this) |
| | 54 | | { |
| 0 | 55 | | hash = (hash * 7) + rule.GetHashCode(); |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| 0 | 59 | | return hash; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Determines whether the specified object is equal to the current object. |
| | 64 | | /// </summary> |
| | 65 | | public override bool Equals(object obj) |
| | 66 | | { |
| 0 | 67 | | var other = obj as AuthorizationRules; |
| 0 | 68 | | return Equals(other); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Determines whether the specified object is equal to the current object. |
| | 73 | | /// </summary> |
| | 74 | | public bool Equals(AuthorizationRules other) |
| | 75 | | { |
| 32 | 76 | | if (other is null || Count != other.Count) |
| | 77 | | { |
| 0 | 78 | | return false; |
| | 79 | | } |
| | 80 | |
|
| 32 | 81 | | var cnt = new Dictionary<string, AuthorizationRule>(); |
| 112 | 82 | | foreach (AuthorizationRule rule in this) |
| | 83 | | { |
| 24 | 84 | | cnt[rule.KeyName] = rule; |
| | 85 | | } |
| | 86 | |
|
| 112 | 87 | | foreach (AuthorizationRule otherRule in other) |
| | 88 | | { |
| 24 | 89 | | if (!cnt.TryGetValue(otherRule.KeyName, out var rule) || !rule.Equals(otherRule)) |
| | 90 | | { |
| 0 | 91 | | return false; |
| | 92 | | } |
| | 93 | | } |
| | 94 | |
|
| 32 | 95 | | return true; |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary></summary> |
| | 99 | | public static bool operator ==(AuthorizationRules left, AuthorizationRules right) |
| | 100 | | { |
| 96 | 101 | | if (ReferenceEquals(left, right)) |
| | 102 | | { |
| 0 | 103 | | return true; |
| | 104 | | } |
| | 105 | |
|
| 96 | 106 | | if (left is null || right is null) |
| | 107 | | { |
| 96 | 108 | | return false; |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | return left.Equals(right); |
| | 112 | | } |
| | 113 | |
|
| | 114 | | /// <summary></summary> |
| | 115 | | public static bool operator !=(AuthorizationRules left, AuthorizationRules right) |
| | 116 | | { |
| 64 | 117 | | return !(left == right); |
| | 118 | | } |
| | 119 | | } |
| | 120 | | } |