-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add operator overrides for public IComparable types, #683 #1056
base: master
Are you sure you want to change the base?
Conversation
This change caught a NRE bug in MutableValue 😄 Fixed. |
Well, being that they didn't override the default behavior (which is reference equality), a direct port would be to override each and then cascade the call to the base class. But given the fact that the comparer uses BTW - I noticed that the public virtual int CompareTo(QualityQuery other)
{
try
{
// compare as ints when ids ints
int n = int.Parse(queryID, CultureInfo.InvariantCulture);
int nOther = int.Parse(other.queryID, CultureInfo.InvariantCulture);
return n - nOther;
}
catch (Exception e) when (e.IsNumberFormatException())
{
// fall back to string comparison
return queryID.CompareToOrdinal(other.queryID);
}
} The constructor could also be improved by using guard clauses on
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR.
We are getting some build warnings in some types where Equals(object)
and GetHashCode()
have not been implemented.
Also, since these are all on nullable types, please use #nullable enable
within the #region Operator overrides
sections and #nullable restore
prior to #endregion
. We don't necessarily have to do this on the entire file in this PR.
See the inline comments.
@@ -470,6 +470,31 @@ public override bool Equals(object obj) | |||
return true; | |||
} | |||
|
|||
#region Operator overrides | |||
#nullable enable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, Visual Studio left aligns #nullable enable
and #nullable restore
, so please do that in each case in this PR.
@@ -2,6 +2,7 @@ | |||
using Lucene.Net.Index; | |||
using System; | |||
using System.Text; | |||
#pragma warning disable CS0660, CS0661 - CompareTo is deprecated, so skipping implementing equality members (lucenenet#683) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the inline comment by preceeding with //
.
|
||
public override int GetHashCode() | ||
{ | ||
unchecked |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please cascade the call to the base class.
public override int GetHashCode() => base.GetHashCode();
@@ -67,6 +67,42 @@ public int CompareTo(Completion o) | |||
{ | |||
return this.Utf8.CompareTo(o.Utf8); | |||
} | |||
|
|||
// LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode | |||
public override bool Equals(object obj) => ReferenceEquals(this, obj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please cascade the call to the base class.
public override bool Equals(object obj) => base.Equals(obj);
@@ -122,14 +122,53 @@ public int CompareTo(LookupResult o) | |||
{ | |||
return CHARSEQUENCE_COMPARER.Compare(Key, o.Key); | |||
} | |||
|
|||
// LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode | |||
public override bool Equals(object obj) => ReferenceEquals(this, obj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please cascade the call to the base class.
public override bool Equals(object obj) => base.Equals(obj);
// LUCENENET specific - per CS0660 and CS0661, we need to override Equals and GetHashCode | ||
public override bool Equals(object obj) => ReferenceEquals(this, obj); | ||
|
||
public override int GetHashCode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please cascade the call to the base class.
public override int GetHashCode() => base.GetHashCode();
// Although it doesn't match GetHashCode(), checking for | ||
// reference equality is by design. | ||
// Implementing Equals() causes difficult to diagnose | ||
// IndexOutOfRangeExceptions when using FuzzyTermsEnum. | ||
// See GH-296. | ||
// Overriding here to prevent CS0660 warning due to defining the == operator. | ||
public override bool Equals(object obj) => ReferenceEquals(this, obj); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please cascade the call to the base class.
public override bool Equals(object obj) => base.Equals(obj);
Add operator overrides for public IComparable types
Fixes #683
Description
This is to resolve the Sonar warning about needing to override the comparison operators for any types that implement IComparable. We are only doing this here for the public types.
QualityQuery was missing Equals and GetHashCode, as well as had some possible NullReferenceExceptions, so those have been fixed as well.