You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by sy...@apache.org on 2016/10/04 20:01:36 UTC

[06/46] lucenenet git commit: Facet.FacetResult & Facet.LabelAndValue: Fixed string format bugs by adding extra constructors to track the underlying data type of return value and formatting accordingly in ToString().

Facet.FacetResult & Facet.LabelAndValue: Fixed string format bugs by adding extra constructors to track the underlying data type of return value and formatting accordingly in ToString().


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

Branch: refs/heads/master
Commit: abf096ef554a2e3a5dcba3c83a71681792761816
Parents: 602bbcc
Author: Shad Storhaug <sh...@shadstorhaug.com>
Authored: Sat Sep 24 17:32:38 2016 +0700
Committer: Shad Storhaug <sh...@shadstorhaug.com>
Committed: Mon Oct 3 23:30:44 2016 +0700

----------------------------------------------------------------------
 src/Lucene.Net.Facet/FacetResult.cs             | 42 ++++++++++++++++++--
 src/Lucene.Net.Facet/LabelAndValue.cs           | 28 ++++++++++++-
 .../Taxonomy/FloatAssociationFacetField.cs      |  3 +-
 3 files changed, 67 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/abf096ef/src/Lucene.Net.Facet/FacetResult.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Facet/FacetResult.cs b/src/Lucene.Net.Facet/FacetResult.cs
index be619b2..924f8d5 100644
--- a/src/Lucene.Net.Facet/FacetResult.cs
+++ b/src/Lucene.Net.Facet/FacetResult.cs
@@ -1,6 +1,8 @@
 \ufeffusing System.Text;
 using Lucene.Net.Support;
 using Lucene.Net.Util;
+using System.Globalization;
+using System;
 
 namespace Lucene.Net.Facet
 {
@@ -51,12 +53,39 @@ namespace Lucene.Net.Facet
         public readonly LabelAndValue[] LabelValues;
 
         /// <summary>
-        /// Sole constructor. </summary>
+        /// The original data type of <see cref="Value"/> that was passed through the constructor.
+        /// </summary>
+        public readonly Type typeOfValue;
+
+        /// <summary>
+        /// Constructor for <see cref="float"/> <paramref name="value"/>. Makes the <see cref="ToString()"/> method 
+        /// print the <paramref name="value"/> as a <see cref="float"/> with at least 1 number after the decimal.
+        /// </summary>
         public FacetResult(string dim, string[] path, float value, LabelAndValue[] labelValues, int childCount)
+            : this(dim, path, labelValues, childCount)
+        {
+            this.Value = value;
+            this.typeOfValue = typeof(float);
+        }
+
+        /// <summary>
+        /// Constructor for <see cref="int"/> <paramref name="value"/>. Makes the <see cref="ToString()"/> method 
+        /// print the <paramref name="value"/> as an <see cref="int"/> with no decimal.
+        /// </summary>
+        public FacetResult(string dim, string[] path, int value, LabelAndValue[] labelValues, int childCount)
+            : this(dim, path, labelValues, childCount)
+        {
+            this.Value = value;
+            this.typeOfValue = typeof(int);
+        }
+
+        /// <summary>
+        /// Private constructor for shared parameters to be called by public constructors.
+        /// </summary>
+        private FacetResult(string dim, string[] path, LabelAndValue[] labelValues, int childCount)
         {
             this.Dim = dim;
             this.Path = path;
-            this.Value = value;
             this.LabelValues = labelValues;
             this.ChildCount = childCount;
         }
@@ -69,7 +98,14 @@ namespace Lucene.Net.Facet
             sb.Append(" path=");
             sb.Append("[" + Arrays.ToString(Path) + "]");
             sb.Append(" value=");
-            sb.Append(Value);
+            if (typeOfValue == typeof(int))
+            {
+                sb.AppendFormat(CultureInfo.InvariantCulture, "{0:0}", Value); // No formatting (looks like int)
+            }
+            else
+            {
+                sb.AppendFormat(CultureInfo.InvariantCulture, "{0:0.0#####}", Value); // Decimal formatting
+            }
             sb.Append(" childCount=");
             sb.Append(ChildCount);
             sb.Append('\n');

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/abf096ef/src/Lucene.Net.Facet/LabelAndValue.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Facet/LabelAndValue.cs b/src/Lucene.Net.Facet/LabelAndValue.cs
index 0cbcddc..6cf1991 100644
--- a/src/Lucene.Net.Facet/LabelAndValue.cs
+++ b/src/Lucene.Net.Facet/LabelAndValue.cs
@@ -1,4 +1,6 @@
 \ufeffusing Lucene.Net.Support;
+using System;
+using System.Globalization;
 
 namespace Lucene.Net.Facet
 {
@@ -35,16 +37,38 @@ namespace Lucene.Net.Facet
         public readonly float value;
 
         /// <summary>
-        /// Sole constructor. </summary>
+        /// The original data type of <see cref="value"/> that was passed through the constructor.
+        /// </summary>
+        public readonly Type typeOfValue;
+
+        /// <summary>
+        /// Constructor for <see cref="float"/> <paramref name="value"/>. Makes the <see cref="ToString()"/> method 
+        /// print the <paramref name="value"/> as a <see cref="float"/> with at least 1 number after the decimal.
+        /// </summary>
         public LabelAndValue(string label, float value)
         {
             this.label = label;
             this.value = value;
+            this.typeOfValue = typeof(float);
+        }
+
+        /// <summary>
+        /// Constructor for <see cref="int"/> <paramref name="value"/>. Makes the <see cref="ToString()"/> method 
+        /// print the <paramref name="value"/> as an <see cref="int"/> with no decimal.
+        /// </summary>
+        public LabelAndValue(string label, int value)
+        {
+            this.label = label;
+            this.value = value;
+            this.typeOfValue = typeof(int);
         }
 
         public override string ToString()
         {
-            return label + " (" + value + ")";
+            string valueString = (typeOfValue == typeof(int)) 
+                ? value.ToString("0", CultureInfo.InvariantCulture) 
+                : value.ToString("0.0#####", CultureInfo.InvariantCulture);
+            return label + " (" + valueString + ")";
         }
 
         public override bool Equals(object _other)

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/abf096ef/src/Lucene.Net.Facet/Taxonomy/FloatAssociationFacetField.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Facet/Taxonomy/FloatAssociationFacetField.cs b/src/Lucene.Net.Facet/Taxonomy/FloatAssociationFacetField.cs
index cc90e61..54e8ef2 100644
--- a/src/Lucene.Net.Facet/Taxonomy/FloatAssociationFacetField.cs
+++ b/src/Lucene.Net.Facet/Taxonomy/FloatAssociationFacetField.cs
@@ -1,4 +1,5 @@
 \ufeffusing Lucene.Net.Support;
+using System.Globalization;
 
 namespace Lucene.Net.Facet.Taxonomy
 {
@@ -58,7 +59,7 @@ namespace Lucene.Net.Facet.Taxonomy
 
 	  public override string ToString()
 	  {
-		return "FloatAssociationFacetField(dim=" + dim + " path=" + Arrays.ToString(path) + " value=" + bytesRefToFloat(assoc) + ")";
+		return "FloatAssociationFacetField(dim=" + dim + " path=" + Arrays.ToString(path) + " value=" + bytesRefToFloat(assoc).ToString("0.0#####", CultureInfo.InvariantCulture) + ")";
 	  }
 	}