lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [18/21] lucenenet git commit: Added PutXXX protected methods to the DefaultCodecFactory, DefaultDocValuesFormatFactory, and DefaultPostingsFormatFactory so their maps can be private, otherwise it would be possible to add a codec using a name that doesn't
Date Sun, 12 Mar 2017 16:38:17 GMT
Added PutXXX protected methods to the DefaultCodecFactory, DefaultDocValuesFormatFactory, and
DefaultPostingsFormatFactory so their maps can be private, otherwise it would be possible
to add a codec using a name that doesn't match its Name property (making it unusable).


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

Branch: refs/heads/api-work
Commit: 6d7613c0300b3a7c921d39dc8631ad84ca50a487
Parents: e61ae2e
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Sun Mar 12 20:44:54 2017 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Sun Mar 12 23:21:34 2017 +0700

----------------------------------------------------------------------
 .../Support/Codecs/DefaultCodecFactory.cs       | 46 ++++++++++++++++----
 .../Codecs/DefaultDocValuesFormatFactory.cs     | 46 ++++++++++++++++----
 .../Codecs/DefaultPostingsFormatFactory.cs      | 46 ++++++++++++++++----
 3 files changed, 111 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/6d7613c0/src/Lucene.Net.Core/Support/Codecs/DefaultCodecFactory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/Codecs/DefaultCodecFactory.cs b/src/Lucene.Net.Core/Support/Codecs/DefaultCodecFactory.cs
index 8906cd7..e5273bb 100644
--- a/src/Lucene.Net.Core/Support/Codecs/DefaultCodecFactory.cs
+++ b/src/Lucene.Net.Core/Support/Codecs/DefaultCodecFactory.cs
@@ -18,12 +18,12 @@ namespace Lucene.Net.Codecs
     ///         overriding <see cref="GetCodec(string)"/>.</item>
     ///     <item>subclass <see cref="DefaultCodecFactory"/> and override
     ///         <see cref="DefaultCodecFactory.GetCodecType(string)"/> so a type new
type can be
-    ///         supplied that is not in the <see cref="DefaultCodecFactory.m_codecNameToTypeMap"/>.</item>
+    ///         supplied that is not in the <see cref="DefaultCodecFactory.codecNameToTypeMap"/>.</item>
     ///     <item>subclass <see cref="DefaultCodecFactory"/> to scan additional
assemblies for <see cref="Codec"/>
     ///         subclasses in the constructor by calling <see cref="ScanForCodecs(Assembly)"/>.

     ///         For performance reasons, the default behavior only loads Lucene.Net codecs.</item>
     ///     <item>subclass <see cref="DefaultCodecFactory"/> to add override
the default <see cref="Codec"/> 
-    ///         types by explicitly setting them in the <see cref="DefaultCodecFactory.m_codecNameToTypeMap"/>.</item>
+    ///         types by calling <see cref="PutCodecType(Type)"/>.</item>
     /// </list>
     /// <para/>
     /// To set the <see cref="ICodecFactory"/>, call <see cref="Codec.SetCodecFactory(ICodecFactory)"/>.
@@ -32,7 +32,7 @@ namespace Lucene.Net.Codecs
     {
         // NOTE: The following 2 dictionaries are static, since this instance is stored in
a static
         // variable in the Codec class.
-        protected readonly IDictionary<string, Type> m_codecNameToTypeMap = new Dictionary<string,
Type>();
+        private readonly IDictionary<string, Type> codecNameToTypeMap = new Dictionary<string,
Type>();
         private readonly IDictionary<Type, Codec> codecInstanceCache = new Dictionary<Type,
Codec>();
 
         public DefaultCodecFactory()
@@ -45,7 +45,7 @@ namespace Lucene.Net.Codecs
 
         /// <summary>
         /// Scans the given <paramref name="assemblies"/> for subclasses of <see
cref="Codec"/>
-        /// and adds their names to the <see cref="m_codecNameToTypeMap"/>. Note that
names will be
+        /// and adds their names to the <see cref="codecNameToTypeMap"/>. Note that
names will be
         /// automatically overridden if the <see cref="Codec"/> name appears multiple
times - the last match wins.
         /// </summary>
         /// <param name="assemblies">A list of assemblies to scan. The assemblies will
be scanned from first to last, 
@@ -60,7 +60,7 @@ namespace Lucene.Net.Codecs
 
         /// <summary>
         /// Scans the given <paramref name="assembly"/> for subclasses of <see cref="Codec"/>
-        /// and adds their names to the <see cref="m_codecNameToTypeMap"/>. Note that
names will be
+        /// and adds their names to the <see cref="codecNameToTypeMap"/>. Note that
names will be
         /// automatically overridden if the <see cref="Codec"/> name appears multiple
times - the last match wins.
         /// </summary>
         /// <param name="assembly">The assembly to scan.</param>
@@ -72,13 +72,41 @@ namespace Lucene.Net.Codecs
             {
                 if (IsServiceType(c))
                 {
-                    string name = GetServiceName(c);
-                    m_codecNameToTypeMap[name] = c;
+                    PutCodecTypeImpl(c);
                 }
             }
         }
 
         /// <summary>
