You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2017/06/22 05:25:29 UTC

[28/38] lucenenet git commit: API: Lucene.Net.Search.Collector: Added Collector.NewAnonymous() method for easy creation of anonymous classes via delegate methods.

API: Lucene.Net.Search.Collector: Added Collector.NewAnonymous() method for easy creation of anonymous classes via delegate methods.


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/f674dc6d
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/f674dc6d
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/f674dc6d

Branch: refs/heads/master
Commit: f674dc6d70eb4ca2e4799685e4fd91d600ef6042
Parents: bd6a1be
Author: Shad Storhaug <sh...@shadstorhaug.com>
Authored: Wed Jun 21 20:23:17 2017 +0700
Committer: Shad Storhaug <sh...@shadstorhaug.com>
Committed: Thu Jun 22 00:13:01 2017 +0700

----------------------------------------------------------------------
 src/Lucene.Net/Search/Collector.cs | 113 +++++++++++++++++++++++++++++++-
 1 file changed, 111 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/f674dc6d/src/Lucene.Net/Search/Collector.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Search/Collector.cs b/src/Lucene.Net/Search/Collector.cs
index f3f9c31..986e5ee 100644
--- a/src/Lucene.Net/Search/Collector.cs
+++ b/src/Lucene.Net/Search/Collector.cs
@@ -1,3 +1,5 @@
+using System;
+
 namespace Lucene.Net.Search
 {
     /*
@@ -186,6 +188,113 @@ namespace Lucene.Net.Search
         /// </summary>
         bool AcceptsDocsOutOfOrder { get; }
     }
-}
 
-// LUCENENET TODO: API: Create Collector.NewAnonymous() static delegate method to allow creation of collectors inline.
\ No newline at end of file
+    /// <summary>
+    /// LUCENENET specific class used to hold the 
+    /// <see cref="NewAnonymous(Action{Scorer}, Action{int}, Action{AtomicReaderContext}, Func{bool})"/> static method.
+    /// </summary>
+    public static class Collector
+    {
+        /// <summary>
+        /// Creates a new instance with the ability to specify the body of the <see cref="ICollector.SetScorer(Scorer)"/>
+        /// method through the <paramref name="setScorer"/> parameter, the body of the <see cref="ICollector.Collect(int)"/>
+        /// method through the <paramref name="collect"/> parameter, the body of the <see cref="ICollector.SetNextReader(AtomicReaderContext)"/>
+        /// method through the <paramref name="setNextReader"/> parameter, and the body of the <see cref="ICollector.AcceptsDocsOutOfOrder"/>
+        /// property through the <paramref name="acceptsDocsOutOfOrder"/> parameter.
+        /// Simple example:
+        /// <code>
+        ///     IndexSearcher searcher = new IndexSearcher(indexReader);
+        ///     OpenBitSet bits = new OpenBitSet(indexReader.MaxDoc);
+        ///     int docBase;
+        ///     searcher.Search(query, 
+        ///         Collector.NewAnonymous(setScorer: (scorer) =>
+        ///         {
+        ///             // ignore scorer
+        ///         }, collect: (doc) =>
+        ///         {
+        ///             bits.Set(doc + docBase);
+        ///         }, setNextReader: (context) =>
+        ///         {
+        ///             docBase = context.DocBase;
+        ///         }, acceptsDocsOutOfOrder: () =>
+        ///         {
+        ///             return true;
+        ///         })
+        ///     );
+        /// </code>
+        /// </summary>
+        /// <param name="setScorer">
+        /// A delegate method that represents (is called by) the <see cref="ICollector.SetScorer(Scorer)"/> 
+        /// method. It accepts a <see cref="Scorer"/> scorer and 
+        /// has no return value.
+        /// </param>
+        /// <param name="collect">
+        /// A delegate method that represents (is called by) the <see cref="ICollector.Collect(int)"/> 
+        /// method. It accepts an <see cref="int"/> doc and 
+        /// has no return value.
+        /// </param>
+        /// <param name="setNextReader">
+        /// A delegate method that represents (is called by) the <see cref="ICollector.SetNextReader(AtomicReaderContext)"/> 
+        /// method. It accepts a <see cref="AtomicReaderContext"/> context and 
+        /// has no return value.
+        /// </param>
+        /// <param name="acceptsDocsOutOfOrder">
+        /// A delegate method that represents (is called by) the <see cref="ICollector.AcceptsDocsOutOfOrder"/> 
+        /// property. It returns a <see cref="bool"/> value.
+        /// </param>
+        /// <returns> A new <see cref="AnonymousCollector"/> instance. </returns>
+        public static ICollector NewAnonymous(Action<Scorer> setScorer, Action<int> collect, Action<AtomicReaderContext> setNextReader, Func<bool> acceptsDocsOutOfOrder)
+        {
+            return new AnonymousCollector(setScorer, collect, setNextReader, acceptsDocsOutOfOrder);
+        }
+
+        // LUCENENET specific
+        private class AnonymousCollector : ICollector
+        {
+            private readonly Action<Scorer> setScorer;
+            private readonly Action<int> collect;
+            private readonly Action<AtomicReaderContext> setNextReader;
+            private readonly Func<bool> acceptsDocsOutOfOrder;
+
+            public AnonymousCollector(Action<Scorer> setScorer, Action<int> collect, Action<AtomicReaderContext> setNextReader, Func<bool> acceptsDocsOutOfOrder)
+            {
+                if (setScorer == null)
+                    throw new ArgumentNullException("setScorer");
+                if (collect == null)
+                    throw new ArgumentNullException("collect");
+                if (setNextReader == null)
+                    throw new ArgumentNullException("setNextReader");
+                if (acceptsDocsOutOfOrder == null)
+                    throw new ArgumentNullException("acceptsDocsOutOfOrder");
+
+                this.setScorer = setScorer;
+                this.collect = collect;
+                this.setNextReader = setNextReader;
+                this.acceptsDocsOutOfOrder = acceptsDocsOutOfOrder;
+            }
+
+            public bool AcceptsDocsOutOfOrder
+            {
+                get
+                {
+                    return this.acceptsDocsOutOfOrder();
+                }
+            }
+
+            public void Collect(int doc)
+            {
+                this.collect(doc);
+            }
+
+            public void SetNextReader(AtomicReaderContext context)
+            {
+                this.setNextReader(context);
+            }
+
+            public void SetScorer(Scorer scorer)
+            {
+                this.setScorer(scorer);
+            }
+        }
+    }
+}
\ No newline at end of file