From 07c9f06f753a4db79e5e9f02b61fd8d2040b191a Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Fri, 2 May 2014 17:59:26 +0200 Subject: [PATCH] add IComparable and tostring/gethashcode to PocketItem --- PocketSharp/Models/PocketItem.cs | 112 ++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) diff --git a/PocketSharp/Models/PocketItem.cs b/PocketSharp/Models/PocketItem.cs index 6c243c3..197c664 100644 --- a/PocketSharp/Models/PocketItem.cs +++ b/PocketSharp/Models/PocketItem.cs @@ -14,8 +14,7 @@ namespace PocketSharp.Models [JsonObject] [ImplementPropertyChanged] [DebuggerDisplay("Uri = {Uri}, Title = {Title}")] - - public class PocketItem + public class PocketItem : IComparable { /// /// Gets or sets the ID. @@ -374,5 +373,114 @@ namespace PocketSharp.Models /// [JsonIgnore] public string Json { get; set; } + + /// + /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. + /// + /// An object to compare with this instance. + /// + /// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes in the sort order. Zero This instance occurs in the same position in the sort order as . Greater than zero This instance follows in the sort order. + /// + int IComparable.CompareTo(object obj) + { + PocketItem item = (PocketItem)obj; + + if (!AddTime.HasValue) + { + return 1; + } + if (!item.AddTime.HasValue) + { + return -1; + } + + return DateTime.Compare(AddTime.Value, item.AddTime.Value); + } + + /// + /// Determines whether the specified , is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(object obj) + { + if (obj == null) + { + return false; + } + + PocketItem item = (PocketItem)obj; + + if ((Object)item == null) + { + return false; + } + + return ID == item.ID; + } + + + /// + /// Implements the operator ==. + /// + /// A. + /// The b. + /// + /// The result of the operator. + /// + public static bool operator ==(PocketItem a, PocketItem b) + { + if (a == null || b == null) + { + return false; + } + + PocketItem itemA = (PocketItem)a; + PocketItem itemB = (PocketItem)b; + + if ((Object)itemA == null || (Object)itemB == null) + { + return false; + } + + return itemA.ID == itemB.ID; + } + + /// + /// Implements the operator !=. + /// + /// A. + /// The b. + /// + /// The result of the operator. + /// + public static bool operator !=(PocketItem a, PocketItem b) + { + return !(a == b); + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + public override int GetHashCode() + { + return ID.GetHashCode(); + } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override string ToString() + { + return ID; + } } }