2013-04-06 01:46:28 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-03-31 14:09:38 +02:00
|
|
|
|
|
|
|
|
|
|
namespace ChanSort.Api
|
|
|
|
|
|
{
|
|
|
|
|
|
public class MappingPool<T> where T : DataMapping
|
|
|
|
|
|
{
|
|
|
|
|
|
private const string ERR_unknownACTChannelDataLength = "Configuration doesn't contain a {0} data mapping for length {1}";
|
2013-05-03 19:02:30 +02:00
|
|
|
|
private readonly Dictionary<string, T> mappings = new Dictionary<string, T>();
|
2013-03-31 14:09:38 +02:00
|
|
|
|
private readonly string caption;
|
2013-09-15 18:27:04 +02:00
|
|
|
|
public System.Text.Encoding DefaultEncoding { get; set; }
|
2013-03-31 14:09:38 +02:00
|
|
|
|
|
|
|
|
|
|
public MappingPool(string caption)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.caption = caption;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-05-03 19:02:30 +02:00
|
|
|
|
public void AddMapping(int dataLength, T mapping)
|
2013-03-31 14:09:38 +02:00
|
|
|
|
{
|
2013-05-03 19:02:30 +02:00
|
|
|
|
this.AddMapping(dataLength.ToString(), mapping);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void AddMapping(string id, T mapping)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.mappings.Add(id, mapping);
|
2013-03-31 14:09:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public T GetMapping(int dataLength, bool throwException = true)
|
|
|
|
|
|
{
|
2013-05-03 19:02:30 +02:00
|
|
|
|
return this.GetMapping(dataLength.ToString(), throwException);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public T GetMapping(string id, bool throwException = true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (id == "0" || string.IsNullOrEmpty(id))
|
2013-03-31 14:09:38 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
T mapping;
|
2013-05-03 19:02:30 +02:00
|
|
|
|
if (!mappings.TryGetValue(id, out mapping) && throwException)
|
2022-11-29 14:56:23 +01:00
|
|
|
|
throw LoaderException.Fail(string.Format(ERR_unknownACTChannelDataLength, this.caption, id));
|
2013-05-03 19:02:30 +02:00
|
|
|
|
|
2013-09-15 18:27:04 +02:00
|
|
|
|
if (mapping != null && this.DefaultEncoding != null)
|
|
|
|
|
|
mapping.DefaultEncoding = this.DefaultEncoding;
|
2013-03-31 14:09:38 +02:00
|
|
|
|
return mapping;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|