You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucenenet.apache.org by "paulirwin (via GitHub)" <gi...@apache.org> on 2024/11/26 04:03:19 UTC

[PR] Remove SetLength from ICharTermAttribute, #1038 [lucenenet]

paulirwin opened a new pull request, #1049:
URL: https://github.com/apache/lucenenet/pull/1049

   - [X] You've read the [Contributor Guide](https://github.com/apache/lucenenet/blob/main/CONTRIBUTING.md) and [Code of Conduct](https://www.apache.org/foundation/policies/conduct.html).
   - [ ] You've included unit or integration tests for your change, where applicable.
   - [X] You've included inline docs for your change, where applicable.
   - [X] There's an open issue for the PR that you are making. If you'd like to propose a change, please [open an issue](https://github.com/apache/lucenenet/issues/new/choose) to discuss the change or find an existing issue.
   
   Remove SetLength from ICharTermAttribute
   
   Fixes #1038
   
   ## Description
   
   See the rationale in #1038.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Remove SetLength and SetEmpty from ICharTermAttribute, #1038 [lucenenet]

Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1866097331


##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs:
##########
@@ -107,32 +107,27 @@ private void GrowTermBuffer(int newSize)
             }
         }
 
-        int ICharTermAttribute.Length { get => Length; set => SetLength(value); }
+        int ICharTermAttribute.Length

Review Comment:
   It appears that also the explicit implementations for `ICharTermAttribute.Buffer`, `ICharSequence.this[int index]`, `ICharTermAttribute.this[int index]`, `ICharTermAttribute.CopyBuffer`, and `ICharTermAttribute.ResizeBuffer` are not needed either (i.e. for covariant return types). I'll go ahead and remove those as well since they just delegate to the same methods/properties/indexer that would be called if implemented implicitly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Remove SetLength and SetEmpty from ICharTermAttribute, #1038 [lucenenet]

Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1859033674


