DiffPackageCollection.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. namespace Update.Core.Entities
  5. {
  6. /// <summary>
  7. /// 增量更新包集合
  8. /// </summary>
  9. public class DiffPackageCollection : KeyedCollection<Version, DiffPackage>
  10. {
  11. /// <summary>
  12. /// 构造函数
  13. /// </summary>
  14. public DiffPackageCollection()
  15. {
  16. }
  17. /// <summary>
  18. /// 构造函数
  19. /// </summary>
  20. /// <param name="items">元素集合</param>
  21. public DiffPackageCollection(IEnumerable<DiffPackage> items)
  22. {
  23. this.AddRange(items);
  24. }
  25. /// <summary>
  26. /// 获取主键
  27. /// </summary>
  28. /// <param name="item">元素</param>
  29. /// <returns>主键</returns>
  30. protected override Version GetKeyForItem(DiffPackage item)
  31. {
  32. return item.From;
  33. }
  34. /// <summary>
  35. /// 插入元素
  36. /// </summary>
  37. /// <param name="index">索引</param>
  38. /// <param name="item">元素</param>
  39. protected override void InsertItem(int index, DiffPackage item)
  40. {
  41. try
  42. {
  43. base.InsertItem(index, item);
  44. }
  45. catch
  46. {
  47. throw new Exception(string.Format("增量更新包冲突 {0}。", item.FileName));
  48. }
  49. }
  50. /// <summary>
  51. /// 添加集合
  52. /// </summary>
  53. /// <param name="range">元素集合</param>
  54. public void AddRange(IEnumerable<DiffPackage> range)
  55. {
  56. foreach (DiffPackage item in range)
  57. base.Add(item);
  58. }
  59. /// <summary>
  60. /// 获取指定主键元素
  61. /// </summary>
  62. /// <param name="key">主键</param>
  63. /// <param name="item">获取到的元素</param>
  64. /// <returns>包含返回true,否则返回false</returns>
  65. public bool TryGetValue(Version key, out DiffPackage item)
  66. {
  67. var dictionary = this.Dictionary;
  68. if (dictionary == null)
  69. {
  70. var comparer = this.Comparer;
  71. foreach (var value in this.Items)
  72. {
  73. if (comparer.Equals(this.GetKeyForItem(value), key))
  74. {
  75. item = value;
  76. return true;
  77. }
  78. }
  79. item = null;
  80. return false;
  81. }
  82. return dictionary.TryGetValue(key, out item);
  83. }
  84. }
  85. }