// // AccessRights.cs // // Author: Jeffrey Stedfast // // Copyright (c) 2013-2020 .NET Foundation and Contributors // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // using System; using System.Collections; using System.Collections.Generic; namespace MailKit { /// /// A set of access rights. /// /// /// The set of access rights for a particular identity. /// public class AccessRights : ICollection { readonly List list = new List (); /// /// Initializes a new instance of the class. /// /// /// Creates a new set of access rights. /// /// The access rights. /// /// is null. /// public AccessRights (IEnumerable rights) { AddRange (rights); } /// /// Initializes a new instance of the class. /// /// /// Creates a new set of access rights. /// /// The access rights. /// /// is null. /// public AccessRights (string rights) { AddRange (rights); } /// /// Initializes a new instance of the class. /// /// /// Creates an empty set of access rights. /// public AccessRights () { } /// /// Get the number of access rights in the collection. /// /// /// Gets the number of access rights in the collection. /// /// The count. public int Count { get { return list.Count; } } /// /// Get whether or not this set of access rights is read only. /// /// /// Gets whether or not this set of access rights is read only. /// /// true if this collection is read only; otherwise, false. public bool IsReadOnly { get { return false; } } /// /// Add the specified access right. /// /// /// Adds the specified access right if it is not already included. /// /// The access right. void ICollection.Add (AccessRight right) { Add (right); } /// /// Add the specified access right. /// /// /// Adds the specified access right if it is not already included. /// /// true if the right was added; otherwise, false. /// The access right. public bool Add (AccessRight right) { if (list.Contains (right)) return false; list.Add (right); return true; } /// /// Add the specified right. /// /// /// Adds the right specified by the given character. /// /// true if the right was added; otherwise, false. /// The right. public bool Add (char right) { return Add (new AccessRight (right)); } /// /// Add the rights specified by the characters in the given string. /// /// /// Adds the rights specified by the characters in the given string. /// /// The rights. /// /// is null. /// public void AddRange (string rights) { if (rights == null) throw new ArgumentNullException (nameof (rights)); for (int i = 0; i < rights.Length; i++) Add (new AccessRight (rights[i])); } /// /// Add the range of specified rights. /// /// /// Adds the range of specified rights. /// /// The rights. /// /// is null. /// public void AddRange (IEnumerable rights) { if (rights == null) throw new ArgumentNullException (nameof (rights)); foreach (var right in rights) Add (right); } /// /// Clears the access rights. /// /// /// Removes all of the access rights. /// public void Clear () { list.Clear (); } /// /// Checks if the set of access rights contains the specified right. /// /// /// Determines whether or not the set of access rights already contains the specified right /// /// true if the specified right exists; otherwise false. /// The access right. public bool Contains (AccessRight right) { return list.Contains (right); } /// /// Copies all of the access rights to the specified array. /// /// /// Copies all of the access rights into the array, /// starting at the specified array index. /// /// The array. /// The array index. /// /// is null. /// /// /// is out of range. /// public void CopyTo (AccessRight[] array, int arrayIndex) { if (array == null) throw new ArgumentNullException (nameof (array)); if (arrayIndex < 0 || arrayIndex + Count > array.Length) throw new ArgumentOutOfRangeException (nameof (arrayIndex)); list.CopyTo (array, arrayIndex); } /// /// Removes the specified access right. /// /// /// Removes the specified access right. /// /// true if the access right was removed; otherwise false. /// The access right. public bool Remove (AccessRight right) { return list.Remove (right); } /// /// Get the access right at the specified index. /// /// /// Gets the access right at the specified index. /// /// The access right at the specified index. /// The index. /// /// is out of range. /// public AccessRight this [int index] { get { if (index < 0 || index >= list.Count) throw new ArgumentOutOfRangeException (nameof (index)); return list[index]; } } #region IEnumerable implementation /// /// Get the access rights enumerator. /// /// /// Gets the access rights enumerator. /// /// The enumerator. public IEnumerator GetEnumerator () { return list.GetEnumerator (); } #endregion #region IEnumerable implementation /// /// Get the access rights enumerator. /// /// /// Gets the access rights enumerator. /// /// The enumerator. IEnumerator IEnumerable.GetEnumerator () { return list.GetEnumerator (); } #endregion /// /// Return a that represents the current . /// /// /// Returns a that represents the current . /// /// A that represents the current . public override string ToString () { var rights = new char[list.Count]; for (int i = 0; i < list.Count; i++) rights[i] = list[i].Right; return new string (rights); } } }