JD2022-TU1/main/tools/framework/JD.Collections/KeyedCacheWithCreator.cs

45 lines
No EOL
1.2 KiB
C#

using System;
using JD.Kernel.StaticHelpers;
namespace JD.Collections
{
/// <summary>
/// Caches values based on a key uses a delegate for new object creation.
/// </summary>
public class KeyedCacheWithCreator<TKey, TValue> : AbstractKeyedCache<TKey, TValue>
{
private Converter<TKey, TValue> m_valueCreator;
/// <summary>
/// Ctor.
/// </summary>
/// <param name="valueCreator">Non null value creator.</param>
public KeyedCacheWithCreator(Converter<TKey, TValue> valueCreator)
{
Throw.IfNull(valueCreator, "valueCreator");
m_valueCreator = valueCreator;
}
/// <summary>
/// The delegate used to construct new values.
/// </summary>
public Converter<TKey, TValue> ValueCreator
{
set
{
Throw.IfNull(value, "value");
m_valueCreator = value;
}
}
/// <summary>
/// Creates a value for the key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
protected override TValue CreateValue(TKey key)
{
return m_valueCreator(key);
}
}
}