Skip to content

Commit

Permalink
PR feedback, make extensions generic
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Dec 2, 2024
1 parent 2e0f52d commit 8c8e21b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 51 deletions.
14 changes: 0 additions & 14 deletions src/Lucene.Net.Analysis.Common/Analysis/NGram/NGramTokenFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,6 @@ public int PositionIncrement
get => 0;
set => _ = value;
}

// LUCENENET specific - The interface requires this to be implemented, since we added it to avoid casts.
public void CopyTo(IAttribute target) => _ = target;

public void Clear()
{
}
}

private sealed class PositionLengthAttributeAnonymousClass : IPositionLengthAttribute
Expand All @@ -137,13 +130,6 @@ public int PositionLength
get => 0;
set => _ = value;
}

// LUCENENET specific - The interface requires this to be implemented, since we added it to avoid casts.
public void CopyTo(IAttribute target) => _ = target;

public void Clear()
{
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net/Analysis/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace Lucene.Net.Analysis
/// Failing that, to create a new <see cref="Token"/> you should first use
/// one of the constructors that starts with null text. To load
/// the token from a char[] use <see cref="ICharTermAttribute.CopyBuffer(char[], int, int)"/>.
/// To load from a <see cref="string"/> use <see cref="ICharTermAttribute.Clear()"/> (or <see cref="CharTermAttributeExtensions.SetEmpty(ICharTermAttribute)"/>) followed by
/// To load from a <see cref="string"/> use <see cref="ICharTermAttribute.Clear()"/> (or <see cref="CharTermAttributeExtensions.SetEmpty{T}(T)"/>) followed by
/// <see cref="ICharTermAttribute.Append(string)"/> or <see cref="ICharTermAttribute.Append(string, int, int)"/>.
/// Alternatively you can get the <see cref="Token"/>'s termBuffer by calling either <see cref="ICharTermAttribute.Buffer"/>,
/// if you know that your text is shorter than the capacity of the termBuffer
Expand Down
24 changes: 11 additions & 13 deletions src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,22 @@ public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
/// LUCENENET: To mimic StringBuilder, we allow this to be settable.
/// This replaces the chainable SetLength method in the Java version.
/// </remarks>
/// <seealso cref="CharTermAttributeExtensions.SetLength(ICharTermAttribute, int)"/>
/// <seealso cref="CharTermAttributeExtensions.SetLength{T}(T, int)"/>
new int Length { get; set; }

// LUCENENET specific: Redefining this[] to make it settable
new char this[int index] { get; set; }

// the following methods are redefined to get rid of IOException declaration:
/// <summary>
/// Clears the values in this attribute and resets it to its
/// default value.
/// </summary>
/// <remarks>
/// LUCENENET specific - This method is not part of the Java Lucene API.
/// This was added to be a more consistent way to clear attributes than SetEmpty().
/// </remarks>
/// <seealso cref="CharTermAttributeExtensions.SetEmpty{T}(T)"/>
void Clear();

/// <summary>
/// Appends the contents of the <see cref="ICharSequence"/> to this character sequence.
Expand Down Expand Up @@ -272,16 +281,5 @@ public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
/// </summary>
/// <param name="value">The sequence of characters to append.</param>
ICharTermAttribute Append(ICharTermAttribute value);

/// <summary>
/// Clears the values in this attribute and resets it to its
/// default value.
/// </summary>
/// <remarks>
/// LUCENENET specific - This method is not part of the Java Lucene API.
/// This was added to be a more consistent way to clear attributes than SetEmpty().
/// </remarks>
/// <seealso cref="CharTermAttributeExtensions.SetEmpty(ICharTermAttribute)"/>
void Clear();
}
}
22 changes: 1 addition & 21 deletions src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ public void CopyBuffer(char[] buffer, int offset, int length)
termLength = length;
}

char[] ICharTermAttribute.Buffer => termBuffer;

[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public char[] Buffer => termBuffer;
Expand Down Expand Up @@ -107,14 +105,6 @@ private void GrowTermBuffer(int newSize)
}
}

int ICharTermAttribute.Length
{
get => Length;
set => Length = value;
}

int ICharSequence.Length => Length;

public int Length
{
get => termLength;
Expand All @@ -124,7 +114,7 @@ public int Length
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(value)} must not be negative.");
if (value > termBuffer.Length)
throw new ArgumentOutOfRangeException(nameof(value), value, "length " + value + " exceeds the size of the termBuffer (" + termBuffer.Length + ")");
throw new ArgumentOutOfRangeException(nameof(value), value, $"length {value} exceeds the size of the termBuffer ({termBuffer.Length})");

termLength = value;
}
Expand All @@ -142,12 +132,6 @@ public virtual void FillBytesRef()

// *** CharSequence interface ***

// LUCENENET specific: Replaced CharAt(int) with this[int] to .NETify

char ICharSequence.this[int index] => this[index];

char ICharTermAttribute.this[int index] { get => this[index]; set => this[index] = value; }

// LUCENENET specific indexer to make CharTermAttribute act more like a .NET type
public char this[int index]
{
Expand Down Expand Up @@ -480,10 +464,6 @@ public override void CopyTo(IAttribute target) // LUCENENET specific - intention

#region ICharTermAttribute Members

void ICharTermAttribute.CopyBuffer(char[] buffer, int offset, int length) => CopyBuffer(buffer, offset, length);

char[] ICharTermAttribute.ResizeBuffer(int newSize) => ResizeBuffer(newSize);

ICharTermAttribute ICharTermAttribute.Append(ICharSequence value) => Append(value);

ICharTermAttribute ICharTermAttribute.Append(ICharSequence value, int startIndex, int count) => Append(value, startIndex, count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public static class CharTermAttributeExtensions
/// </code>
/// </summary>
/// <param name="length">The truncated length</param>
public static ICharTermAttribute SetLength(this ICharTermAttribute termAttr, int length)
public static T SetLength<T>(this T termAttr, int length)
where T : ICharTermAttribute
{
if (termAttr is null)
{
Expand All @@ -59,7 +60,8 @@ public static ICharTermAttribute SetLength(this ICharTermAttribute termAttr, int
/// obj.SetEmpty().Append("hey you");
/// </code>
/// </summary>
public static ICharTermAttribute SetEmpty(this ICharTermAttribute termAttr)
public static T SetEmpty<T>(this T termAttr)
where T : ICharTermAttribute
{
if (termAttr is null)
{
Expand Down

0 comments on commit 8c8e21b

Please sign in to comment.