From a8648898d9e80106b80b2393a0633f4445d9caa7 Mon Sep 17 00:00:00 2001 From: manognyab <100461186+manognya-b@users.noreply.github.com> Date: Tue, 7 Jan 2025 19:16:53 +0000 Subject: [PATCH] Refactor code: Replace string concatenation with string interpolation and change static readonly to const, #1077 --- .../Analysis/NGram/EdgeNGramTokenizerFactory.cs | 2 +- .../Analysis/Util/AnalysisSPILoader.cs | 2 +- .../Codecs/Lucene3x/Lucene3xSegmentInfoFormat.cs | 10 +++++----- .../Codecs/Lucene40/Lucene40FieldInfosReader.cs | 4 ++-- .../Codecs/PerField/PerFieldDocValuesFormat.cs | 4 ++-- .../Codecs/PerField/PerFieldPostingsFormat.cs | 4 ++-- src/Lucene.Net/Support/Util/NamedServiceFactory.cs | 2 +- src/Lucene.Net/Util/CommandLineUtil.cs | 8 ++++---- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Lucene.Net.Analysis.Common/Analysis/NGram/EdgeNGramTokenizerFactory.cs b/src/Lucene.Net.Analysis.Common/Analysis/NGram/EdgeNGramTokenizerFactory.cs index b788cd1813..406bbec880 100644 --- a/src/Lucene.Net.Analysis.Common/Analysis/NGram/EdgeNGramTokenizerFactory.cs +++ b/src/Lucene.Net.Analysis.Common/Analysis/NGram/EdgeNGramTokenizerFactory.cs @@ -61,7 +61,7 @@ public override Tokenizer Create(AttributeSource.AttributeFactory factory, TextR EdgeNGramTokenFilter.Side sideEnum; if (!Enum.TryParse(this.side, true, out sideEnum)) { - throw new ArgumentException(nameof(EdgeNGramTokenizer) + " does not support backward n-grams as of Lucene 4.4"); + throw new ArgumentException($"{nameof(EdgeNGramTokenizer)} does not support backward n-grams as of Lucene 4.4"); } return new EdgeNGramTokenizer(m_luceneMatchVersion, input, minGramSize, maxGramSize); } diff --git a/src/Lucene.Net.Analysis.Common/Analysis/Util/AnalysisSPILoader.cs b/src/Lucene.Net.Analysis.Common/Analysis/Util/AnalysisSPILoader.cs index 284fc59876..f362acd0cf 100644 --- a/src/Lucene.Net.Analysis.Common/Analysis/Util/AnalysisSPILoader.cs +++ b/src/Lucene.Net.Analysis.Common/Analysis/Util/AnalysisSPILoader.cs @@ -36,7 +36,7 @@ internal sealed class AnalysisSPILoader where S : AbstractAnalysisFactory private readonly string[] suffixes; public AnalysisSPILoader() - : this(new string[] { nameof(S) }) + : this(new string[] { typeof(S).Name }) { } diff --git a/src/Lucene.Net/Codecs/Lucene3x/Lucene3xSegmentInfoFormat.cs b/src/Lucene.Net/Codecs/Lucene3x/Lucene3xSegmentInfoFormat.cs index 2c697d978f..ce768214bd 100644 --- a/src/Lucene.Net/Codecs/Lucene3x/Lucene3xSegmentInfoFormat.cs +++ b/src/Lucene.Net/Codecs/Lucene3x/Lucene3xSegmentInfoFormat.cs @@ -62,12 +62,12 @@ public class Lucene3xSegmentInfoFormat : SegmentInfoFormat public override SegmentInfoWriter SegmentInfoWriter => throw UnsupportedOperationException.Create("this codec can only be used for reading"); // only for backwards compat - public static readonly string DS_OFFSET_KEY = nameof(Lucene3xSegmentInfoFormat) + ".dsoffset"; + public const string DS_OFFSET_KEY = nameof(Lucene3xSegmentInfoFormat) + ".dsoffset"; - public static readonly string DS_NAME_KEY = nameof(Lucene3xSegmentInfoFormat) + ".dsname"; - public static readonly string DS_COMPOUND_KEY = nameof(Lucene3xSegmentInfoFormat) + ".dscompound"; - public static readonly string NORMGEN_KEY = nameof(Lucene3xSegmentInfoFormat) + ".normgen"; - public static readonly string NORMGEN_PREFIX = nameof(Lucene3xSegmentInfoFormat) + ".normfield"; + public const string DS_NAME_KEY = nameof(Lucene3xSegmentInfoFormat) + ".dsname"; + public const string DS_COMPOUND_KEY = nameof(Lucene3xSegmentInfoFormat) + ".dscompound"; + public const string NORMGEN_KEY = nameof(Lucene3xSegmentInfoFormat) + ".normgen"; + public const string NORMGEN_PREFIX = nameof(Lucene3xSegmentInfoFormat) + ".normfield"; /// If this segment shares stored fields & vectors, this /// offset is where in that file this segment's docs begin. diff --git a/src/Lucene.Net/Codecs/Lucene40/Lucene40FieldInfosReader.cs b/src/Lucene.Net/Codecs/Lucene40/Lucene40FieldInfosReader.cs index 675b3cbc80..06a29e06db 100644 --- a/src/Lucene.Net/Codecs/Lucene40/Lucene40FieldInfosReader.cs +++ b/src/Lucene.Net/Codecs/Lucene40/Lucene40FieldInfosReader.cs @@ -137,8 +137,8 @@ public override FieldInfos Read(Directory directory, string segmentName, string } } - internal static readonly string LEGACY_DV_TYPE_KEY = nameof(Lucene40FieldInfosReader) + ".dvtype"; - internal static readonly string LEGACY_NORM_TYPE_KEY = nameof(Lucene40FieldInfosReader) + ".normtype"; + internal const string LEGACY_DV_TYPE_KEY = nameof(Lucene40FieldInfosReader) + ".dvtype"; + internal const string LEGACY_NORM_TYPE_KEY = nameof(Lucene40FieldInfosReader) + ".normtype"; // mapping of 4.0 types -> 4.2 types /*internal enum LegacyDocValuesType diff --git a/src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs b/src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs index c700cc79eb..9e1e681c7c 100644 --- a/src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs +++ b/src/Lucene.Net/Codecs/PerField/PerFieldDocValuesFormat.cs @@ -66,13 +66,13 @@ public abstract class PerFieldDocValuesFormat : DocValuesFormat /// attribute name used to store the /// format name for each field. /// - public static readonly string PER_FIELD_FORMAT_KEY = nameof(PerFieldDocValuesFormat) + ".format"; + public const string PER_FIELD_FORMAT_KEY = nameof(PerFieldDocValuesFormat) + ".format"; /// /// attribute name used to store the /// segment suffix name for each field. /// - public static readonly string PER_FIELD_SUFFIX_KEY = nameof(PerFieldDocValuesFormat) + ".suffix"; + public const string PER_FIELD_SUFFIX_KEY = nameof(PerFieldDocValuesFormat) + ".suffix"; /// /// Sole constructor. diff --git a/src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs b/src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs index a85132101c..1ffb28c9db 100644 --- a/src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs +++ b/src/Lucene.Net/Codecs/PerField/PerFieldPostingsFormat.cs @@ -60,13 +60,13 @@ public abstract class PerFieldPostingsFormat : PostingsFormat /// attribute name used to store the /// format name for each field. /// - public static readonly string PER_FIELD_FORMAT_KEY = nameof(PerFieldPostingsFormat) + ".format"; + public const string PER_FIELD_FORMAT_KEY = nameof(PerFieldPostingsFormat) + ".format"; /// /// attribute name used to store the /// segment suffix name for each field. /// - public static readonly string PER_FIELD_SUFFIX_KEY = nameof(PerFieldPostingsFormat) + ".suffix"; + public const string PER_FIELD_SUFFIX_KEY = nameof(PerFieldPostingsFormat) + ".suffix"; /// /// Sole constructor. diff --git a/src/Lucene.Net/Support/Util/NamedServiceFactory.cs b/src/Lucene.Net/Support/Util/NamedServiceFactory.cs index bef0175947..8174607b34 100644 --- a/src/Lucene.Net/Support/Util/NamedServiceFactory.cs +++ b/src/Lucene.Net/Support/Util/NamedServiceFactory.cs @@ -129,7 +129,7 @@ protected static string GetCanonicalName(Type type) genericSuffix = "Generic" + name.Substring(genericIndex + 1); name = name.Substring(0, genericIndex); } - string serviceName = nameof(TService); + string serviceName = typeof(TService).Name; if (name.EndsWith(serviceName, StringComparison.Ordinal)) { name = name.Substring(0, name.Length - serviceName.Length); diff --git a/src/Lucene.Net/Util/CommandLineUtil.cs b/src/Lucene.Net/Util/CommandLineUtil.cs index 9dfe3e9a87..e0496d6110 100644 --- a/src/Lucene.Net/Util/CommandLineUtil.cs +++ b/src/Lucene.Net/Util/CommandLineUtil.cs @@ -43,21 +43,21 @@ public static FSDirectory NewFSDirectory(string clazzName, DirectoryInfo dir) // LUCENENET: In .NET, we get a null when the class is not found, so we need to throw here for compatibility if (clazz is null) - throw new ArgumentException(nameof(FSDirectory) + " implementation not found: " + clazzName); + throw new ArgumentException($"{nameof(FSDirectory)} implementation not found: {clazzName}"); return NewFSDirectory(clazz, dir); } catch (Exception e) when (e.IsClassNotFoundException()) { - throw new ArgumentException(nameof(FSDirectory) + " implementation not found: " + clazzName, e); + throw new ArgumentException($"{nameof(FSDirectory)} implementation not found: {clazzName}", e); } catch (Exception e) when (e.IsClassCastException()) { - throw new ArgumentException(clazzName + " is not a " + nameof(FSDirectory) + " implementation", e); + throw new ArgumentException($"{clazzName} is not a {nameof(FSDirectory)} implementation", e); } catch (Exception e) when (e.IsNoSuchMethodException()) { - throw new ArgumentException(clazzName + " constructor with " + nameof(FileInfo) + " as parameter not found", e); + throw new ArgumentException($"{clazzName} constructor with {nameof(FileInfo)} as parameter not found", e); } catch (Exception e) {