File: fx\src\data\System\Data\SqlClient\SqlConnectionPoolKey.cs
Project: ndp\System.Data.csproj (System.Data)
//------------------------------------------------------------------------------
// <copyright file="ConnectionPoolKey.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------
 
namespace System.Data.SqlClient
{
 
    using System;
    using System.Collections;
    using System.Data.Common;
    using System.Diagnostics;
 
    // SqlConnectionPoolKey: Implementation of a key to connection pool groups for specifically to be used for SqlConnection
    //  Connection string and SqlCredential are used as a key
    internal class SqlConnectionPoolKey : DbConnectionPoolKey, ICloneable
    {
        private SqlCredential _credential;
        private int _hashValue;
        private readonly string _accessToken;
 
        internal SqlConnectionPoolKey(string connectionString, SqlCredential credential, string accessToken) : base(connectionString)
        {
            Debug.Assert(_credential == null || _accessToken == null, "Credential and AccessToken can't have the value at the same time.");
            _credential = credential;
            _accessToken = accessToken;
            CalculateHashCode();
        }
 
        private SqlConnectionPoolKey(SqlConnectionPoolKey key) : base (key)
        {
             _credential = key.Credential;
             _accessToken = key.AccessToken;
             CalculateHashCode();
        }
 
        object ICloneable.Clone()
        {
            return new SqlConnectionPoolKey(this);
        }
 
        internal override string ConnectionString
        {
            get
            {
                return base.ConnectionString;
            }
 
            set
            {
                base.ConnectionString = value;
                CalculateHashCode();
            }
        }
 
        internal SqlCredential Credential
        {
            get
            {
                return _credential;
            }
        }
 
        internal string AccessToken
        {
            get
            {
                return _accessToken;
            }
        }
 
        public override bool Equals(object obj)
        {
            SqlConnectionPoolKey key = obj as SqlConnectionPoolKey;
 
            return (key != null && _credential == key._credential && ConnectionString == key.ConnectionString && Object.ReferenceEquals(_accessToken, key._accessToken));
        }
 
        public override int GetHashCode()
        {
            return _hashValue;
        }
 
        private void CalculateHashCode()
        {
            _hashValue = base.GetHashCode();
 
            if (_credential != null)
            {
                unchecked
                {
                    _hashValue = _hashValue * 17 + _credential.GetHashCode();
                }
            }
            else if (_accessToken != null)
            {
                unchecked
                {
                    _hashValue = _hashValue * 17 + _accessToken.GetHashCode();
                }
            }
        }
    }
}