This is an automated email from the ASF dual-hosted git repository. nightowl888 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 3f2cf240b67e15462d2b56743b5382f6ea990380 Author: Shad Storhaug <[email protected]> AuthorDate: Tue Sep 22 19:51:33 2020 +0700 Lucene.Net.Search.FieldCache: Added delegate constructor to field value types so they can be declared inline --- src/Lucene.Net/Search/FieldCache.cs | 199 ++++++++++++++++++++++++------------ 1 file changed, 132 insertions(+), 67 deletions(-) diff --git a/src/Lucene.Net/Search/FieldCache.cs b/src/Lucene.Net/Search/FieldCache.cs index 7acd2b1..27931f7 100644 --- a/src/Lucene.Net/Search/FieldCache.cs +++ b/src/Lucene.Net/Search/FieldCache.cs @@ -363,25 +363,36 @@ namespace Lucene.Net.Search /// <summary> /// Field values as 8-bit signed bytes /// </summary> - public abstract class Bytes + public class Bytes // LUCENENET specific - removed abstract so we can pass a delegate to the constructor { + private readonly Func<int, byte> get; + private readonly bool hasGet; + /// <summary> - /// Return a single Byte representation of this field's value. + /// Initialize an instance of <see cref="Bytes"/>. /// </summary> - public abstract byte Get(int docID); + protected Bytes() { } // LUCENENET specific - Added default constructor for subclasses /// <summary> - /// Zero value for every document + /// Initialize an instance of <see cref="Bytes"/> with the specified + /// <paramref name="get"/> delegate method. /// </summary> - public static readonly Bytes EMPTY = new EmptyBytes(); - - private sealed class EmptyBytes : Bytes + /// <param name="get">A <see cref="Func{T, TResult}"/> that implements the <see cref="Get(int)"/> method body.</param> + public Bytes(Func<int, byte> get) { - public override byte Get(int docID) - { - return 0; - } + this.get = get ?? throw new ArgumentNullException(nameof(get)); + this.hasGet = true; } + + /// <summary> + /// Return a single <see cref="byte"/> representation of this field's value. + /// </summary> + public virtual byte Get(int docID) => hasGet ? get(docID) : default; // LUCENENET specific - implemented with delegate by default + + /// <summary> + /// Zero value for every document + /// </summary> + public static readonly Bytes EMPTY = new Bytes((docID) => 0); } /// <summary> @@ -389,25 +400,36 @@ namespace Lucene.Net.Search /// <para/> /// NOTE: This was Shorts in Lucene /// </summary> - public abstract class Int16s + public class Int16s // LUCENENET specific - removed abstract so we can pass a delegate to the constructor { + private readonly Func<int, short> get; + private readonly bool hasGet; + /// <summary> - /// Return a <see cref="short"/> representation of this field's value. + /// Initialize an instance of <see cref="Int16s"/>. /// </summary> - public abstract short Get(int docID); + protected Int16s() { } // LUCENENET specific - Added default constructor for subclasses /// <summary> - /// Zero value for every document + /// Initialize an instance of <see cref="Int16s"/> with the specified + /// <paramref name="get"/> delegate method. /// </summary> - public static readonly Int16s EMPTY = new EmptyInt16s(); - - private sealed class EmptyInt16s : Int16s + /// <param name="get">A <see cref="Func{T, TResult}"/> that implements the <see cref="Get(int)"/> method body.</param> + public Int16s(Func<int, short> get) { - public override short Get(int docID) - { - return 0; - } + this.get = get ?? throw new ArgumentNullException(nameof(get)); + this.hasGet = true; } + + /// <summary> + /// Return a <see cref="short"/> representation of this field's value. + /// </summary> + public virtual short Get(int docID) => hasGet ? get(docID) : default; // LUCENENET specific - implemented with delegate by default + + /// <summary> + /// Zero value for every document + /// </summary> + public static readonly Int16s EMPTY = new Int16s((docID) => 0); } /// <summary> @@ -415,25 +437,36 @@ namespace Lucene.Net.Search /// <para/> /// NOTE: This was Ints in Lucene /// </summary> - public abstract class Int32s + public class Int32s // LUCENENET specific - removed abstract so we can pass a delegate to the constructor { + private readonly Func<int, int> get; + private readonly bool hasGet; + /// <summary> - /// Return an <see cref="int"/> representation of this field's value. + /// Initialize an instance of <see cref="Int32s"/>. /// </summary> - public abstract int Get(int docID); + protected Int32s() { } // LUCENENET specific - Added default constructor for subclasses /// <summary> - /// Zero value for every document + /// Initialize an instance of <see cref="Int32s"/> with the specified + /// <paramref name="get"/> delegate method. /// </summary> - public static readonly Int32s EMPTY = new EmptyInt32s(); - - private sealed class EmptyInt32s : Int32s + /// <param name="get">A <see cref="Func{T, TResult}"/> that implements the <see cref="Get(int)"/> method body.</param> + public Int32s(Func<int, int> get) { - public override int Get(int docID) - { - return 0; - } + this.get = get ?? throw new ArgumentNullException(nameof(get)); + this.hasGet = true; } + + /// <summary> + /// Return an <see cref="int"/> representation of this field's value. + /// </summary> + public virtual int Get(int docID) => hasGet ? get(docID) : default; // LUCENENET specific - implemented with delegate by default + + /// <summary> + /// Zero value for every document + /// </summary> + public static readonly Int32s EMPTY = new Int32s((docID) => 0); } /// <summary> @@ -441,25 +474,36 @@ namespace Lucene.Net.Search /// <para/> /// NOTE: This was Longs in Lucene /// </summary> - public abstract class Int64s + public class Int64s // LUCENENET specific - removed abstract so we can pass a delegate to the constructor { + private readonly Func<int, long> get; + private readonly bool hasGet; + /// <summary> - /// Return an <see cref="long"/> representation of this field's value. + /// Initialize an instance of <see cref="Int64s"/>. /// </summary> - public abstract long Get(int docID); + protected Int64s() { } // LUCENENET specific - Added default constructor for subclasses /// <summary> - /// Zero value for every document + /// Initialize an instance of <see cref="Int64s"/> with the specified + /// <paramref name="get"/> delegate method. /// </summary> - public static readonly Int64s EMPTY = new EmptyInt64s(); - - private sealed class EmptyInt64s : Int64s + /// <param name="get">A <see cref="Func{T, TResult}"/> that implements the <see cref="Get(int)"/> method body.</param> + public Int64s(Func<int, long> get) { - public override long Get(int docID) - { - return 0; - } + this.get = get ?? throw new ArgumentNullException(nameof(get)); + this.hasGet = true; } + + /// <summary> + /// Return an <see cref="long"/> representation of this field's value. + /// </summary> + public virtual long Get(int docID) => hasGet ? get(docID) : default; // LUCENENET specific - implemented with delegate by default + + /// <summary> + /// Zero value for every document + /// </summary> + public static readonly Int64s EMPTY = new Int64s((docID) => 0); } /// <summary> @@ -467,50 +511,71 @@ namespace Lucene.Net.Search /// <para/> /// NOTE: This was Floats in Lucene /// </summary> - public abstract class Singles + public class Singles // LUCENENET specific - removed abstract so we can pass a delegate to the constructor { + private readonly Func<int, float> get; + private readonly bool hasGet; + /// <summary> - /// Return an <see cref="float"/> representation of this field's value. + /// Initialize an instance of <see cref="Singles"/>. /// </summary> - public abstract float Get(int docID); + protected Singles() { } // LUCENENET specific - Added default constructor for subclasses /// <summary> - /// Zero value for every document + /// Initialize an instance of <see cref="Singles"/> with the specified + /// <paramref name="get"/> delegate method. /// </summary> - public static readonly Singles EMPTY = new EmptySingles(); - - private sealed class EmptySingles : Singles + /// <param name="get">A <see cref="Func{T, TResult}"/> that implements the <see cref="Get(int)"/> method body.</param> + public Singles(Func<int, float> get) { - public override float Get(int docID) - { - return 0; - } + this.get = get ?? throw new ArgumentNullException(nameof(get)); + this.hasGet = true; } + + /// <summary> + /// Return an <see cref="float"/> representation of this field's value. + /// </summary> + public virtual float Get(int docID) => hasGet ? get(docID) : default; // LUCENENET specific - implemented with delegate by default + + /// <summary> + /// Zero value for every document + /// </summary> + public static readonly Singles EMPTY = new Singles((docID) => 0); } /// <summary> /// Field values as 64-bit doubles /// </summary> - public abstract class Doubles + public class Doubles // LUCENENET specific - removed abstract so we can pass a delegate to the constructor { + private readonly Func<int, double> get; + private readonly bool hasGet; + /// <summary> - /// Return a <see cref="double"/> representation of this field's value. + /// Initialize an instance of <see cref="Doubles"/>. /// </summary> - /// <param name="docID"></param> - public abstract double Get(int docID); + protected Doubles() { } // LUCENENET specific - Added default constructor for subclasses /// <summary> - /// Zero value for every document + /// Initialize an instance of <see cref="Doubles"/> with the specified + /// <paramref name="get"/> delegate method. /// </summary> - public static readonly Doubles EMPTY = new EmptyDoubles(); - - private sealed class EmptyDoubles : Doubles + /// <param name="get">A <see cref="Func{T, TResult}"/> that implements the <see cref="Get(int)"/> method body.</param> + public Doubles(Func<int, double> get) { - public override double Get(int docID) - { - return 0; - } + this.get = get ?? throw new ArgumentNullException(nameof(get)); + this.hasGet = true; } + + /// <summary> + /// Return a <see cref="double"/> representation of this field's value. + /// </summary> + public virtual double Get(int docID) => hasGet ? get(docID) : default; // LUCENENET specific - implemented with delegate by default + + /// <summary> + /// Zero value for every document + /// </summary> + public static readonly Doubles EMPTY = new Doubles((docID) => 0); } /// <summary>
