Skip to content

Commit

Permalink
Add Equals and GetHashCode to QualityQuery, enavle nullable, clean up…
Browse files Browse the repository at this point in the history
… CompareTo implementation
  • Loading branch information
paulirwin committed Dec 4, 2024
1 parent 95399b4 commit 53527b5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
1 change: 1 addition & 0 deletions Lucene.Net.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Coord/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=csharpsquid/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=LUCENENET/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=testsettings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
58 changes: 36 additions & 22 deletions src/Lucene.Net.Benchmark/Quality/QualityQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
#nullable enable

namespace Lucene.Net.Benchmarks.Quality
{
Expand Down Expand Up @@ -49,8 +50,8 @@ public class QualityQuery : IComparable<QualityQuery>
/// <param name="nameValPairs">The contents of this quality query.</param>
public QualityQuery(string queryID, IDictionary<string, string> nameValPairs)
{
this.queryID = queryID;
this.nameValPairs = nameValPairs;
this.queryID = queryID ?? throw new ArgumentNullException(nameof(queryID));
this.nameValPairs = nameValPairs ?? throw new ArgumentNullException(nameof(nameValPairs));
}

/// <summary>
Expand All @@ -66,10 +67,9 @@ public virtual string[] GetNames()
/// </summary>
/// <param name="name">The name whose value should be returned.</param>
/// <returns></returns>
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;
}

/// <summary>
Expand All @@ -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.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public virtual int CompareTo(QualityQuery other)
/// <param name="other">The other <see cref="QualityQuery"/> to compare to.</param>
/// <returns>0 if equal, a negative value if smaller, a positive value if larger.</returns>
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<T> 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
Expand Down

0 comments on commit 53527b5

Please sign in to comment.