PageRenderTime 29ms CodeModel.GetById 21ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/StateCollection.cs

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