diff --git a/Lucene.Net.sln.DotSettings b/Lucene.Net.sln.DotSettings index 6fd109800d..dae1019ebe 100644 --- a/Lucene.Net.sln.DotSettings +++ b/Lucene.Net.sln.DotSettings @@ -1,4 +1,5 @@  True + True True True \ No newline at end of file diff --git a/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs b/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs index 5791fd3b74..b15aac7223 100644 --- a/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs +++ b/src/Lucene.Net.Benchmark/Quality/QualityQuery.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Globalization; +#nullable enable namespace Lucene.Net.Benchmarks.Quality { @@ -49,8 +50,8 @@ public class QualityQuery : IComparable /// The contents of this quality query. public QualityQuery(string queryID, IDictionary nameValPairs) { - this.queryID = queryID; - this.nameValPairs = nameValPairs; + this.queryID = queryID ?? throw new ArgumentNullException(nameof(queryID)); + this.nameValPairs = nameValPairs ?? throw new ArgumentNullException(nameof(nameValPairs)); } /// @@ -66,10 +67,9 @@ public virtual string[] GetNames() /// /// The name whose value should be returned. /// - public virtual string GetValue(string name) + public virtual string? GetValue(string name) { - nameValPairs.TryGetValue(name, out string result); - return result; + return nameValPairs.TryGetValue(name, out string? result) ? result : null; } /// @@ -82,43 +82,57 @@ public virtual string GetValue(string name) /// For a nicer sort of input queries before running them. /// Try first as ints, fall back to string if not int. /// - /// - /// - public virtual int CompareTo(QualityQuery other) + /// The other to compare to. + /// 0 if equal, a negative value if smaller, a positive value if larger. + public virtual int CompareTo(QualityQuery? other) { - try + if (other is null) { - // compare as ints when ids ints - int n = int.Parse(queryID, CultureInfo.InvariantCulture); - int nOther = int.Parse(other.queryID, CultureInfo.InvariantCulture); - return n - nOther; + return 1; } - catch (Exception e) when (e.IsNumberFormatException()) + + if (int.TryParse(queryID, NumberStyles.Integer, CultureInfo.InvariantCulture, out int n) + && int.TryParse(other.queryID, NumberStyles.Integer, CultureInfo.InvariantCulture, out int nOther)) { - // fall back to string comparison - return queryID.CompareToOrdinal(other.queryID); + return n - nOther; } + + // fall back to string comparison + return queryID.CompareToOrdinal(other.queryID); } + // LUCENENET specific - provide Equals and GetHashCode due to providing operator overrides + protected bool Equals(QualityQuery? other) => queryID == other?.queryID; + + public override bool Equals(object? obj) + { + if (obj is null) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != GetType()) return false; + return Equals((QualityQuery)obj); + } + + public override int GetHashCode() => queryID.GetHashCode(); + #region Operator overrides // LUCENENET specific - per csharpsquid:S1210, IComparable should override comparison operators - public static bool operator <(QualityQuery left, QualityQuery right) + public static bool operator <(QualityQuery? left, QualityQuery? right) => left is null ? right is not null : left.CompareTo(right) < 0; - public static bool operator <=(QualityQuery left, QualityQuery right) + public static bool operator <=(QualityQuery? left, QualityQuery? right) => left is null || left.CompareTo(right) <= 0; - public static bool operator >(QualityQuery left, QualityQuery right) + public static bool operator >(QualityQuery? left, QualityQuery? right) => left is not null && left.CompareTo(right) > 0; - public static bool operator >=(QualityQuery left, QualityQuery right) + public static bool operator >=(QualityQuery? left, QualityQuery? right) => left is null ? right is null : left.CompareTo(right) >= 0; - public static bool operator ==(QualityQuery left, QualityQuery right) + public static bool operator ==(QualityQuery? left, QualityQuery? right) => left?.Equals(right) ?? right is null; - public static bool operator !=(QualityQuery left, QualityQuery right) + public static bool operator !=(QualityQuery? left, QualityQuery? right) => !(left == right); #endregion