/* Copyright 2010-present MongoDB Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; namespace MongoDB.Driver { /// /// Represents a list of credentials and the rules about how credentials can be used together. /// internal class MongoCredentialStore : IEnumerable, IEquatable { // private fields private readonly ReadOnlyCollection _credentials; // constructors /// /// Creates a new instance of the MongoCredentialStore class. /// /// The credentials. public MongoCredentialStore(IEnumerable credentials) { if (credentials == null) { throw new ArgumentNullException("credentials"); } _credentials = new ReadOnlyCollection(new List(credentials)); EnsureCredentialsAreCompatibleWithEachOther(); } // public operators /// /// Determines whether two instances are equal. /// /// The LHS. /// The RHS. /// /// true if the left hand side is equal to the right hand side; otherwise, false. /// public static bool operator ==(MongoCredentialStore lhs, MongoCredentialStore rhs) { return object.Equals(lhs, rhs); // handles lhs == null correctly } /// /// Determines whether two instances are not equal. /// /// The LHS. /// The RHS. /// /// true if the left hand side is not equal to the right hand side; otherwise, false. /// public static bool operator !=(MongoCredentialStore lhs, MongoCredentialStore rhs) { return !(lhs == rhs); } // public methods /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public bool Equals(MongoCredentialStore obj) { return Equals((object)obj); // handles obj == null correctly } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object obj) { if (object.ReferenceEquals(obj, null) || GetType() != obj.GetType()) { return false; } var rhs = (MongoCredentialStore)obj; return _credentials.SequenceEqual(rhs._credentials); } /// /// Gets the enumerator. /// /// public IEnumerator GetEnumerator() { return _credentials.GetEnumerator(); } /// /// Gets the hashcode for the credential store. /// /// The hashcode. public override int GetHashCode() { // see Effective Java by Joshua Bloch int hash = 17; foreach (var credential in _credentials) { hash = 37 * hash + credential.GetHashCode(); } return hash; } /// /// Returns a string representation of the credential store. /// /// A string representation of the credential store. public override string ToString() { return string.Format("{{{0}}}", string.Join(",", _credentials.Select(c => c.ToString()).ToArray())); } // explicit interface implementations /// /// Returns an enumerator that iterates through a collection. /// /// /// An object that can be used to iterate through the collection. /// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return _credentials.GetEnumerator(); } // private metods private void EnsureCredentialsAreCompatibleWithEachOther() { var sources = new HashSet(_credentials.Select(c => c.Source)); if (sources.Count < _credentials.Count) { throw new ArgumentException("The server requires that each credential provided be from a different source."); } } } }