using System; using System.Collections.Generic; using System.Collections.ObjectModel; namespace Update.Core.Entities { /// /// 全量更新包集合 /// public class FullPackageCollection : KeyedCollection { /// /// 构造函数 /// public FullPackageCollection() { } /// /// 构造函数 /// /// 元素集合 public FullPackageCollection(IEnumerable items) { this.AddRange(items); } /// /// 获取主键 /// /// 元素 /// 主键 protected override Version GetKeyForItem(FullPackage item) { return item.To; } /// /// 插入元素 /// /// 索引 /// 元素 protected override void InsertItem(int index, FullPackage item) { try { base.InsertItem(index, item); } catch { throw new Exception(string.Format("全量更新包冲突 {0}。", item.FileName)); } } /// /// 添加集合 /// /// 元素集合 public void AddRange(IEnumerable range) { foreach (FullPackage item in range) base.Add(item); } /// /// 获取指定主键元素 /// /// 主键 /// 获取到的元素 /// 包含返回true,否则返回false public bool TryGetValue(Version key, out FullPackage item) { var dictionary = this.Dictionary; if (dictionary == null) { var comparer = this.Comparer; foreach (var value in this.Items) { if (comparer.Equals(this.GetKeyForItem(value), key)) { item = value; return true; } } item = null; return false; } return dictionary.TryGetValue(key, out item); } } }