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/08/13 16:33:36 UTC

[4/5] lucenenet git commit: API: Lucene.Net.Documents.Field: Added GetNumericType() method and refactored codecs, suggest, misc, spatial, and classification so they utilize the GetXXXValue() methods instead of GetNumericValue(). Added extension methods G

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/c22ef079/src/Lucene.Net/Index/IndexableField.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Index/IndexableField.cs b/src/Lucene.Net/Index/IndexableField.cs
index a04c5e6..7f913ca 100644
--- a/src/Lucene.Net/Index/IndexableField.cs
+++ b/src/Lucene.Net/Index/IndexableField.cs
@@ -1,3 +1,4 @@
+using System;
 using System.IO;
 
 namespace Lucene.Net.Index
@@ -78,56 +79,107 @@ namespace Lucene.Net.Index
 
         /// <summary>
         /// Non-null if this field has a string value. </summary>
+        /// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns>
         string GetStringValue();
 
         /// <summary>
+        /// The value of the field as a <see cref="string"/>, or <c>null</c>. If <c>null</c>, the <see cref="TextReader"/> value or
+        /// binary value is used. Exactly one of <see cref="GetStringValue()"/>, <see cref="GetReaderValue()"/>, and
+        /// <see cref="GetBinaryValue()"/> must be set.
+        /// </summary>
+        /// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param>
+        /// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns>
+        // LUCENENET specific overload.
+        string GetStringValue(IFormatProvider provider);
+
+        /// <summary>
+        /// The value of the field as a <see cref="string"/>, or <c>null</c>. If <c>null</c>, the <see cref="TextReader"/> value or
+        /// binary value is used. Exactly one of <see cref="GetStringValue()"/>, <see cref="GetReaderValue()"/>, and
+        /// <see cref="GetBinaryValue()"/> must be set.
+        /// </summary>
+        /// <param name="format">A standard or custom numeric format string. This parameter has no effect if this field is non-numeric.</param>
+        /// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns>
+        // LUCENENET specific overload.
+        string GetStringValue(string format);
+
+        /// <summary>
+        /// The value of the field as a <see cref="string"/>, or <c>null</c>. If <c>null</c>, the <see cref="TextReader"/> value or
+        /// binary value is used. Exactly one of <see cref="GetStringValue()"/>, <see cref="GetReaderValue()"/>, and
+        /// <see cref="GetBinaryValue()"/> must be set.
+        /// </summary>
+        /// <param name="format">A standard or custom numeric format string. This parameter has no effect if this field is non-numeric.</param>
+        /// <param name="provider">An object that supplies culture-specific formatting information. This parameter has no effect if this field is non-numeric.</param>
+        /// <returns>The string representation of the value if it is either a <see cref="string"/> or numeric type.</returns>
+        // LUCENENET specific overload.
+        string GetStringValue(string format, IFormatProvider provider);
+
+
+        /// <summary>
         /// Non-null if this field has a <see cref="TextReader"/> value </summary>
         TextReader GetReaderValue();
 
         /// <summary>
         /// Non-null if this field has a numeric value. </summary>
-        object GetNumericValue(); // LUCENENET TODO: Can we eliminate object?
+        [Obsolete("In .NET, use of this method will cause boxing/unboxing. Instead, call GetNumericType() to check the underlying type and call the appropriate GetXXXValue() method to retrieve the value.")]
+        object GetNumericValue(); 
 
         /// <summary>
-        /// Non-null if this field has a numeric value.
+        /// Gets the <see cref="Type"/> of the underlying value, or <c>null</c> if the value is not set or non-numeric.
         /// <para/>
-        /// LUCENENET specific
+        /// LUCENENET specific. In Java, the numeric type was determined by checking the type of  
+        /// <see cref="GetNumericValue()"/>. However, since there are no reference number
+        /// types in .NET, using <see cref="GetNumericValue()"/> so will cause boxing/unboxing. It is
+        /// therefore recommended to call this method to check the underlying type and the corresponding 
+        /// <c>Get*Value()</c> method to retrieve the value.
         /// </summary>
+        Type GetNumericType();
+
+        /// <summary>
+        /// Returns the field value as <see cref="byte"/> or <c>null</c> if the type
+        /// is non-numeric.
+        /// </summary>
+        /// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
+        // LUCENENET specific
         byte? GetByteValue();
 
         /// <summary>
-        /// Non-null if this field has a numeric value.
-        /// <para/>
-        /// LUCENENET specific
+        /// Returns the field value as <see cref="short"/> or <c>null</c> if the type
+        /// is non-numeric.
         /// </summary>
+        /// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
+        // LUCENENET specific
         short? GetInt16Value();
 
         /// <summary>
-        /// Non-null if this field has a numeric value.
-        /// <para/>
-        /// LUCENENET specific
+        /// Returns the field value as <see cref="int"/> or <c>null</c> if the type
+        /// is non-numeric.
         /// </summary>
+        /// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
+        // LUCENENET specific
         int? GetInt32Value();
 
         /// <summary>
-        /// Non-null if this field has a numeric value.
-        /// <para/>
-        /// LUCENENET specific
+        /// Returns the field value as <see cref="long"/> or <c>null</c> if the type
+        /// is non-numeric.
         /// </summary>
