lucenenet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andy Pook (JIRA)" <>
Subject [jira] [Commented] (LUCENENET-469) Convert Java Iterator classes to implement IEnumerable<T>
Date Wed, 09 Aug 2017 18:44:00 GMT


Andy Pook commented on LUCENENET-469:

Yes, agreed. I think I wasn't quick using the right words to disambiguate.

So... take a breath... lots of Esomething words coming....

Yes, the Enum types have the characteristics of a c# IEnumerator (Next() etc).
But also represent the current item (via Term etc).
But also has additional "collection" type operations ({{SeekExact()}} etc) (are these part
of what you were referring to as "extras"?).
It's this Enum class I was calling the "collection" type 'cos it kind of holds the collection

What I was proposing was to make the Enum also look like an IEnumerable so that you can {{foreach}}
over it.
Which means it needs a {{GetEnumerable()}} to return an IEnumerator. This new enumerator is
the {{EnumEnumerator}} type from in previous messages.

So, I'm taking an Enum, adding an IEnumerable facade, to return a new IEnumerable type which
pokes the Enum to move forward.

It was the {{Dispose()}} of this new IEnumerator that I was referring to. Pretty convinced
that this one should always be a no-op. Exiting the loop should not Dispose the Enum (ie the
"collection") and as the new IEnumerator is just poking methods on the Enum, there's nothing
for it to clean up.

Pretty sure I've got all the right E's in the right place :)

NB: some thought would be needed to define what the "item" class (representing Current) should
look like in each case.
NB: Calling a Current changing method (ie one of the Seek...() methods) from inside a foreach
would result in undefined behavior.

Phew... Did that make sense; fit with your view of the universe?

> Convert Java Iterator classes to implement IEnumerable<T>
> ---------------------------------------------------------
>                 Key: LUCENENET-469
>                 URL:
>             Project: Lucene.Net
>          Issue Type: Sub-task
>          Components: Lucene.Net Contrib, Lucene.Net Core
>    Affects Versions: Lucene.Net 2.9.4, Lucene.Net 2.9.4g, Lucene.Net 3.0.3, Lucene.Net
>         Environment: all
>            Reporter: Christopher Currens
>             Fix For: Lucene.Net 4.8.0
> The Iterator pattern in Java is equivalent to IEnumerable in .NET.  Classes that were
directly ported in Java using the Iterator pattern, cannot be used with Linq or foreach blocks
in .NET.
> {{Next()}} would be equivalent to .NET's {{MoveNext()}}, and in the below case, {{Term()}}
would be as .NET's {{Current}} property.  In cases as below, it will require {{TermEnum}}
to become an abstract class with {{Term}} and {{DocFreq}} properties, which would be returned
from another class or method that implemented {{IEnumerable<TermEnum>}}.
> {noformat} 
> 	public abstract class TermEnum : IDisposable
> 	{
> 		public abstract bool Next();
> 		public abstract Term Term();
> 		public abstract int DocFreq();
> 		public abstract void  Close();
> 	        public abstract void Dispose();
> 	}
> {noformat} 
> would instead look something like:
> {noformat} 
> 	public class TermFreq
> 	{
> 		public abstract Term { get; }
> 		public abstract int { get; }
> 	}
>         public abstract class TermEnum : IEnumerable<TermFreq>, IDisposable
>         {
>                 // ...
>         }
> {noformat}
> Keep in mind that it is important that if the class being converted implements {{IDisposable}},
the class that is enumerating the terms (in this case {{TermEnum}}) should inherit from both
{{IEnumerable<T>}} *and* {{IDisposable}}.  This won't be any change to the user, as
the compiler automatically calls {{IDisposable}} when used in a {{foreach}} loop.

This message was sent by Atlassian JIRA

View raw message