JD2022-TU1/main/tools/framework/JD.Collections/ObjectModel/ReadOnlyKeyedList.cs

110 lines
No EOL
2.7 KiB
C#

using System.Collections.Generic;
using JD.Collections.Extensions;
using JD.Collections.Interfaces;
using JD.Kernel.StaticHelpers;
namespace JD.Collections.ObjectModel
{
/// <summary>
/// A read-only implementation of a <see cref="IKeyedList{K,V}"/>
/// </summary>
public class ReadOnlyKeyedList<K, V> : ReadOnlyListBase<V>, IKeyedList<K, V>
{
#region Variables
private readonly System.Collections.ObjectModel.KeyedCollection<K, V> m_wrappedCollection;
#endregion
#region Constructor
/// <summary>
/// Constructor.
/// </summary>
/// <param name="collection"></param>
public ReadOnlyKeyedList(System.Collections.ObjectModel.KeyedCollection<K, V> collection)
{
Throw.IfNull(collection, "collection");
m_wrappedCollection = collection;
}
#endregion
#region Properties
/// <summary>
/// Count.
/// </summary>
public override int Count
{
get { return m_wrappedCollection.Count; }
}
/// <summary>
/// Indexer.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public V this[K key]
{
get { return m_wrappedCollection[key]; }
}
#endregion
#region Methods
/// <summary>
/// GetItem.
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
protected override V GetItem(int index)
{
return m_wrappedCollection[index];
}
/// <summary>
/// ContainsKey.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsKey(K key)
{
return m_wrappedCollection.Contains(key);
}
/// <summary>
/// Remove.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Remove(K key)
{
MethodModifiesCollection();
return false;
}
/// <summary>
/// TryGetValue
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool TryGetValue(K key, out V value)
{
return m_wrappedCollection.TryGetValue(key, out value);
}
/// <summary>
/// GetEnumerator.
/// </summary>
/// <returns></returns>
public override IEnumerator<V> GetEnumerator()
{
return m_wrappedCollection.GetEnumerator();
}
#endregion
}
}