+        /// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
+        // LUCENENET specific
         long? GetInt64Value();
 
         /// <summary>
-        /// Non-null if this field has a numeric value.
-        /// <para/>
-        /// LUCENENET specific
+        /// Returns the field value as <see cref="float"/> or <c>null</c> if the type
+        /// is non-numeric.
         /// </summary>
+        /// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
+        // LUCENENET specific
         float? GetSingleValue();
 
         /// <summary>
-        /// Non-null if this field has a numeric value.
-        /// <para/>
-        /// LUCENENET specific
+        /// Returns the field value as <see cref="double"/> or <c>null</c> if the type
+        /// is non-numeric.
         /// </summary>
+        /// <returns>The field value or <c>null</c> if the type is non-numeric.</returns>
+        // LUCENENET specific
         double? GetDoubleValue();
 
         /// <summary>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/c22ef079/src/Lucene.Net/Lucene.Net.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Lucene.Net.csproj b/src/Lucene.Net/Lucene.Net.csproj
index 8ca72e7..7234e15 100644
--- a/src/Lucene.Net/Lucene.Net.csproj
+++ b/src/Lucene.Net/Lucene.Net.csproj
@@ -397,6 +397,7 @@
     <Compile Include="Support\AssemblyUtils.cs" />
     <Compile Include="Support\Document\DocumentExtensions.cs" />
     <Compile Include="Support\Document\Field.cs" />
+    <Compile Include="Support\Document\IndexableFieldExtensions.cs" />
     <Compile Include="Support\IO\Compression\LZOCompressor.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Search\AutomatonQuery.cs" />

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/c22ef079/src/Lucene.Net/Support/Document/IndexableFieldExtensions.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/Document/IndexableFieldExtensions.cs b/src/Lucene.Net/Support/Document/IndexableFieldExtensions.cs
new file mode 100644
index 0000000..d56634d
--- /dev/null
+++ b/src/Lucene.Net/Support/Document/IndexableFieldExtensions.cs
@@ -0,0 +1,99 @@
+using Lucene.Net.Index;
+
+namespace Lucene.Net.Documents
+{
+    /*
+     * 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 to the <see cref="IIndexableField"/> interface.
+    /// </summary>
+    public static class IndexableFieldExtensions
+    {
+        /// <summary>
+        /// Returns the field value as <see cref="byte"/> or <c>0</c> if the type
+        /// is non-numeric.
+        /// </summary>
+        /// <param name="field">This <see cref="IIndexableField"/>.</param>
+        /// <returns>The field value or <c>0</c> if the type is non-numeric.</returns>
+        public static byte GetByteValueOrDefault(this IIndexableField field)
+        {
+            if (field == null) return default(byte);
+            return field.GetByteValue().GetValueOrDefault();
+        }
+
+        /// <summary>
+        /// Returns the field value as <see cref="short"/> or <c>0</c> if the type
+        /// is non-numeric.
+        /// </summary>
+        /// <param name="field">This <see cref="IIndexableField"/>.</param>
+        /// <returns>The field value or <c>0</c> if the type is non-numeric.</returns>
+        public static short GetInt16ValueOrDefault(this IIndexableField field)
+        {
+            if (field == null) return default(short);
+            return field.GetInt16Value().GetValueOrDefault();
+        }
+
+        /// <summary>
+        /// Returns the field value as <see cref="int"/> or <c>0</c> if the type
+        /// is non-numeric.
+        /// </summary>
+        /// <param name="field">This <see cref="IIndexableField"/>.</param>
+        /// <returns>The field value or <c>0</c> if the type is non-numeric.</returns>
+        public static int GetInt32ValueOrDefault(this IIndexableField field)
+        {
+            if (field == null) return default(int);
+            return field.GetInt32Value().GetValueOrDefault();
+        }
+
+        /// <summary>
+        /// Returns the field value as <see cref="long"/> or <c>0</c> if the type
+        /// is non-numeric.
+        /// </summary>
+        /// <param name="field">This <see cref="IIndexableField"/>.</param>
+        /// <returns>The field value or <c>0</c> if the type is non-numeric.</returns>
+        public static long GetInt64ValueOrDefault(this IIndexableField field)
+        {
+            if (field == null) return default(long);
+            return field.GetInt64Value().GetValueOrDefault();
+        }
+
+        /// <summary>
+        /// Returns the field value as <see cref="float"/> or <c>0</c> if the type
+        /// is non-numeric.
+        /// </summary>
+        /// <param name="field">This <see cref="IIndexableField"/>.</param>
+        /// <returns>The field value or <c>0</c> if the type is non-numeric.</returns>
+        public static float GetSingleValueOrDefault(this IIndexableField field)
+        {
+            if (field == null) return default(float);
+            return field.GetSingleValue().GetValueOrDefault();
+        }
+
+        /// <summary>
+        /// Returns the field value as <see cref="double"/> or <c>0</c> if the type
+        /// is non-numeric.
+        /// </summary>
+        /// <param name="field">This <see cref="IIndexableField"/>.</param>
+        /// <returns>The field value or <c>0</c> if the type is non-numeric.</returns>
+        public static double GetDoubleValueOrDefault(this IIndexableField field)
+        {
+            if (field == null) return default(double);
+            return field.GetDoubleValue().GetValueOrDefault();
+        }
+    }
+}