+        /// Adds a <see cref="Codec"/> type to the <see cref="codecNameToTypeMap"/>,
using 
+        /// the name provided in the <see cref="CodecNameAttribute"/>, if present,
or the name
+        /// of the codec class minus the "Codec" suffix as the name by default.
+        /// <para/>
+        /// Note that if a <see cref="Codec"/> with the same name already exists in
the map,
+        /// calling this method will update it to the new type.
+        /// </summary>
+        /// <param name="codec">A type that subclasses <see cref="Codec"/>.</param>
+        protected virtual void PutCodecType(Type codec)
+        {
+            if (codec == null)
+            {
+                throw new ArgumentNullException("codec", "codec may not be null");
+            }
+            if (!typeof(Codec).GetTypeInfo().IsAssignableFrom(codec))
+            {
+                throw new ArgumentException("System.Type passed dose not subclass Codec.");
+            }
+
+            PutCodecTypeImpl(codec);
+        }
+
+        private void PutCodecTypeImpl(Type codec)
+        {
+            string name = GetServiceName(codec);
+            codecNameToTypeMap[name] = codec;
+        }
+
+        /// <summary>
         /// Gets the <see cref="Codec"/> instance from the provided <paramref name="name"/>.
         /// </summary>
         /// <param name="name">The name of the <see cref="Codec"/> instance to
