From d3aa20b03cfec31ab8db9b85eb0d357dbc36cd46 Mon Sep 17 00:00:00 2001 From: Alessio Parma Date: Sat, 24 Jun 2017 09:30:46 +0200 Subject: [PATCH] restored equality members --- .gitignore | 6 ++ .../Core/PooledObjectInfo.cs | 63 +++++++++++++- src/CodeProject.ObjectPool/PooledObject.cs | 87 ++++++++++++------- 3 files changed, 123 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 6ea4e1a..224ca86 100644 --- a/.gitignore +++ b/.gitignore @@ -215,3 +215,9 @@ pip-log.txt #Mr Developer .mr.developer.cfg + +############# +## Rider +############# + +.idea diff --git a/src/CodeProject.ObjectPool/Core/PooledObjectInfo.cs b/src/CodeProject.ObjectPool/Core/PooledObjectInfo.cs index 6518559..6dd6e7c 100644 --- a/src/CodeProject.ObjectPool/Core/PooledObjectInfo.cs +++ b/src/CodeProject.ObjectPool/Core/PooledObjectInfo.cs @@ -21,12 +21,14 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT // OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +using System; + namespace CodeProject.ObjectPool.Core { /// /// Core information about a specific . /// - public sealed class PooledObjectInfo + public sealed class PooledObjectInfo : IEquatable { /// /// An identifier which is unique inside the pool to which this object belongs. Moreover, @@ -50,5 +52,64 @@ public sealed class PooledObjectInfo /// that object to re-add itself back to the pool. /// internal IObjectPoolHandle Handle { get; set; } + + /// + /// Returns a string that represents the current object. + /// + /// A string that represents the current object. + public override string ToString() => $"{nameof(Id)}: {Id}, {nameof(Payload)}: {Payload}"; + + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// + /// true if the current object is equal to the parameter; + /// otherwise, false. + /// + /// An object to compare with this object. + public bool Equals(PooledObjectInfo other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Id == other.Id; + } + + /// + /// Determines whether the specified is equal to the current . + /// + /// + /// true if the specified is equal to the current + /// ; otherwise, false. + /// + /// The to compare with the current . + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + var info = obj as PooledObjectInfo; + return info != null && Equals(info); + } + + /// + /// Serves as a hash function for a particular type. + /// + /// A hash code for the current . + public override int GetHashCode() => Id; + + /// + /// Compares to pooled objects info by . + /// + /// Left object. + /// Right object. + /// True if given pooled objects info are equal, false otherwise. + public static bool operator ==(PooledObjectInfo left, PooledObjectInfo right) => Equals(left, right); + + /// + /// Compares to pooled objects info by . + /// + /// Left object. + /// Right object. + /// True if given pooled objects info are not equal, false otherwise. + public static bool operator !=(PooledObjectInfo left, PooledObjectInfo right) => !Equals(left, right); } } \ No newline at end of file diff --git a/src/CodeProject.ObjectPool/PooledObject.cs b/src/CodeProject.ObjectPool/PooledObject.cs index b0d57b3..cc800da 100644 --- a/src/CodeProject.ObjectPool/PooledObject.cs +++ b/src/CodeProject.ObjectPool/PooledObject.cs @@ -192,41 +192,64 @@ private void HandleReAddingToPool(bool reRegisterForFinalization) #region Formatting and equality - ///// - ///// Returns all property (or field) values, along with their names, so that they can be - ///// used to produce a meaningful . - ///// - ///// - ///// All property (or field) values, along with their names, so that they can be used to - ///// produce a meaningful . - ///// - //protected override IEnumerable> GetFormattingMembers() - //{ - // yield return new KeyValuePair(nameof(PooledObjectInfo.Id), PooledObjectInfo.Id); - // if (PooledObjectInfo.Payload != null) - // { - // yield return new KeyValuePair(nameof(PooledObjectInfo.Payload), PooledObjectInfo.Payload); - // } - //} - - ///// - ///// Returns all property (or field) values that should be used inside - ///// or . - ///// - ///// - ///// All property (or field) values that should be used inside - ///// or . - ///// - //protected override IEnumerable GetIdentifyingMembers() - //{ - // yield return PooledObjectInfo.Id; - //} - - public bool Equals(PooledObject other) + /// + /// Returns a string that represents the current object. + /// + /// A string that represents the current object. + public override string ToString() => PooledObjectInfo.ToString(); + + /// + /// Indicates whether the current object is equal to another object of the same type. + /// + /// + /// true if the current object is equal to the parameter; + /// otherwise, false. + /// + /// An object to compare with this object. + public virtual bool Equals(PooledObject other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return PooledObjectInfo.Equals(other.PooledObjectInfo); + } + + /// + /// Determines whether the specified is equal to the current . + /// + /// + /// true if the specified is equal to the current + /// ; otherwise, false. + /// + /// The to compare with the current . + public override bool Equals(object obj) { - throw new NotImplementedException(); + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj.GetType() == GetType() && Equals(obj as PooledObject); } + /// + /// Serves as a hash function for a particular type. + /// + /// A hash code for the current . + public override int GetHashCode() => PooledObjectInfo.GetHashCode(); + + /// + /// Compares to pooled objects. + /// + /// Left object. + /// Right object. + /// True if given pooled objects are equal, false otherwise. + public static bool operator ==(PooledObject left, PooledObject right) => Equals(left, right); + + /// + /// Compares to pooled objects. + /// + /// Left object. + /// Right object. + /// True if given pooled objects are not equal, false otherwise. + public static bool operator !=(PooledObject left, PooledObject right) => !Equals(left, right); + #endregion Formatting and equality } } \ No newline at end of file