##########
src/Lucene.Net.TestFramework/Index/BaseTermVectorsFormatTestCase.cs:
##########
@@ -360,7 +360,8 @@ public sealed override bool IncrementToken()
             {
                 if (i < terms.Length)
                 {
-                    termAtt.SetLength(0).Append(terms[i]);
+                    termAtt.Length = 0;

Review Comment:
   Done; added the extension method for SetLength, added Clear() to the interface (it was already on the concrete type via implementing Attribute's abstract method), moved SetEmpty to the extensions, and updated doc references.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Remove SetLength from ICharTermAttribute, #1038 [lucenenet]

Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1857951550


##########
src/Lucene.Net.TestFramework/Index/BaseTermVectorsFormatTestCase.cs:
##########
@@ -360,7 +360,8 @@ public sealed override bool IncrementToken()
             {
                 if (i < terms.Length)
                 {
-                    termAtt.SetLength(0).Append(terms[i]);
+                    termAtt.Length = 0;

Review Comment:
   The idea is to add an extension method to fill the gap so we can still use a fluent API like above, but remove the method as a required member to implement with `ICharTermAttribute`. Please add the `SetLength()` extension method in a new class at `Lucene.Net/Support/Analysis/TokenAttributes/Extensions/CharTermAttributeExtensions.cs` . Use the namespace `Lucene.Net.Analysis.TokenAttributes.Extensions`. This will make its usage optional by importing the namespace. Use the `ICharTermAttribute` interface rather than `CharTermAttribute` as the `this` (target) type for the extension method. Move the validation into the setter of `Length` so it doesn't have to be duplicated in the extension method.
   
   ------
   
   We will be migrating other methods into this namespace, such as the `SetOffset()` method from `IOffsetAttribute` once we add a settable `Length` property that can be set independent of `StartOffset`. `EndOffset` can be kept readonly and then there is no real need for `SetOffset()` except for Java Lucene compatibility, so it can be an optional extension method like this one.
   
   Note that we took a similar approach to this on the `IndexWriterConfig` class, changing all of the [setters and getters to properties](https://github.com/apache/lucenenet/blob/ccff6ddf6ab37cc699ef978abaff4d28f7a8ce23/src/Lucene.Net/Index/IndexWriterConfig.cs#L279-L282) and migrating the [fluent API](https://github.com/apache/lucenenet/blob/ccff6ddf6ab37cc699ef978abaff4d28f7a8ce23/src/Lucene.Net/Support/Index/Extensions/IndexWriterConfigExtensions.cs#L295-L299) to extension methods that are optional to import. So this makes the project more consistent. All of the tests still use the fluent API to stay aligned with the upstream tests (note this tests the properties simultaneously).
   
   > **NOTE:** We are missing the `null` guard clauses in the extension methods, but we should throw `ArgumentNullException` rather than relying on `NullReferenceException`. `ArgumentNullException` is a design choice, but `NullReferenceException` is a bug. This can be made into a separate issue for `IndexWriterConfigExtensions`, but please add a guard clause in the extension method for this PR.
   
   The `ICharTermAttribute.SetEmpty()` method could probably also be eliminated from the interface, replaced with a `Clear()` method, and made into an extension method. This would make `ICharTermAttribute` easier to understand when not used as a fluent API because it matches `StringBuilder` and collection types. The downside is that it would add an additional method that does the same operation. The upside is it would neatly make all of the fluent APIs into extension methods while keeping the interface and implementation free from fluent APIs.



##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs:
##########
@@ -62,29 +61,21 @@ public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
         char[] ResizeBuffer(int newSize);
 
         /// <summary>
-        /// Gets or Sets the number of valid characters (in
+        /// Gets or sets the number of valid characters (length of the term) in
         /// the termBuffer array.
-        /// <seealso cref="SetLength(int)"/>

Review Comment:
   Please add this `<seealso>` back after implementing the `SetLength()` extension method and update the reference appropriately.



##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs:
##########
@@ -489,8 +489,6 @@ public override void CopyTo(IAttribute target) // LUCENENET specific - intention
 
         char[] ICharTermAttribute.ResizeBuffer(int newSize) => ResizeBuffer(newSize);
 
-        ICharTermAttribute ICharTermAttribute.SetLength(int length) => SetLength(length);

Review Comment:
   Please remove the `SetLength()` method from this class, as it will be entirely replaced with the optional `SetLength()` extension method.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Remove SetLength from ICharTermAttribute, #1038 [lucenenet]

Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1857951550


##########
src/Lucene.Net.TestFramework/Index/BaseTermVectorsFormatTestCase.cs:
##########
@@ -360,7 +360,8 @@ public sealed override bool IncrementToken()
             {
                 if (i < terms.Length)
                 {
-                    termAtt.SetLength(0).Append(terms[i]);
+                    termAtt.Length = 0;

Review Comment:
   The idea is to add an extension method to fill the gap so we can still use a fluent API like above, but remove the method as a required member to implement with `ICharTermAttribute`. Please add the `SetLength()` extension method in a new class at `Lucene.Net/Support/Analysis/TokenAttributes/Extensions/CharTermAttributeExtensions.cs` . Use the namespace `Lucene.Net.Analysis.TokenAttributes.Extensions`. This will make its usage optional by importing the namespace. Use the `ICharTermAttribute` interface rather than `CharTermAttribute` as the `this` (target) type for the extension method. Move the guard clauses into the setter of `Length` so it doesn't have to be duplicated in the extension method.
   
   ------
   
   We will be migrating other methods into this namespace, such as the `SetOffset()` method from `IOffsetAttribute` once we add a settable `Length` property that can be set independent of `StartOffset`. `EndOffset` can be kept readonly and then there is no real need for `SetOffset()` except for Java Lucene compatibility, so it can be an optional extension method like this one.
   
   Note that we took a similar approach to this on the `IndexWriterConfig` class, changing all of the [setters and getters to properties](https://github.com/apache/lucenenet/blob/ccff6ddf6ab37cc699ef978abaff4d28f7a8ce23/src/Lucene.Net/Index/IndexWriterConfig.cs#L279-L282) and migrating the [fluent API](https://github.com/apache/lucenenet/blob/ccff6ddf6ab37cc699ef978abaff4d28f7a8ce23/src/Lucene.Net/Support/Index/Extensions/IndexWriterConfigExtensions.cs#L295-L299) to extension methods that are optional to import. So this makes the project more consistent. All of the tests still use the fluent API to stay aligned with the upstream tests (note this tests the properties simultaneously).
   
   > **NOTE:** We are missing the `null` guard clauses in the extension methods, but we should throw `ArgumentNullException` rather than relying on `NullReferenceException`. `ArgumentNullException` is a design choice, but `NullReferenceException` is a bug. This can be made into a separate issue for `IndexWriterConfigExtensions`, but please add a guard clause in the extension method for this PR.
   
   The `ICharTermAttribute.SetEmpty()` method could probably also be eliminated from the interface, replaced with a `Clear()` method, and made into an extension method. This would make `ICharTermAttribute` easier to understand when not used as a fluent API because it matches `StringBuilder` and collection types. The downside is that it would add an additional method that does the same operation. The upside is it would neatly make all of the fluent APIs into extension methods while keeping the interface and implementation free from fluent APIs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Remove SetLength and SetEmpty from ICharTermAttribute, #1038 [lucenenet]

Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1859108506


##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs:
##########
@@ -62,35 +61,29 @@ public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
         char[] ResizeBuffer(int newSize);
 
         /// <summary>
-        /// Gets or Sets the number of valid characters (in
+        /// Gets or sets the number of valid characters (length of the term) in
         /// the termBuffer array.
-        /// <seealso cref="SetLength(int)"/>
-        /// </summary>
-        new int Length { get; set; } // LUCENENET: To mimic StringBuilder, we allow this to be settable.
-
-        // LUCENENET specific: Redefining this[] to make it settable
-        new char this[int index] { get; set; }
-
-        /// <summary>
-        /// Set number of valid characters (length of the term) in
-        /// the termBuffer array. Use this to truncate the termBuffer
+        /// Use this setter to truncate the termBuffer
         /// or to synchronize with external manipulation of the termBuffer.
         /// Note: to grow the size of the array,
         /// use <see cref="ResizeBuffer(int)"/> first.
-        /// NOTE: This is exactly the same operation as calling the <see cref="Length"/> setter, the primary
-        /// difference is that this method returns a reference to the current object so it can be chained.
-        /// <code>
-        /// obj.SetLength(30).Append("hey you");
-        /// </code>
         /// </summary>
-        /// <param name="length"> the truncated length </param>
-        ICharTermAttribute SetLength(int length);
+        /// <remarks>
+        /// LUCENENET: To mimic StringBuilder, we allow this to be settable.
+        /// This replaces the chainable SetLength method in the Java version.
+        /// </remarks>
+        /// <seealso cref="Lucene.Net.Analysis.TokenAttributes.Extensions.CharTermAttributeExtensions.SetLength(ICharTermAttribute, int)"/>
+        new int Length { get; set; }
+
+        // LUCENENET specific: Redefining this[] to make it settable
+        new char this[int index] { get; set; }
 
         /// <summary>
         /// Sets the length of the termBuffer to zero.
         /// Use this method before appending contents.
         /// </summary>
-        ICharTermAttribute SetEmpty();
+        /// <seealso cref="Lucene.Net.Analysis.TokenAttributes.Extensions.CharTermAttributeExtensions.SetEmpty(ICharTermAttribute)"/>
+        void Clear();

Review Comment:
   We have already [deviated from upstream on `IAttribute`](https://github.com/apache/lucene/blob/releases/lucene-solr/4.8.0/lucene/core/src/java/org/apache/lucene/util/Attribute.java) by [adding a `CopyTo()` method](https://github.com/apache/lucenenet/blob/bcd49d8c584cdcee480f80fa46c62495740b2bc4/src/Lucene.Net/Util/Attribute.cs#L23). Like `Clear()`, `CopyTo()` is also an abstract member of `Attribute`. I don't see any reason why we shouldn't treat `Clear()` the same way.
   
   So, please move this declaration to `IAttribute` instead of here. Since `ICharTermAttribute` implements `IAttribute`, it will also provide a way to call `Clear()` from the extension method.
   
   Granted, there may be a good reason not to do this, but I am struggling to find one. Since an attribute represents a dynamically added piece of data, the ability to clear that data seems like it will always be a requirement.
   
   Adding `CopyTo()` to `IAttrbute` was a workaround because in .NET we would require a cast to do it the same way they did in Java, and this seems like a very similar scenario.



##########
src/Lucene.Net/Support/Analysis/TokenAttributes/Extensions/CharTermAttributeExtensions.cs:
##########
@@ -0,0 +1,73 @@
+/*

Review Comment:
   While I think that all of these headers should probably be moved up to the top of the file like this, currently they have been normalized to be just inside of the namespace (and indented) in every file of the solution. See: https://github.com/apache/lucenenet/blob/bcd49d8c584cdcee480f80fa46c62495740b2bc4/src/Lucene.Net/Analysis/TokenAttributes/FlagsAttributeImpl.cs#L7-L22
   
   Please place this license header inside of the namespace like the others for the time being.



##########
src/Lucene.Net.Tests.Analysis.Common/Analysis/Position/PositionFilterTest.cs:
##########
@@ -105,7 +107,7 @@ public virtual void TestReset()
         /// <summary>
         /// Tests ShingleFilter up to six shingles against six terms.
         ///  Tests PositionFilter setting all but the first positionIncrement to zero. </summary> </exception>
-        /// <exception cref="java.io.IOException"> <seealso cref= Token#next(Token) </seealso>
+        /// <exception cref="IOException"> <seealso cref= Token#next(Token) </seealso>

Review Comment:
   Please also fix the `<seealso>`  reference.



##########
src/Lucene.Net/Analysis/Token.cs:
##########
@@ -64,15 +65,15 @@ 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.SetEmpty()"/> followed by 
+    /// To load from a <see cref="string"/> use <see cref="ICharTermAttribute.SetEmpty()"/> followed by

Review Comment:
   Please fix this reference, as it is not resolving.



##########
src/Lucene.Net/Analysis/Token.cs:
##########
@@ -64,15 +65,15 @@ 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.SetEmpty()"/> followed by 
+    /// To load from a <see cref="string"/> use <see cref="ICharTermAttribute.SetEmpty()"/> 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
     /// or <see cref="ICharTermAttribute.ResizeBuffer(int)"/>, if there is any possibility
     /// that you may need to grow the buffer. Fill in the characters of your term into this
     /// buffer, with <see cref="string.ToCharArray(int, int)"/> if loading from a string,
-    /// or with <see cref="System.Array.Copy(System.Array, int, System.Array, int, int)"/>, 
-    /// and finally call <see cref="ICharTermAttribute.SetLength(int)"/> to
+    /// or with <see cref="System.Array.Copy(System.Array, int, System.Array, int, int)"/>,
+    /// and finally assign to <see cref="ICharTermAttribute.Length"/> to

Review Comment:
   Please change this to read `finally set the <see cref="ICharTermAttribute.Length"/> of the term text.`



##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs:
##########
@@ -489,9 +484,7 @@ public override void CopyTo(IAttribute target) // LUCENENET specific - intention
 
         char[] ICharTermAttribute.ResizeBuffer(int newSize) => ResizeBuffer(newSize);
 
-        ICharTermAttribute ICharTermAttribute.SetLength(int length) => SetLength(length);
-
-        ICharTermAttribute ICharTermAttribute.SetEmpty() => SetEmpty();
+        void ICharTermAttribute.Clear() => Clear();

Review Comment:
   We technically only need these explicit declarations to change the return type of the public methods that implement the interface. Since this returns `void`, we don't need it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Remove SetLength and SetEmpty from ICharTermAttribute, #1038 [lucenenet]

Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1859473842


##########
src/Lucene.Net.Tests.Analysis.Common/Analysis/Position/PositionFilterTest.cs:
##########
@@ -105,7 +107,7 @@ public virtual void TestReset()
         /// <summary>
         /// Tests ShingleFilter up to six shingles against six terms.
         ///  Tests PositionFilter setting all but the first positionIncrement to zero. </summary> </exception>
-        /// <exception cref="java.io.IOException"> <seealso cref= Token#next(Token) </seealso>
+        /// <exception cref="IOException"> <seealso cref= Token#next(Token) </seealso>

Review Comment:
   I am not sure what this is supposed to be pointing to, seems like an outdated reference. Token does not have a `Next(Token)` method in either .NET or Java. It looks from the upstream CHANGES doc that this hasn't been a thing since Lucene 2.9. PositionFilter also doesn't seem to throw IOException itself. Going to just remove this for now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IAttribute and ICharTermAttribute method changes, #1038 [lucenenet]

Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin merged PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Remove SetLength and SetEmpty from ICharTermAttribute, #1038 [lucenenet]

Posted by "paulirwin (via GitHub)" <gi...@apache.org>.
paulirwin commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1859448972


##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs:
##########
@@ -62,35 +61,29 @@ public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
         char[] ResizeBuffer(int newSize);
 
         /// <summary>
-        /// Gets or Sets the number of valid characters (in
+        /// Gets or sets the number of valid characters (length of the term) in
         /// the termBuffer array.
-        /// <seealso cref="SetLength(int)"/>
-        /// </summary>
-        new int Length { get; set; } // LUCENENET: To mimic StringBuilder, we allow this to be settable.
-
-        // LUCENENET specific: Redefining this[] to make it settable
-        new char this[int index] { get; set; }
-
-        /// <summary>
-        /// Set number of valid characters (length of the term) in
-        /// the termBuffer array. Use this to truncate the termBuffer
+        /// Use this setter to truncate the termBuffer
         /// or to synchronize with external manipulation of the termBuffer.
         /// Note: to grow the size of the array,
         /// use <see cref="ResizeBuffer(int)"/> first.
-        /// NOTE: This is exactly the same operation as calling the <see cref="Length"/> setter, the primary
-        /// difference is that this method returns a reference to the current object so it can be chained.
-        /// <code>
-        /// obj.SetLength(30).Append("hey you");
-        /// </code>
         /// </summary>
-        /// <param name="length"> the truncated length </param>
-        ICharTermAttribute SetLength(int length);
+        /// <remarks>
+        /// LUCENENET: To mimic StringBuilder, we allow this to be settable.
+        /// This replaces the chainable SetLength method in the Java version.
+        /// </remarks>
+        /// <seealso cref="Lucene.Net.Analysis.TokenAttributes.Extensions.CharTermAttributeExtensions.SetLength(ICharTermAttribute, int)"/>
+        new int Length { get; set; }
+
+        // LUCENENET specific: Redefining this[] to make it settable
+        new char this[int index] { get; set; }
 
         /// <summary>
         /// Sets the length of the termBuffer to zero.
         /// Use this method before appending contents.
         /// </summary>
-        ICharTermAttribute SetEmpty();
+        /// <seealso cref="Lucene.Net.Analysis.TokenAttributes.Extensions.CharTermAttributeExtensions.SetEmpty(ICharTermAttribute)"/>
+        void Clear();

Review Comment:
   I think this is a good idea, and most implementations seem to be using the `Attribute` abstract class anyways, which requires implementing it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IAttribute and ICharTermAttribute method changes, #1038 [lucenenet]

Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1867357716


##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs:
##########
@@ -62,37 +62,33 @@ public interface ICharTermAttribute : IAttribute, ICharSequence, IAppendable
         char[] ResizeBuffer(int newSize);
 
         /// <summary>
-        /// Gets or Sets the number of valid characters (in
+        /// Gets or sets the number of valid characters (length of the term) in
         /// the termBuffer array.
-        /// <seealso cref="SetLength(int)"/>
-        /// </summary>
-        new int Length { get; set; } // LUCENENET: To mimic StringBuilder, we allow this to be settable.
-
-        // LUCENENET specific: Redefining this[] to make it settable
-        new char this[int index] { get; set; }
-
-        /// <summary>
-        /// Set number of valid characters (length of the term) in
-        /// the termBuffer array. Use this to truncate the termBuffer
+        /// Use this setter to truncate the termBuffer
         /// or to synchronize with external manipulation of the termBuffer.
         /// Note: to grow the size of the array,
         /// use <see cref="ResizeBuffer(int)"/> first.
-        /// NOTE: This is exactly the same operation as calling the <see cref="Length"/> setter, the primary
-        /// difference is that this method returns a reference to the current object so it can be chained.
-        /// <code>
-        /// obj.SetLength(30).Append("hey you");
-        /// </code>
         /// </summary>
-        /// <param name="length"> the truncated length </param>
-        ICharTermAttribute SetLength(int length);
+        /// <remarks>
+        /// LUCENENET: To mimic StringBuilder, we allow this to be settable.
+        /// This replaces the chainable SetLength method in the Java version.

Review Comment:
   It isn't really fair to say this replaces the chainable `SetLength()` method as to supplement it. Let's change this to read:
   
   ```c#
   // The setter may be used as an alternative to <see cref="CharTermAttributeExtensions.SetLength{T}(T, int)"/> if chaining is not required.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Remove SetLength and SetEmpty from ICharTermAttribute, #1038 [lucenenet]

Posted by "NightOwl888 (via GitHub)" <gi...@apache.org>.
NightOwl888 commented on code in PR #1049:
URL: https://github.com/apache/lucenenet/pull/1049#discussion_r1860892946


##########
src/Lucene.Net/Support/Analysis/TokenAttributes/Extensions/CharTermAttributeExtensions.cs:
##########
@@ -0,0 +1,73 @@
+using System;
+
+namespace Lucene.Net.Analysis.TokenAttributes.Extensions
+{
+    /*
+     * Licensed to the Apache Software Foundation (ASF) under one or more
+     * contributor license agreements.  See the NOTICE file distributed with
+     * this work for additional information regarding copyright ownership.
+     * The ASF licenses this file to You under the Apache License, Version 2.0
+     * (the "License"); you may not use this file except in compliance with
+     * the License.  You may obtain a copy of the License at
+     *
+     *     http://www.apache.org/licenses/LICENSE-2.0
+     *
+     * Unless required by applicable law or agreed to in writing, software
+     * distributed under the License is distributed on an "AS IS" BASIS,
+     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     * See the License for the specific language governing permissions and
+     * limitations under the License.
+     */
+
+    /// <summary>
+    /// Extension methods on <see cref="ICharTermAttribute"/>.
+    /// </summary>
+    public static class CharTermAttributeExtensions
+    {
+        /// <summary>
+        /// Set number of valid characters (length of the term) in
+        /// the termBuffer array. Use this to truncate the termBuffer
+        /// or to synchronize with external manipulation of the termBuffer.
+        /// Note: to grow the size of the array,
+        /// use <see cref="ICharTermAttribute.ResizeBuffer(int)"/> first.
+        /// <para />
+        /// NOTE: This is exactly the same operation as calling the <see cref="ICharTermAttribute.Length"/> setter, the primary
+        /// difference is that this method returns a reference to the current object so it can be chained.
+        /// <code>
+        /// obj.SetLength(30).Append("hey you");
+        /// </code>
+        /// </summary>
+        /// <param name="length">The truncated length</param>
+        public static ICharTermAttribute SetLength(this ICharTermAttribute termAttr, int length)
+        {
+            if (termAttr is null)
+            {
+                throw new ArgumentNullException(nameof(termAttr));
+            }
+
+            termAttr.Length = length;
+            return termAttr;
+        }
+
+        /// <summary>
+        /// Sets the length of the termBuffer to zero.
+        /// Use this method before appending contents.
+        /// <para />
+        /// NOTE: This is exactly the same operation as calling <see cref="Lucene.Net.Util.IAttribute.Clear()"/>, the primary
+        /// difference is that this method returns a reference to the current object so it can be chained.
+        /// <code>
+        /// obj.SetEmpty().Append("hey you");
+        /// </code>
+        /// </summary>
+        public static ICharTermAttribute SetEmpty(this ICharTermAttribute termAttr)

Review Comment:
   Please change the method signature to return the concrete type, not the interface type.
   
   ```c#
   public static T SetEmpty<T>(this T termAttr) where T : ICharTermAttribute
   ```



##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs:
##########
@@ -107,32 +107,27 @@ private void GrowTermBuffer(int newSize)
             }
         }
 
-        int ICharTermAttribute.Length { get => Length; set => SetLength(value); }
+        int ICharTermAttribute.Length
+        {
+            get => Length;
+            set => Length = value;
+        }
 
         int ICharSequence.Length => Length;
 
         public int Length
         {
             get => termLength;
-            set => SetLength(value);
-        }
-
-        public CharTermAttribute SetLength(int length)
-        {
-            // LUCENENET: added guard clause
-            if (length < 0)
-                throw new ArgumentOutOfRangeException(nameof(length), length, $"{nameof(length)} must not be negative.");
-            if (length > termBuffer.Length)
-                throw new ArgumentOutOfRangeException(nameof(length), length, "length " + length + " exceeds the size of the termBuffer (" + termBuffer.Length + ")");
-
-            termLength = length;
-            return this;
-        }
+            set
+            {
+                // LUCENENET: added guard clause
+                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 + ")");

Review Comment:
   Please change this to use string interpolation. This appears to be the only one that hasn't been converted in this class.



##########
src/Lucene.Net/Support/Analysis/TokenAttributes/Extensions/CharTermAttributeExtensions.cs:
##########
@@ -0,0 +1,73 @@
+using System;
+
+namespace Lucene.Net.Analysis.TokenAttributes.Extensions
+{
+    /*
+     * Licensed to the Apache Software Foundation (ASF) under one or more
+     * contributor license agreements.  See the NOTICE file distributed with
+     * this work for additional information regarding copyright ownership.
+     * The ASF licenses this file to You under the Apache License, Version 2.0
+     * (the "License"); you may not use this file except in compliance with
+     * the License.  You may obtain a copy of the License at
+     *
+     *     http://www.apache.org/licenses/LICENSE-2.0
+     *
+     * Unless required by applicable law or agreed to in writing, software
+     * distributed under the License is distributed on an "AS IS" BASIS,
+     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     * See the License for the specific language governing permissions and
+     * limitations under the License.
+     */
+
+    /// <summary>
+    /// Extension methods on <see cref="ICharTermAttribute"/>.
+    /// </summary>
+    public static class CharTermAttributeExtensions
+    {
+        /// <summary>
+        /// Set number of valid characters (length of the term) in
+        /// the termBuffer array. Use this to truncate the termBuffer
+        /// or to synchronize with external manipulation of the termBuffer.
+        /// Note: to grow the size of the array,
+        /// use <see cref="ICharTermAttribute.ResizeBuffer(int)"/> first.
+        /// <para />
+        /// NOTE: This is exactly the same operation as calling the <see cref="ICharTermAttribute.Length"/> setter, the primary
+        /// difference is that this method returns a reference to the current object so it can be chained.
+        /// <code>
+        /// obj.SetLength(30).Append("hey you");
+        /// </code>
+        /// </summary>
+        /// <param name="length">The truncated length</param>
+        public static ICharTermAttribute SetLength(this ICharTermAttribute termAttr, int length)

Review Comment:
   Please change the method signature to return the concrete type, not the interface type.
   ```c#
   public static T SetLength<T>(this T termAttr, int length) where T : ICharTermAttribute
   ```



##########
src/Lucene.Net.Analysis.Common/Analysis/NGram/NGramTokenFilter.cs:
##########
@@ -136,6 +140,10 @@ public int PositionLength
 
             // LUCENENET specific - The interface requires this to be implemented, since we added it to avoid casts.
             public void CopyTo(IAttribute target) => _ = target;

Review Comment:
   Now that `CopyTo()` and `Clear()` are not defined on `IAttribute`, they can be removed to align with upstream.



##########
src/Lucene.Net.Analysis.Common/Analysis/NGram/NGramTokenFilter.cs:
##########
@@ -124,6 +124,10 @@ public int PositionIncrement
 
             // LUCENENET specific - The interface requires this to be implemented, since we added it to avoid casts.
             public void CopyTo(IAttribute target) => _ = target;

Review Comment:
   Now that `CopyTo()` and `Clear()` are not defined on `IAttribute`, they can be removed to align with upstream.



##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttribute.cs:
##########
@@ -285,5 +272,16 @@ 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();

Review Comment:
   Since we are effectively replacing `SetEmpty()` with `Clear()` on this attribute, please move the declaration above the `Append()` methods so it can remain in the same order as upstream.



##########
src/Lucene.Net/Analysis/TokenAttributes/CharTermAttributeImpl.cs:
##########
@@ -107,32 +107,27 @@ private void GrowTermBuffer(int newSize)
             }
         }
 
-        int ICharTermAttribute.Length { get => Length; set => SetLength(value); }
+        int ICharTermAttribute.Length

Review Comment:
   These two explicit interface implementations for `Length` are superfluous and can be removed. I suspect that cascading the call to the public `Length` property would probably not perform as well as allowing the compiler to call the property directly. This property is sealed by design, so there is no reason we need to do this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@lucenenet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org