lucenenet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [lucenenet] NightOwl888 commented on pull request #385: Adding overloads to Document for GetValues and Get
Date Wed, 18 Nov 2020 19:31:09 GMT

NightOwl888 commented on pull request #385:
URL: https://github.com/apache/lucenenet/pull/385#issuecomment-729904933


   I guess it is simpler just to show the code than to describe it.
   
   We test for `IFormattable` in the if statement to make it an optional feature of implementations
of either `IIndexableField` or `FieldsValue`. If the type implements `IFormattable`, we call
`IFormattable.ToString(string, IFormatProvider)`. If not, we just skip formatting and call
`ToString()` on the object. This makes it possible to use an `IIndexableField` implementation
that has no `ToString()` implementations on it or to implement both `IIndexableField` and
`IFormattable` to provide localizable formatting support.
   
   ### In all implementations of IIndexableField (4 overloads of `ToString()`)
   
   ```c#
   public override string ToString()
   {
       return ToString(null, J2N.Text.StringFormatter.CurrentCulture);
   }
   
   public virtual string ToString(string format)
   {
       return ToString(format, J2N.Text.StringFormatter.CurrentCulture);
   }
   
   public virtual string ToString(IFormatProvider provider)
   {
       return ToString(null, provider);
   }
   
   public virtual string ToString(string format, IFormatProvider provider)
   {
       StringBuilder result = new StringBuilder();
       result.Append(m_type.ToString());
       result.Append('<');
       result.Append(m_name);
       result.Append(':');
   
       if (FieldsData != null)
       {
           if (FieldsData is IFormattable formattable)
               result.Append(formattable.ToString(format, provider);
           else
               result.Append(FieldsData.ToString());
       }
   
       result.Append('>');
       return result.ToString();
   }
   ```
   
   ### In Document (2 public and one hidden overload of `ToString()`)
   
   ```c#
   
   public override string ToString()
   {
       return ToString(null, J2N.Text.StringFormatter.CurrentCulture);
   }
   
   public virtual string ToString(IFormatProvider provider)
   {
       return ToString(null, provider);
   }
   
   string IFormattable.ToString(string format, IFormatProvider provider) // <- explicit
implementation
   {
       var buffer = new StringBuilder();
       buffer.Append("Document<");
       for (int i = 0; i < fields.Count; i++)
       {
           IIndexableField field = fields[i];
           if (field is IFormattable formattable)
               buffer.Append(formattable.ToString(format, provider));
           else
               buffer.Append(field.ToString());
           if (i != fields.Count - 1)
           {
               buffer.Append(" ");
           }
       }
       buffer.Append(">");
       return buffer.ToString();
   }
   
   ```
   
   Be sure that all built-in `IIndexableField` types, the `Field.Number` type, and `Document`
all implement `IFormattable` themselves. 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message