Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

Commit

Permalink
restored equality members
Browse files Browse the repository at this point in the history
  • Loading branch information
pomma89 committed Jun 24, 2017
1 parent 5a1af7e commit d3aa20b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,9 @@ pip-log.txt

#Mr Developer
.mr.developer.cfg

#############
## Rider
#############

.idea
63 changes: 62 additions & 1 deletion src/CodeProject.ObjectPool/Core/PooledObjectInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Core information about a specific <see cref="PooledObject"/>.
/// </summary>
public sealed class PooledObjectInfo
public sealed class PooledObjectInfo : IEquatable<PooledObjectInfo>
{
/// <summary>
/// An identifier which is unique inside the pool to which this object belongs. Moreover,
Expand All @@ -50,5 +52,64 @@ public sealed class PooledObjectInfo
/// that object to re-add itself back to the pool.
/// </summary>
internal IObjectPoolHandle Handle { get; set; }

/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString() => $"{nameof(Id)}: {Id}, {nameof(Payload)}: {Payload}";

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter;
/// otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public bool Equals(PooledObjectInfo other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Id == other.Id;
}

/// <summary>
/// Determines whether the specified <see cref="object"/> is equal to the current <see cref="PooledObjectInfo"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="object"/> is equal to the current
/// <see cref="PooledObjectInfo"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="object"/> to compare with the current <see cref="PooledObjectInfo"/>.</param>
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);
}

/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>A hash code for the current <see cref="PooledObjectInfo"/>.</returns>
public override int GetHashCode() => Id;

/// <summary>
/// Compares to pooled objects info by <see cref="Id"/>.
/// </summary>
/// <param name="left">Left object.</param>
/// <param name="right">Right object.</param>
/// <returns>True if given pooled objects info are equal, false otherwise.</returns>
public static bool operator ==(PooledObjectInfo left, PooledObjectInfo right) => Equals(left, right);

/// <summary>
/// Compares to pooled objects info by <see cref="Id"/>.
/// </summary>
/// <param name="left">Left object.</param>
/// <param name="right">Right object.</param>
/// <returns>True if given pooled objects info are not equal, false otherwise.</returns>
public static bool operator !=(PooledObjectInfo left, PooledObjectInfo right) => !Equals(left, right);
}
}
87 changes: 55 additions & 32 deletions src/CodeProject.ObjectPool/PooledObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,41 +192,64 @@ private void HandleReAddingToPool(bool reRegisterForFinalization)

#region Formatting and equality

///// <summary>
///// Returns all property (or field) values, along with their names, so that they can be
///// used to produce a meaningful <see cref="object.ToString"/>.
///// </summary>
///// <returns>
///// All property (or field) values, along with their names, so that they can be used to
///// produce a meaningful <see cref="object.ToString"/>.
///// </returns>
//protected override IEnumerable<KeyValuePair<string, object>> GetFormattingMembers()
//{
// yield return new KeyValuePair<string, object>(nameof(PooledObjectInfo.Id), PooledObjectInfo.Id);
// if (PooledObjectInfo.Payload != null)
// {
// yield return new KeyValuePair<string, object>(nameof(PooledObjectInfo.Payload), PooledObjectInfo.Payload);
// }
//}

///// <summary>
///// Returns all property (or field) values that should be used inside
///// <see cref="IEquatable{T}.Equals(T)"/> or <see cref="object.GetHashCode"/>.
///// </summary>
///// <returns>
///// All property (or field) values that should be used inside
///// <see cref="IEquatable{T}.Equals(T)"/> or <see cref="object.GetHashCode"/>.
///// </returns>
//protected override IEnumerable<object> GetIdentifyingMembers()
//{
// yield return PooledObjectInfo.Id;
//}

public bool Equals(PooledObject other)
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
/// <returns>A string that represents the current object.</returns>
public override string ToString() => PooledObjectInfo.ToString();

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter;
/// otherwise, false.
/// </returns>
/// <param name="other">An object to compare with this object.</param>
public virtual bool Equals(PooledObject other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return PooledObjectInfo.Equals(other.PooledObjectInfo);
}

/// <summary>
/// Determines whether the specified <see cref="object"/> is equal to the current <see cref="PooledObject"/>.
/// </summary>
/// <returns>
/// true if the specified <see cref="object"/> is equal to the current
/// <see cref="PooledObject"/>; otherwise, false.
/// </returns>
/// <param name="obj">The <see cref="object"/> to compare with the current <see cref="PooledObject"/>.</param>
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);
}

/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>A hash code for the current <see cref="PooledObject"/>.</returns>
public override int GetHashCode() => PooledObjectInfo.GetHashCode();

/// <summary>
/// Compares to pooled objects.
/// </summary>
/// <param name="left">Left object.</param>
/// <param name="right">Right object.</param>
/// <returns>True if given pooled objects are equal, false otherwise.</returns>
public static bool operator ==(PooledObject left, PooledObject right) => Equals(left, right);

/// <summary>
/// Compares to pooled objects.
/// </summary>
/// <param name="left">Left object.</param>
/// <param name="right">Right object.</param>
/// <returns>True if given pooled objects are not equal, false otherwise.</returns>
public static bool operator !=(PooledObject left, PooledObject right) => !Equals(left, right);

#endregion Formatting and equality
}
}

0 comments on commit d3aa20b

Please sign in to comment.