#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
{
///
/// Used internally to accelerate hash table searches.
///
///
///
/// Internal class used to improve performance of
/// string keyed hashtables.
///
///
/// The hashcode of the string is cached for reuse.
/// The string is stored as an interned value.
/// When comparing two objects for equality
/// the reference equality of the interned strings is compared.
///
///
/// Nicko Cadell
/// Gert Driesen
internal sealed class LoggerKey
{
#region Internal Instance Constructors
///
/// Construct key with string name
///
///
///
/// Initializes a new instance of the class
/// with the specified name.
///
///
/// Stores the hashcode of the string and interns
/// the string key to optimize comparisons.
///
///
/// The Compact Framework 1.0 the
/// method does not work. On the Compact Framework
/// the string keys are not interned nor are they
/// compared by reference.
///
///
/// The name of the logger.
internal LoggerKey(string name)
{
#if NETCF || NETSTANDARD1_3
// 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
///
/// Returns a hash code for the current instance.
///
/// A hash code for the current instance.
///
///
/// Returns the cached hashcode.
///
///
public override int GetHashCode()
{
return m_hashCache;
}
///
/// Determines whether two instances
/// are equal.
///
/// The to compare with the current .
///
/// true if the specified is equal to the current ; otherwise, false.
///
///
///
/// Compares the references of the interned strings.
///
///
public override 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 || NETSTANDARD1_3
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
}
}