using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Masuit.Tools.Mapping.Core
{
///
/// 单例存储映射器。
///
/// 不需要单例,因为适用于所有线程。
public class MapperConfigurationCollectionContainer : IEnumerable
{
private readonly HashSet _items;
private static MapperConfigurationCollectionContainer currentInstance;
private MapperConfigurationCollectionContainer()
{
_items = new HashSet();
}
///
/// mapper映射容器
///
public static MapperConfigurationCollectionContainer Instance => currentInstance ??= new MapperConfigurationCollectionContainer();
///
/// count
///
public int Count => _items.Count;
///
/// 索引器
///
///
///
internal MapperConfigurationBase this[int index]
{
get
{
if (index > _items.Count)
throw new IndexOutOfRangeException();
var enumerator = GetEnumerator();
int i = 0;
while (enumerator.MoveNext())
{
if (i == index)
{
return enumerator.Current;
}
i++;
}
return null;
}
}
///
/// 查找指定的源。
///
/// 源类型
/// 目标对象
/// 别名
internal MapperConfigurationBase Find(Type source, Type target, string name = null)
{
foreach (var current in this)
{
string nameMapper = string.IsNullOrEmpty(name) ? current.paramClassSource.Name : name;
if (current.SourceType == source && current.TargetType == target && current.Name == nameMapper)
{
return current;
}
}
return null;
}
///
/// 是否存在谓词的映射。
///
/// 条件表达式
public bool Exists(Func match)
{
return this.Any(match);
}
///
/// 移除指定的元素
///
///
public void RemoveAt(int index)
{
MapperConfigurationBase itemToDelete = this[index];
if (itemToDelete != null)
{
_items.Remove(itemToDelete);
}
}
///
/// 清除
///
public void Clear()
{
_items.Clear();
}
///
/// 添加
///
///
public void Add(MapperConfigurationBase value)
{
_items.Add(value);
}
///
/// 迭代器
///
///
public IEnumerator GetEnumerator()
{
return _items.GetEnumerator();
}
///
/// 迭代器
///
///
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}