retrieve.</param>
@@ -115,7 +143,7 @@ namespace Lucene.Net.Codecs
         protected virtual Type GetCodecType(string name)
         {
             Type codecType;
-            m_codecNameToTypeMap.TryGetValue(name, out codecType);
+            codecNameToTypeMap.TryGetValue(name, out codecType);
             if (codecType == null)
             {
                 throw new ArgumentException(string.Format("Codec '{0}' cannot be loaded.
If the codec is not " +
@@ -132,7 +160,7 @@ namespace Lucene.Net.Codecs
         /// <returns>A <see cref="T:ICollection{string}"/> of <see cref="Codec"/>
names.</returns>
         public ICollection<string> AvailableServices()
         {
-            return m_codecNameToTypeMap.Keys;
+            return codecNameToTypeMap.Keys;
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/6d7613c0/src/Lucene.Net.Core/Support/Codecs/DefaultDocValuesFormatFactory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/Codecs/DefaultDocValuesFormatFactory.cs b/src/Lucene.Net.Core/Support/Codecs/DefaultDocValuesFormatFactory.cs
index 772026c..5b838a6 100644
--- a/src/Lucene.Net.Core/Support/Codecs/DefaultDocValuesFormatFactory.cs
+++ b/src/Lucene.Net.Core/Support/Codecs/DefaultDocValuesFormatFactory.cs
@@ -18,12 +18,12 @@ namespace Lucene.Net.Codecs
     ///         overriding <see cref="GetDocValuesFormat(string)"/>.</item>
     ///     <item>subclass <see cref="DefaultDocValuesFormatFactory"/> and override
     ///         <see cref="DefaultDocValuesFormatFactory.GetDocValuesFormatType(string)"/>
so a type new type can be
-    ///         supplied that is not in the <see cref="DefaultDocValuesFormatFactory.m_docValuesFormatNameToTypeMap"/>.</item>
+    ///         supplied that is not in the <see cref="DefaultDocValuesFormatFactory.docValuesFormatNameToTypeMap"/>.</item>
     ///     <item>subclass <see cref="DefaultDocValuesFormatFactory"/> to scan
additional assemblies for <see cref="DocValuesFormat"/>
     ///         subclasses in the constructor by calling <see cref="ScanForDocValuesFormats(Assembly)"/>.

     ///         For performance reasons, the default behavior only loads Lucene.Net codecs.</item>
     ///     <item>subclass <see cref="DefaultDocValuesFormatFactory"/> to add
override the default <see cref="DocValuesFormat"/> 
-    ///         types by explicitly setting them in the <see cref="DefaultDocValuesFormatFactory.m_docValuesFormatNameToTypeMap"/>.</item>
+    ///         types by calling <see cref="PutDocValuesFormatType(Type)"/>.</item>
     /// </list>
     /// <para/>
     /// To set the <see cref="IDocValuesFormatFactory"/>, call <see cref="DocValuesFormat.SetDocValuesFormatFactory(IDocValuesFormatFactory)"/>.
@@ -32,7 +32,7 @@ namespace Lucene.Net.Codecs
     {
         // NOTE: The following 2 dictionaries are static, since this instance is stored in
a static
         // variable in the Codec class.
-        protected readonly IDictionary<string, Type> m_docValuesFormatNameToTypeMap
= new Dictionary<string, Type>();
+        private readonly IDictionary<string, Type> docValuesFormatNameToTypeMap = new
Dictionary<string, Type>();
         private readonly IDictionary<Type, DocValuesFormat> docValuesFormatInstanceCache
= new Dictionary<Type, DocValuesFormat>();
 
         public DefaultDocValuesFormatFactory()
@@ -45,7 +45,7 @@ namespace Lucene.Net.Codecs
 
         /// <summary>
         /// Scans the given <paramref name="assemblies"/> for subclasses of <see
cref="Codec"/>
-        /// and adds their names to the <see cref="m_docValuesFormatNameToTypeMap"/>.
Note that names will be
+        /// and adds their names to the <see cref="docValuesFormatNameToTypeMap"/>.
Note that names will be
         /// automatically overridden if the <see cref="DocValuesFormat"/> name appears
multiple times - the last match wins.
         /// </summary>
         /// <param name="assemblies">A list of assemblies to scan. The assemblies will
be scanned from first to last, 
@@ -60,7 +60,7 @@ namespace Lucene.Net.Codecs
 
         /// <summary>
         /// Scans the given <paramref name="assembly"/> for subclasses of <see cref="DocValuesFormat"/>
-        /// and adds their names to the <see cref="m_docValuesFormatNameToTypeMap"/>.
Note that names will be
+        /// and adds their names to the <see cref="docValuesFormatNameToTypeMap"/>.
Note that names will be
         /// automatically overridden if the <see cref="DocValuesFormat"/> name appears
multiple times - the last match wins.
         /// </summary>
         /// <param name="assembly">The assembly to scan.</param>
@@ -72,13 +72,41 @@ namespace Lucene.Net.Codecs
             {
                 if (IsServiceType(c))
                 {
-                    string name = GetServiceName(c);
-                    m_docValuesFormatNameToTypeMap[name] = c;
+                    PutCodecTypeImpl(c);
                 }
             }
         }
 
         /// <summary>
+        /// Adds a <see cref="DocValuesFormat"/> type to the <see cref="docValuesFormatNameToTypeMap"/>,
using 
+        /// the name provided in the <see cref="DocValuesFormatNameAttribute"/>, if
present, or the name
+        /// of the codec class minus the "DocValuesFormat" suffix as the name by default.
+        /// <para/>
+        /// Note that if a <see cref="DocValuesFormat"/> with the same name already
exists in the map,
+        /// calling this method will update it to the new type.
+        /// </summary>
+        /// <param name="docValuesFormat">A type that subclasses <see cref="DocValuesFormat"/>.</param>
+        protected virtual void PutDocValuesFormatType(Type docValuesFormat)
+        {
+            if (docValuesFormat == null)
+            {
+                throw new ArgumentNullException("docValuesFormat", "docValuesFormat may not
be null");
+            }
+            if (!typeof(DocValuesFormat).GetTypeInfo().IsAssignableFrom(docValuesFormat))
+            {
+                throw new ArgumentException("System.Type passed dose not subclass DocValuesFormat.");
+            }
+
+            PutCodecTypeImpl(docValuesFormat);
+        }
+
+        private void PutCodecTypeImpl(Type docValuesFormat)
+        {
+            string name = GetServiceName(docValuesFormat);
+            docValuesFormatNameToTypeMap[name] = docValuesFormat;
+        }
+
+        /// <summary>
         /// Gets the <see cref="DocValuesFormat"/> instance from the provided <paramref
name="name"/>.
         /// </summary>
         /// <param name="name">The name of the <see cref="DocValuesFormat"/>
instance to retrieve.</param>
@@ -115,7 +143,7 @@ namespace Lucene.Net.Codecs
         protected virtual Type GetDocValuesFormatType(string name)
         {
             Type codecType;
-            m_docValuesFormatNameToTypeMap.TryGetValue(name, out codecType);
+            docValuesFormatNameToTypeMap.TryGetValue(name, out codecType);
             if (codecType == null)
             {
                 throw new ArgumentException(string.Format("DocValuesFormat '{0}' cannot be
loaded. If the format is not " +
@@ -132,7 +160,7 @@ namespace Lucene.Net.Codecs
         /// <returns>A <see cref="ICollection{string}"/> of <see cref="DocValuesFormat"/>
names.</returns>
         public ICollection<string> AvailableServices()
         {
-            return m_docValuesFormatNameToTypeMap.Keys;
+            return docValuesFormatNameToTypeMap.Keys;
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/6d7613c0/src/Lucene.Net.Core/Support/Codecs/DefaultPostingsFormatFactory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/Codecs/DefaultPostingsFormatFactory.cs b/src/Lucene.Net.Core/Support/Codecs/DefaultPostingsFormatFactory.cs
index d9e0511..b1e33e8 100644
--- a/src/Lucene.Net.Core/Support/Codecs/DefaultPostingsFormatFactory.cs
+++ b/src/Lucene.Net.Core/Support/Codecs/DefaultPostingsFormatFactory.cs
@@ -18,12 +18,12 @@ namespace Lucene.Net.Codecs
     ///         overriding <see cref="GetDocValuesFormat(string)"/>.</item>
     ///     <item>subclass <see cref="DefaultDPostingsFormatFactory"/> and override
     ///         <see cref="DefaultPostingsFormatFactory.GetPostingsFormatType(string)"/>
so a type new type can be
-    ///         supplied that is not in the <see cref="DefaultPostingsFormatFactory.m_postingsFormatNameToTypeMap"/>.</item>
+    ///         supplied that is not in the <see cref="DefaultPostingsFormatFactory.postingsFormatNameToTypeMap"/>.</item>
     ///     <item>subclass <see cref="DefaultPostingsFormatFactory"/> to scan
additional assemblies for <see cref="PostingsFormat"/>
     ///         subclasses in the constructor by calling <see cref="ScanForPostingsFormats(Assembly)"/>.

     ///         For performance reasons, the default behavior only loads Lucene.Net codecs.</item>
     ///     <item>subclass <see cref="DefaultPostingsFormatFactory"/> to add
override the default <see cref="PostingsFormat"/> 
-    ///         types by explicitly setting them in the <see cref="DefaultPostingsFormatFactory.m_postingsFormatNameToTypeMap"/>.</item>
+    ///         types by calling <see cref="PutPostingsFormatType(Type)"/>.</item>
     /// </list>
     /// <para/>
     /// To set the <see cref="IPostingsFormatFactory"/>, call <see cref="DocValuesFormat.SetPostingsFormatFactory(IPostingsFormatFactory)"/>.
@@ -32,7 +32,7 @@ namespace Lucene.Net.Codecs
     {
         // NOTE: The following 2 dictionaries are static, since this instance is stored in
a static
         // variable in the Codec class.
-        protected readonly IDictionary<string, Type> m_postingsFormatNameToTypeMap
= new Dictionary<string, Type>();
+        private readonly IDictionary<string, Type> postingsFormatNameToTypeMap = new
Dictionary<string, Type>();
         private readonly IDictionary<Type, PostingsFormat> postingsFormatInstanceCache
= new Dictionary<Type, PostingsFormat>();
 
         public DefaultPostingsFormatFactory()
@@ -45,7 +45,7 @@ namespace Lucene.Net.Codecs
 
         /// <summary>
         /// Scans the given <paramref name="assemblies"/> for subclasses of <see
cref="Codec"/>
-        /// and adds their names to the <see cref="m_postingsFormatNameToTypeMap"/>.
Note that names will be
+        /// and adds their names to the <see cref="postingsFormatNameToTypeMap"/>.
Note that names will be
         /// automatically overridden if the <see cref="PostingsFormat"/> name appears
multiple times - the last match wins.
         /// </summary>
         /// <param name="assemblies">A list of assemblies to scan. The assemblies will
be scanned from first to last, 
@@ -60,7 +60,7 @@ namespace Lucene.Net.Codecs
 
         /// <summary>
         /// Scans the given <paramref name="assembly"/> for subclasses of <see cref="PostingsFormat"/>
-        /// and adds their names to the <see cref="m_postingsFormatNameToTypeMap"/>.
Note that names will be
+        /// and adds their names to the <see cref="postingsFormatNameToTypeMap"/>.
Note that names will be
         /// automatically overridden if the <see cref="PostingsFormat"/> name appears
multiple times - the last match wins.
         /// </summary>
         /// <param name="assembly">The assembly to scan.</param>
@@ -72,13 +72,41 @@ namespace Lucene.Net.Codecs
             {
                 if (IsServiceType(c))
                 {
-                    string name = GetServiceName(c);
-                    m_postingsFormatNameToTypeMap[name] = c;
+                    PutPostingsFormatTypeImpl(c);
                 }
             }
         }
 
         /// <summary>
+        /// Adds a <see cref="PostingsFormat"/> type to the <see cref="postingsFormaNameToTypeMap"/>,
using 
+        /// the name provided in the <see cref="PostingsFormatNameAttribute"/>, if
present, or the name
+        /// of the codec class minus the "Codec" suffix as the name by default.
+        /// <para/>
+        /// Note that if a <see cref="PostingsFormat"/> with the same name already
exists in the map,
+        /// calling this method will update it to the new type.
+        /// </summary>
+        /// <param name="postingsFormat">A type that subclasses <see cref="PostingsFormat"/>.</param>
+        protected virtual void PutPostingsFormatType(Type postingsFormat)
+        {
+            if (postingsFormat == null)
+            {
+                throw new ArgumentNullException("postingsFormat", "postingsFormat may not
be null");
+            }
+            if (!typeof(PostingsFormat).GetTypeInfo().IsAssignableFrom(postingsFormat))
+            {
+                throw new ArgumentException("System.Type passed dose not subclass PostingsFormat.");
+            }
+
+            PutPostingsFormatTypeImpl(postingsFormat);
+        }
+
+        private void PutPostingsFormatTypeImpl(Type postingsFormat)
+        {
+            string name = GetServiceName(postingsFormat);
+            postingsFormatNameToTypeMap[name] = postingsFormat;
+        }
+
+        /// <summary>
         /// Gets the <see cref="PostingsFormat"/> instance from the provided <paramref
name="name"/>.
         /// </summary>
         /// <param name="name">The name of the <see cref="PostingsFormat"/> instance
to retrieve.</param>
@@ -115,7 +143,7 @@ namespace Lucene.Net.Codecs
         protected virtual Type GetPostingsFormatType(string name)
         {
             Type codecType;
-            m_postingsFormatNameToTypeMap.TryGetValue(name, out codecType);
+            postingsFormatNameToTypeMap.TryGetValue(name, out codecType);
             if (codecType == null)
             {
                 throw new ArgumentException(string.Format("PostingsFormat '{0}' cannot be
loaded. If the format is not " +
@@ -132,7 +160,7 @@ namespace Lucene.Net.Codecs
         /// <returns>A <see cref="ICollection{string}"/> of <see cref="PostingsFormat"/>
names.</returns>
         public ICollection<string> AvailableServices()
         {
-            return m_postingsFormatNameToTypeMap.Keys;
+            return postingsFormatNameToTypeMap.Keys;
         }
     }
 }


Mime
View raw message