File: UniqueIdentifierService.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (110 lines) | stat: -rw-r--r-- 4,609 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//---------------------------------------------------------------------
// <copyright file="UniqueIdentifierService.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner       [....]
// @backupOwner [....]
//---------------------------------------------------------------------


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


    internal sealed class UniqueIdentifierService
    {
        internal UniqueIdentifierService(bool caseSensitive)
        {
            _knownIdentifiers = new Dictionary<string, bool>(caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);
            _identifierToAdjustedIdentifier = new Dictionary<object, string>();
            _transform = s => s;
        }

        internal UniqueIdentifierService(bool caseSensitive, Func<string, string> transform)
        {
            Debug.Assert(transform != null, "use the other constructor if you don't want any transform");
            _knownIdentifiers = new Dictionary<string, bool>(caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);
            _identifierToAdjustedIdentifier = new Dictionary<object, string>();
            _transform = transform;
        }

        private readonly Dictionary<string, bool> _knownIdentifiers;
        private readonly Dictionary<object, string> _identifierToAdjustedIdentifier;
        private readonly Func<string, string> _transform;

        /// <summary>
        /// This method can be used in when you have an
        /// identifier that you know can't be used, but you don't want
        /// an adjusted version of it
        /// </summary>
        /// <param name="identifier"></param>
        internal void RegisterUsedIdentifier(string identifier)
        {
            Debug.Assert(!_knownIdentifiers.ContainsKey(identifier), "don't register identifiers that already exist");
            _knownIdentifiers.Add(identifier, true);
        }


        /// <summary>
        /// Given an identifier, makes it unique within the scope by adding
        /// a suffix (1, 2, 3, ...), and returns the adjusted identifier.
        /// </summary>
        /// <param name="identifier">Identifier. Must not be null or empty.</param>
        /// <param name="value">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.</param>
        /// <returns>Identifier adjusted to be unique within the scope.</returns>
        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 = _transform(identifier);
            while (_knownIdentifiers.ContainsKey(adjustedIdentifier))
            {
                ++numberOfConflicts;
                adjustedIdentifier = _transform(identifier) + numberOfConflicts.ToString(CultureInfo.InvariantCulture);
            }

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

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

            return adjustedIdentifier;
        }

        
        /// <summary>
        /// Simple overload when you don't need to track back to an object
        /// </summary>
        /// <param name="identifier"></param>
        /// <returns></returns>
        internal string AdjustIdentifier(string identifier)
        {
            return AdjustIdentifier(identifier, null);
        }

        /// <summary>
        /// Determines the adjusted name for an identifier if it has been registered in this scope.
        /// </summary>
        internal bool TryGetAdjustedName(object value, out string adjustedIdentifier)
        {
            return _identifierToAdjustedIdentifier.TryGetValue(value, out adjustedIdentifier);
        }
    }
}