LoggerKey.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. namespace log4net.Repository.Hierarchy
  21. {
  22. /// <summary>
  23. /// Used internally to accelerate hash table searches.
  24. /// </summary>
  25. /// <remarks>
  26. /// <para>
  27. /// Internal class used to improve performance of
  28. /// string keyed hashtables.
  29. /// </para>
  30. /// <para>
  31. /// The hashcode of the string is cached for reuse.
  32. /// The string is stored as an interned value.
  33. /// When comparing two <see cref="LoggerKey"/> objects for equality
  34. /// the reference equality of the interned strings is compared.
  35. /// </para>
  36. /// </remarks>
  37. /// <author>Nicko Cadell</author>
  38. /// <author>Gert Driesen</author>
  39. internal sealed class LoggerKey
  40. {
  41. #region Internal Instance Constructors
  42. /// <summary>
  43. /// Construct key with string name
  44. /// </summary>
  45. /// <remarks>
  46. /// <para>
  47. /// Initializes a new instance of the <see cref="LoggerKey" /> class
  48. /// with the specified name.
  49. /// </para>
  50. /// <para>
  51. /// Stores the hashcode of the string and interns
  52. /// the string key to optimize comparisons.
  53. /// </para>
  54. /// <note>
  55. /// The Compact Framework 1.0 the <see cref="String.Intern"/>
  56. /// method does not work. On the Compact Framework
  57. /// the string keys are not interned nor are they
  58. /// compared by reference.
  59. /// </note>
  60. /// </remarks>
  61. /// <param name="name">The name of the logger.</param>
  62. internal LoggerKey(string name)
  63. {
  64. #if NETCF || NETSTANDARD1_3
  65. // NETCF: String.Intern causes Native Exception
  66. m_name = name;
  67. #else
  68. m_name = string.Intern(name);
  69. #endif
  70. m_hashCache = name.GetHashCode();
  71. }
  72. #endregion Internal Instance Constructors
  73. #region Override implementation of Object
  74. /// <summary>
  75. /// Returns a hash code for the current instance.
  76. /// </summary>
  77. /// <returns>A hash code for the current instance.</returns>
  78. /// <remarks>
  79. /// <para>
  80. /// Returns the cached hashcode.
  81. /// </para>
  82. /// </remarks>
  83. override public int GetHashCode()
  84. {
  85. return m_hashCache;
  86. }
  87. /// <summary>
  88. /// Determines whether two <see cref="LoggerKey" /> instances
  89. /// are equal.
  90. /// </summary>
  91. /// <param name="obj">The <see cref="object" /> to compare with the current <see cref="LoggerKey" />.</param>
  92. /// <returns>
  93. /// <c>true</c> if the specified <see cref="object" /> is equal to the current <see cref="LoggerKey" />; otherwise, <c>false</c>.
  94. /// </returns>
  95. /// <remarks>
  96. /// <para>
  97. /// Compares the references of the interned strings.
  98. /// </para>
  99. /// </remarks>
  100. override public bool Equals(object obj)
  101. {
  102. // Compare reference type of this against argument
  103. if (((object)this) == obj)
  104. {
  105. return true;
  106. }
  107. LoggerKey objKey = obj as LoggerKey;
  108. if (objKey != null)
  109. {
  110. #if NETCF || NETSTANDARD1_3
  111. return ( m_name == objKey.m_name );
  112. #else
  113. // Compare reference types rather than string's overloaded ==
  114. return ( ((object)m_name) == ((object)objKey.m_name) );
  115. #endif
  116. }
  117. return false;
  118. }
  119. #endregion
  120. #region Private Instance Fields
  121. private readonly string m_name;
  122. private readonly int m_hashCache;
  123. #endregion Private Instance Fields
  124. }
  125. }