UniqueIdentifierService.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataWeb / Design / system / Data / Entity / Design / Common / UniqueIdentifierService.cs / 1 / UniqueIdentifierService.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner       [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 

 
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
namespace System.Data.Services.Design.Common 
{
    ///  
    /// Service making names within a scope unique. Initialize a new instance 
    /// for every scope.
    /// 
    ///


    internal sealed class UniqueIdentifierService 
    {
        internal UniqueIdentifierService(bool caseSensitive) 
        { 
            _knownIdentifiers = new HashSet(caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);
            _identifierToAdjustedIdentifier = new Dictionary(); 
        }

        private readonly HashSet _knownIdentifiers;
        private readonly Dictionary _identifierToAdjustedIdentifier; 

        ///  
        /// Given an identifier, makes it unique within the scope by adding 
        /// a suffix (1, 2, 3, ...), and returns the adjusted identifier.
        ///  
        /// Identifier. Must not be null or empty.
        /// Object associated with this identifier in case it is required to
        /// retrieve the adjusted identifier. If not null, must not exist in the current scope already.
        /// Identifier adjusted to be unique within the scope. 
        internal string AdjustIdentifier(string identifier, object value)
        { 
            Debug.Assert(!string.IsNullOrEmpty(identifier), "identifier is null or empty"); 

            // find a unique name by adding suffix as necessary 
            int numberOfConflicts = 0;
            string adjustedIdentifier = identifier;
            while (_knownIdentifiers.Contains(adjustedIdentifier))
            { 
                ++numberOfConflicts;
                adjustedIdentifier = identifier + numberOfConflicts.ToString(CultureInfo.InvariantCulture); 
            } 

            // remember the identifier in this scope 
            Debug.Assert(!_knownIdentifiers.Contains(adjustedIdentifier), "we just made it unique");
            _knownIdentifiers.Add(adjustedIdentifier);

            if (null != value) 
            {
                Debug.Assert(!_identifierToAdjustedIdentifier.ContainsKey(value), "should never register one value twice"); 
                _identifierToAdjustedIdentifier.Add(value, adjustedIdentifier); 
            }
 
            return adjustedIdentifier;
        }

 
        /// 
        /// Simple overload when you don't need to track back to an object 
        ///  
        /// 
        ///  
        internal string AdjustIdentifier(string identifier)
        {
            return AdjustIdentifier(identifier, null);
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//---------------------------------------------------------------------- 
// 
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner       [....]
// @backupOwner [....] 
//--------------------------------------------------------------------- 

 
using System.Diagnostics;
using System.Collections.Generic;
using System.Globalization;
namespace System.Data.Services.Design.Common 
{
    ///  
    /// Service making names within a scope unique. Initialize a new instance 
    /// for every scope.
    /// 
    ///


    internal sealed class UniqueIdentifierService 
    {
        internal UniqueIdentifierService(bool caseSensitive) 
        { 
            _knownIdentifiers = new HashSet(caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);
            _identifierToAdjustedIdentifier = new Dictionary(); 
        }

        private readonly HashSet _knownIdentifiers;
        private readonly Dictionary _identifierToAdjustedIdentifier; 

        ///  
        /// Given an identifier, makes it unique within the scope by adding 
        /// a suffix (1, 2, 3, ...), and returns the adjusted identifier.
        ///  
        /// Identifier. Must not be null or empty.
        /// Object associated with this identifier in case it is required to
        /// retrieve the adjusted identifier. If not null, must not exist in the current scope already.
        /// Identifier adjusted to be unique within the scope. 
        internal string AdjustIdentifier(string identifier, object value)
        { 
            Debug.Assert(!string.IsNullOrEmpty(identifier), "identifier is null or empty"); 

            // find a unique name by adding suffix as necessary 
            int numberOfConflicts = 0;
            string adjustedIdentifier = identifier;
            while (_knownIdentifiers.Contains(adjustedIdentifier))
            { 
                ++numberOfConflicts;
                adjustedIdentifier = identifier + numberOfConflicts.ToString(CultureInfo.InvariantCulture); 
            } 

            // remember the identifier in this scope 
            Debug.Assert(!_knownIdentifiers.Contains(adjustedIdentifier), "we just made it unique");
            _knownIdentifiers.Add(adjustedIdentifier);

            if (null != value) 
            {
                Debug.Assert(!_identifierToAdjustedIdentifier.ContainsKey(value), "should never register one value twice"); 
                _identifierToAdjustedIdentifier.Add(value, adjustedIdentifier); 
            }
 
            return adjustedIdentifier;
        }

 
        /// 
        /// Simple overload when you don't need to track back to an object 
        ///  
        /// 
        ///  
        internal string AdjustIdentifier(string identifier)
        {
            return AdjustIdentifier(identifier, null);
        } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK