#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;
using System.Collections;
namespace log4net.Util
{
///
/// An always empty .
///
///
///
/// A singleton implementation of the
/// interface that always represents an empty collection.
///
///
/// Nicko Cadell
/// Gert Driesen
#if !NETCF
[Serializable]
#endif
public sealed class EmptyDictionary : IDictionary
{
#region Private Instance Constructors
///
/// Initializes a new instance of the class.
///
///
///
/// Uses a private access modifier to enforce the singleton pattern.
///
///
private EmptyDictionary()
{
}
#endregion Private Instance Constructors
#region Public Static Properties
///
/// Gets the singleton instance of the .
///
/// The singleton instance of the .
///
///
/// Gets the singleton instance of the .
///
///
public static EmptyDictionary Instance
{
get { return s_instance; }
}
#endregion Public Static Properties
#region Implementation of ICollection
///
/// Copies the elements of the to an
/// , starting at a particular Array index.
///
/// The one-dimensional
/// that is the destination of the elements copied from
/// . The Array must have zero-based
/// indexing.
/// The zero-based index in array at which
/// copying begins.
///
///
/// As the collection is empty no values are copied into the array.
///
///
public void CopyTo(System.Array array, int index)
{
// copy nothing
}
///
/// Gets a value indicating if access to the is synchronized (thread-safe).
///
///
/// true if access to the is synchronized (thread-safe); otherwise, false.
///
///
///
/// For the this property is always true.
///
///
public bool IsSynchronized
{
get { return true; }
}
///
/// Gets the number of elements contained in the
///
///
/// The number of elements contained in the .
///
///
///
/// As the collection is empty the is always 0.
///
///
public int Count
{
get { return 0; }
}
///
/// Gets an object that can be used to synchronize access to the .
///
///
/// An object that can be used to synchronize access to the .
///
///
///
/// As the collection is empty and thread safe and synchronized this instance is also
/// the object.
///
///
public object SyncRoot
{
get { return this; }
}
#endregion Implementation of ICollection
#region Implementation of IEnumerable
///
/// Returns an enumerator that can iterate through a collection.
///
///
/// An that can be used to
/// iterate through the collection.
///
///
///
/// As the collection is empty a is returned.
///
///
IEnumerator IEnumerable.GetEnumerator()
{
return NullEnumerator.Instance;
}
#endregion Implementation of IEnumerable
#region Implementation of IDictionary
///
/// Adds an element with the provided key and value to the
/// .
///
/// The to use as the key of the element to add.
/// The to use as the value of the element to add.
///
///
/// As the collection is empty no new values can be added. A
/// is thrown if this method is called.
///
///
/// This dictionary is always empty and cannot be modified.
public void Add(object key, object value)
{
throw new InvalidOperationException();
}
///
/// Removes all elements from the .
///
///
///
/// As the collection is empty no values can be removed. A
/// is thrown if this method is called.
///
///
/// This dictionary is always empty and cannot be modified.
public void Clear()
{
throw new InvalidOperationException();
}
///
/// Determines whether the contains an element
/// with the specified key.
///
/// The key to locate in the .
/// false
///
///
/// As the collection is empty the method always returns false.
///
///
public bool Contains(object key)
{
return false;
}
///
/// Returns an enumerator that can iterate through a collection.
///
///
/// An that can be used to
/// iterate through the collection.
///
///
///
/// As the collection is empty a is returned.
///
///
public IDictionaryEnumerator GetEnumerator()
{
return NullDictionaryEnumerator.Instance;
}
///
/// Removes the element with the specified key from the .
///
/// The key of the element to remove.
///
///
/// As the collection is empty no values can be removed. A
/// is thrown if this method is called.
///
///
/// This dictionary is always empty and cannot be modified.
public void Remove(object key)
{
throw new InvalidOperationException();
}
///
/// Gets a value indicating whether the has a fixed size.
///
/// true
///
///
/// As the collection is empty always returns true.
///
///
public bool IsFixedSize
{
get { return true; }
}
///
/// Gets a value indicating whether the is read-only.
///
/// true
///
///
/// As the collection is empty always returns true.
///
///
public bool IsReadOnly
{
get { return true; }
}
///
/// Gets an containing the keys of the .
///
/// An containing the keys of the .
///
///
/// As the collection is empty a is returned.
///
///
public System.Collections.ICollection Keys
{
get { return EmptyCollection.Instance; }
}
///
/// Gets an containing the values of the .
///
/// An containing the values of the .
///
///
/// As the collection is empty a is returned.
///
///
public System.Collections.ICollection Values
{
get { return EmptyCollection.Instance; }
}
///
/// Gets or sets the element with the specified key.
///
/// The key of the element to get or set.
/// null
///
///
/// As the collection is empty no values can be looked up or stored.
/// If the index getter is called then null is returned.
/// A is thrown if the setter is called.
///
///
/// This dictionary is always empty and cannot be modified.
public object this[object key]
{
get { return null; }
set { throw new InvalidOperationException(); }
}
#endregion Implementation of IDictionary
#region Private Static Fields
///
/// The singleton instance of the empty dictionary.
///
private readonly static EmptyDictionary s_instance = new EmptyDictionary();
#endregion Private Static Fields
}
}