using System.Collections;
using XFEExtension.NetCore.Exceptions;
namespace XFEExtension.NetCore.FormatExtension;
///
/// XFE唯一字典存储
///
public class XFEDictionary : ICollection
{
private List _xFEDictionaryList = [];
///
/// 字典分隔符
///
public static string[] DictionarySeparator { get; } = ["|{+-", "-+}|"];
///
/// 条目分隔符
///
public static string[] EntrySeparator { get; } = ["[+-", "-+]"];
///
/// 字典中的条目数
///
public int Count => _xFEDictionaryList.Count;
///
/// 只读
///
public bool IsReadOnly => false;
///
/// 从字符串中加载字典
///
///
public void Load(string dictionaryString)
{
_xFEDictionaryList = ToList(dictionaryString);
}
///
/// 追加条目
///
/// 条目对象
///
public void Add(XFEEntry entry)
{
if (Contains(entry.Header))
throw new XFEDictionaryException("Header的值应唯一!");
_xFEDictionaryList.Add(entry);
}
///
/// 追加条目
///
/// 条目字符串
///
public void Add(string entryString)
{
var entry = XFEEntry.ToEntry(entryString);
if (entry is not null)
{
if (Contains(entry.Header))
throw new XFEDictionaryException("Header的值应唯一!");
_xFEDictionaryList.Add(entry);
}
}
///
/// 追加条目
///
/// 头
/// 内容
///
public void Add(string header, string content)
{
if (Contains(header))
throw new XFEDictionaryException("Header的值应唯一!");
_xFEDictionaryList.Add(new XFEEntry(header, content));
}
///
/// 追加字典
///
/// 字典对象
///
public void AddRange(List collection)
{
var duplicates = collection.GroupBy(entry => entry.Header)
.Where(group => group.Count() > 1)
.Select(group => group.Key);
if (duplicates.Any())
throw new XFEDictionaryException("Header的值应唯一");
_xFEDictionaryList.AddRange(collection);
}
///
/// 追加字典
///
/// 字典字符串
public void AddRange(string dictionaryString)
{
_xFEDictionaryList.AddRange(ToList(dictionaryString));
}
///
/// 插入条目
///
/// 插入位置
/// 条目
///
public void Insert(int index, XFEEntry entry)
{
if (Contains(entry.Header))
throw new XFEDictionaryException("Header的值应唯一!");
_xFEDictionaryList.Insert(index, entry);
}
///
/// 插入条目
///
/// 插入位置
/// 条目
///
public void Insert(int index, string entryString)
{
var entry = XFEEntry.ToEntry(entryString);
if (entry is not null)
{
if (Contains(entry.Header))
throw new XFEDictionaryException("Header的值应唯一!");
_xFEDictionaryList.Insert(index, entry);
}
}
///
/// 插入条目
///
/// 插入位置
/// 头
/// 内容
///
public void Insert(int index, string header, string content)
{
if (Contains(header))
throw new XFEDictionaryException("Header的值应唯一!");
_xFEDictionaryList.Insert(index, new XFEEntry(header, content));
}
///
/// 清空字典
///
public void Clear()
{
_xFEDictionaryList.Clear();
}
///
/// 检测是否包含
///
/// 条目对象
///
public bool Contains(XFEEntry item)
{
return _xFEDictionaryList.Contains(item);
}
///
/// 检测是否包含头
///
/// 指定头
///
public bool Contains(string headerString)
{
return _xFEDictionaryList.Find(x => x.Header == headerString) is not null;
}
///
/// 检测是否包含内容
///
/// 指定内容
///
public bool ContainContent(string contentString)
{
return _xFEDictionaryList.Find(x => x.Content == contentString) is not null;
}
///
/// 复制
///
/// 条目数组
/// 索引
public void CopyTo(XFEEntry[] array, int arrayIndex)
{
_xFEDictionaryList.CopyTo(array, arrayIndex);
}
///
/// 获取索引器
///
///
public IEnumerator GetEnumerator()
{
return _xFEDictionaryList.GetEnumerator();
}
///
/// 移除指定的条目
///
/// 条目对象
///
public bool Remove(XFEEntry item)
{
return _xFEDictionaryList.Remove(item);
}
///
/// 移除指定的条目
///
/// 头
///
public bool Remove(string headerString)
{
var entry = _xFEDictionaryList.Find(x => x.Header == headerString);
if (entry is not null)
return _xFEDictionaryList.Remove(entry);
return false;
}
///
/// 移除指定范围的数组
///
/// 起始位置
/// 移除长度
public void RemoveRange(int startIndex, int count)
{
_xFEDictionaryList.RemoveRange(startIndex, count);
}
///
/// 索引器
///
/// 头
///
public string? this[string headerString]
{
get
{
return _xFEDictionaryList.Find(x => x.Header == headerString)?.Content;
}
set
{
if (value is not null)
{
var entry = _xFEDictionaryList.Find(x => x.Header == headerString);
if (entry is not null)
entry.Content = value;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return _xFEDictionaryList.GetEnumerator();
}
///
/// 转换为字符串
///
///
public override string ToString()
{
var result = "";
foreach (var item in _xFEDictionaryList)
{
result += item.ToString();
}
return result;
}
///
/// 将字符串转换为XFEEntry的List
///
/// 字符串
///
///
protected static List ToList(string dictionaryString)
{
List xFEList = [];
var messages = dictionaryString.Split(DictionarySeparator, StringSplitOptions.None);
foreach (var message in messages)
{
var xFEDictionaryString = message.Split(EntrySeparator, StringSplitOptions.None);
if (xFEDictionaryString.Length == 5)
{
xFEList.Add(new XFEEntry(xFEDictionaryString[1].Replace("[++", "[+").Replace("++]", "+]").Replace("|{++", "|{+").Replace("++}|", "+}|"), xFEDictionaryString[3].Replace("[++", "[+").Replace("++]", "+]").Replace("|{++", "|{+").Replace("++}|", "+}|")));
}
}
var duplicates = xFEList.GroupBy(entry => entry.Header)
.Where(group => group.Count() > 1)
.Select(group => group.Key);
if (duplicates.Any())
throw new XFEDictionaryException("Header的值应唯一");
return xFEList;
}
///
/// 将字符串转换为XFEDictionary
///
/// 字符串
///
///
public static XFEDictionary ToDictionary(string dictionaryString)
{
return new XFEDictionary(ToList(dictionaryString));
}
///
/// XFE字典存储
///
/// 条目列表
public XFEDictionary(List xFEDictionaryList)
{
this._xFEDictionaryList = xFEDictionaryList;
}
///
/// XFE字典存储
///
///
///
public XFEDictionary(params string[] headerAndContentStrings)
{
if (!(headerAndContentStrings.Length % 2 == 0))
throw new XFEDictionaryException("输入的字符串数组格式不正确,应为Header,Content,Header,Content...");
for (var i = 0; i < headerAndContentStrings.Length; i += 2)
{
_xFEDictionaryList.Add(new XFEEntry(headerAndContentStrings[i], headerAndContentStrings[i + 1]));
}
}
///
/// XFE字典存储
///
/// 字典字符串
public XFEDictionary(string dictionaryString)
{
_xFEDictionaryList = ToList(dictionaryString);
}
///
/// XFE字典存储
///
public XFEDictionary() { }
///
/// 从字符串加载字典
///
///
public static implicit operator XFEDictionary(string dictionaryString) => new(dictionaryString);
///
/// 将字典转为字符串
///
///
public static implicit operator string(XFEDictionary xFEEntries) => new(xFEEntries.ToString());
}