zhao
2021-07-02 081df17b8cc4a6e7e4f4e1e1887f24810e3ec2f9
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you 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.
//
#endregion
 
using System;
 
namespace log4net.Repository.Hierarchy
{
    /// <summary>
    /// Used internally to accelerate hash table searches.
    /// </summary>
    /// <remarks>
    /// <para>
    /// Internal class used to improve performance of 
    /// string keyed hashtables.
    /// </para>
    /// <para>
    /// The hashcode of the string is cached for reuse.
    /// The string is stored as an interned value.
    /// When comparing two <see cref="LoggerKey"/> objects for equality 
    /// the reference equality of the interned strings is compared.
    /// </para>
    /// </remarks>
    /// <author>Nicko Cadell</author>
    /// <author>Gert Driesen</author>
    internal sealed class LoggerKey
    {
        #region Internal Instance Constructors
 
        /// <summary>
        /// Construct key with string name
        /// </summary>
        /// <remarks>
        /// <para>
        /// Initializes a new instance of the <see cref="LoggerKey" /> class 
        /// with the specified name.
        /// </para>
        /// <para>
        /// Stores the hashcode of the string and interns
        /// the string key to optimize comparisons.
        /// </para>
        /// <note>
        /// The Compact Framework 1.0 the <see cref="String.Intern"/>
        /// method does not work. On the Compact Framework
        /// the string keys are not interned nor are they
        /// compared by reference.
        /// </note>
        /// </remarks>
        /// <param name="name">The name of the logger.</param>
        internal LoggerKey(string name) 
        {
#if NETCF
            // NETCF: String.Intern causes Native Exception
            m_name = name;
#else
            m_name = string.Intern(name);
#endif
            m_hashCache = name.GetHashCode();
        }
 
        #endregion Internal Instance Constructors
 
        #region Override implementation of Object
 
        /// <summary>
        /// Returns a hash code for the current instance.
        /// </summary>
        /// <returns>A hash code for the current instance.</returns>
        /// <remarks>
        /// <para>
        /// Returns the cached hashcode.
        /// </para>
        /// </remarks>
        override public int GetHashCode() 
        {
            return m_hashCache;
        }
 
        /// <summary>
        /// Determines whether two <see cref="LoggerKey" /> instances 
        /// are equal.
        /// </summary>
        /// <param name="obj">The <see cref="object" /> to compare with the current <see cref="LoggerKey" />.</param>
        /// <returns>
        /// <c>true</c> if the specified <see cref="object" /> is equal to the current <see cref="LoggerKey" />; otherwise, <c>false</c>.
        /// </returns>
        /// <remarks>
        /// <para>
        /// Compares the references of the interned strings.
        /// </para>
        /// </remarks>
        override public bool Equals(object obj) 
        {
            // Compare reference type of this against argument
            if (((object)this) == obj)
            {
                return true;
            }
            
            LoggerKey objKey = obj as LoggerKey;
            if (objKey != null) 
            {
#if NETCF
                return ( m_name == objKey.m_name );
#else
                // Compare reference types rather than string's overloaded ==
                return ( ((object)m_name) == ((object)objKey.m_name) );
#endif
            }
            return false;
        }
 
        #endregion
 
        #region Private Instance Fields
 
        private readonly string m_name;  
        private readonly int m_hashCache;
 
        #endregion Private Instance Fields
    }    
}