PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/StateCollection.cs

#
C# | 128 lines | 46 code | 17 blank | 65 comment | 6 complexity | 5cedf7c4d6a4e3809934e0f614d29552 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  1. namespace BlogEngine.Core
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. /// <summary>
  7. /// A generic collection with the ability to
  8. /// check if it has been changed.
  9. /// </summary>
  10. /// <typeparam name="T">
  11. /// The type of the list elements.
  12. /// </typeparam>
  13. [Serializable]
  14. public class StateList<T> : List<T>
  15. {
  16. ///// <summary>
  17. ///// Inserts an element into the collection at the specified index and marks it changed.
  18. ///// </summary>
  19. // protected override void InsertItem(int index, T item)
  20. // {
  21. // base.InsertItem(index, item);
  22. // _IsChanged = true;
  23. // }
  24. ///// <summary>
  25. ///// Removes all the items in the collection and marks it changed.
  26. ///// </summary>
  27. // protected override void ClearItems()
  28. // {
  29. // base.ClearItems();
  30. // _IsChanged = true;
  31. // }
  32. ///// <summary>
  33. ///// Removes the element at the specified index and marks the collection changed.
  34. ///// </summary>
  35. // protected override void RemoveItem(int index)
  36. // {
  37. // base.RemoveItem(index);
  38. // _IsChanged = true;
  39. // }
  40. ///// <summary>
  41. ///// Replaces the element at the specified index and marks the collection changed.
  42. ///// </summary>
  43. // protected override void SetItem(int index, T item)
  44. // {
  45. // base.SetItem(index, item);
  46. // _IsChanged = true;
  47. // }
  48. #region Constants and Fields
  49. /// <summary>
  50. /// Has code int.
  51. /// </summary>
  52. private int hasCode;
  53. #endregion
  54. #region Properties
  55. /// <summary>
  56. /// Gets a value indicating whether this object's data has been changed.
  57. /// </summary>
  58. /// <returns>A value indicating if this object's data has been changed.</returns>
  59. public virtual bool IsChanged
  60. {
  61. get
  62. {
  63. return this.GetHashCode() != this.hasCode;
  64. }
  65. }
  66. #endregion
  67. #region Public Methods
  68. /// <summary>
  69. /// Determines whether the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>.
  70. /// </summary>
  71. /// <param name="obj">
  72. /// The <see cref="T:System.Object"></see> to compare with the current <see cref="T:System.Object"></see>.
  73. /// </param>
  74. /// <returns>
  75. /// true if the specified <see cref="T:System.Object"></see> is equal to the current <see cref="T:System.Object"></see>; otherwise, false.
  76. /// </returns>
  77. public override bool Equals(object obj)
  78. {
  79. if (obj == null)
  80. {
  81. return false;
  82. }
  83. if (obj.GetType() == this.GetType())
  84. {
  85. return obj.GetHashCode() == this.GetHashCode();
  86. }
  87. return false;
  88. }
  89. /// <summary>
  90. /// Serves as a hash function for a particular type. <see cref="M:System.Object.GetHashCode"></see> is suitable for use in hashing algorithms and data structures like a hash table.
  91. /// </summary>
  92. /// <returns>
  93. /// A hash code for the current <see cref="T:System.Object"></see>.
  94. /// </returns>
  95. public override int GetHashCode()
  96. {
  97. var hash = this.Aggregate(string.Empty, (current, item) => current + item.GetHashCode().ToString());
  98. return hash.GetHashCode();
  99. }
  100. /// <summary>
  101. /// Marks the object as being clean,
  102. /// which means not changed.
  103. /// </summary>
  104. public virtual void MarkOld()
  105. {
  106. this.hasCode = this.GetHashCode();
  107. this.TrimExcess();
  108. }
  109. #endregion
  110. }
  111. }