lucenenet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Digy" <>
Subject RE: [Lucene.Net] [jira] [Created] (LUCENENET-469) Convert Java Iterator classes to implement IEnumerable<T>
Date Fri, 08 Jun 2012 22:19:12 GMT
Hi Andy,

I have used similar extension methods for a long time. What I like
especially in extension methods is that they don't require changes in
Lucene.Net kernel and make further ports of independent from
.Net structures.

+1 for a Lucene.Net extensions in contrib. Even a +1 for a
"Lucene.Net.Extensions" for the core.


-----Original Message-----
From: Andy Pook [] 
Sent: Friday, June 08, 2012 10:26 PM
Subject: Re: [Lucene.Net] [jira] [Created] (LUCENENET-469) Convert Java
Iterator classes to implement IEnumerable<T>

If we don't want to add IEnumerable (though it seems that IEnumerable could
be added in parallel with the existing pattern) could we add a bunch of
extension methods?
Something like the following...

public static class LuceneExtensions
public static IEnumerable<Term> GetEnumerable(this TermEnum termEnum) {
yield return termEnum.Term(); while (termEnum.Next()) yield return
termEnum.Term(); } } {noformat}

Then you can...
foreach(var e in myTernEnum.GetEnumerable()) {
    // do stuff with e

Not as elegant as a direct implementation but gives easy enough access to
foreach sematics.

The second option is to realize that you don't need to explicitly implement
IEnumerable. You just need a GetEnumerator method.
So just add...

public IEnumerable<Term> GetEnumerator() { yield return Term(); while
(Next()) yield return Term(); } {noformat}

Now you get nice foreach sematics without even mentioning IEnumerable.
Compiler magic is your friend :-)

BTW: Dispose() is only called automatically when exiting a using block.
Exiting a foreach will not.


On 24 January 2012 06:37, Christopher Currens (Created) (JIRA) <> wrote:

> 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 3.0.3, Lucene.Net 2.9.4g
>         Environment: all
>            Reporter: Christopher Currens
>             Fix For: Lucene.Net 3.0.3
> 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
> {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 is automatically generated by JIRA.
> If you think it was sent incorrectly, please contact your JIRA
> administrators:
> pa For more information on JIRA, see: 


Checked by AVG -
Version: 2012.0.2177 / Virus Database: 2433/5056 - Release Date: 06/08/12

View raw message