149 lines
4 KiB
C#
149 lines
4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using JD.Kernel.StaticHelpers;
|
|
|
|
namespace JD.Collections.ObjectModel
|
|
{
|
|
/// <summary>
|
|
/// A read-only wrapper around another <see cref="IDictionary{TKey, TValue}"/>.
|
|
/// </summary>
|
|
public class ReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
|
|
{
|
|
#region Variables
|
|
|
|
private readonly IDictionary<TKey, TValue> m_dictionary;
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Constructor.
|
|
/// </summary>
|
|
/// <param name="dictionary">Wrapped dictionary.</param>
|
|
public ReadOnlyDictionary(IDictionary<TKey, TValue> dictionary)
|
|
{
|
|
Throw.IfNull(dictionary, "dictionary");
|
|
|
|
m_dictionary = dictionary;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Gets or sets the element with the specified key.
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public TValue this[TKey key]
|
|
{
|
|
get
|
|
{
|
|
return m_dictionary[key];
|
|
}
|
|
set
|
|
{
|
|
throw new NotSupportedException("This dictionary is read-only.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets an ICollection containing the keys of the IDictionary.
|
|
/// </summary>
|
|
public IEnumerable<TKey> Keys
|
|
{
|
|
get { return m_dictionary.Keys; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets an ICollection containing the values of the IDictionary.
|
|
/// </summary>
|
|
public IEnumerable<TValue> Values
|
|
{
|
|
get { return m_dictionary.Values; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the number of key/value pairs contained in the Dictionary.
|
|
/// </summary>
|
|
public int Count
|
|
{
|
|
get { return m_dictionary.Count; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the collection is read-only.
|
|
/// </summary>
|
|
public bool IsReadOnly
|
|
{
|
|
get { return true; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
#region Other Methods
|
|
|
|
/// <summary>
|
|
/// Determines whether the IDictionary contains an element with the specified key.
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public bool ContainsKey(TKey key)
|
|
{
|
|
return m_dictionary.ContainsKey(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the value associated with the specified key.
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public bool TryGetValue(TKey key, out TValue value)
|
|
{
|
|
return m_dictionary.TryGetValue(key, out value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines whether the ICollection contains a specific key and value.
|
|
/// </summary>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public bool Contains(KeyValuePair<TKey, TValue> item)
|
|
{
|
|
return m_dictionary.Contains(item);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copies the elements of the ICollection to an array, starting at the specified array index.
|
|
/// </summary>
|
|
/// <param name="array"></param>
|
|
/// <param name="arrayIndex"></param>
|
|
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
|
{
|
|
m_dictionary.CopyTo(array, arrayIndex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an enumerator that iterates through the collection.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
|
{
|
|
return m_dictionary.GetEnumerator();
|
|
}
|
|
|
|
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#endregion
|
|
}
|
|
}
|