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 2019/07/09 21:29:44 UTC
[lucenenet] 28/29: Added missing guard clauses for HashMap and
LinkedHashMap constructors
This is an automated email from the ASF dual-hosted git repository.
nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git
commit 66c27e106bf18e16024cdfbcc41aa0415a0611e2
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Wed Jul 10 00:33:41 2019 +0700
Added missing guard clauses for HashMap and LinkedHashMap constructors
---
src/Lucene.Net/Support/HashMap.cs | 11 +++++++++--
src/Lucene.Net/Support/LinkedHashMap.cs | 26 ++++++++++++++------------
2 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/src/Lucene.Net/Support/HashMap.cs b/src/Lucene.Net/Support/HashMap.cs
index f099404..db8723e 100644
--- a/src/Lucene.Net/Support/HashMap.cs
+++ b/src/Lucene.Net/Support/HashMap.cs
@@ -107,6 +107,9 @@ namespace Lucene.Net.Support
public HashMap(IEnumerable<KeyValuePair<TKey, TValue>> other)
: this(0)
{
+ if (other == null)
+ throw new ArgumentNullException(nameof(other));
+
foreach (var kvp in other)
{
Add(kvp.Key, kvp.Value);
@@ -115,8 +118,12 @@ namespace Lucene.Net.Support
internal HashMap(IDictionary<TKey, TValue> wrappedDict, IEqualityComparer<TKey> comparer)
{
- // LUCENENET TODO: Is this a bug? Shouldn't we be using the passed in comparer if non-null?
- _comparer = /* comparer ?? */ EqualityComparer<TKey>.Default;
+ if (wrappedDict == null)
+ throw new ArgumentNullException(nameof(wrappedDict));
+ if (comparer == null)
+ throw new ArgumentNullException(nameof(comparer));
+
+ _comparer = comparer;
_dict = wrappedDict;
_hasNullValue = false;
diff --git a/src/Lucene.Net/Support/LinkedHashMap.cs b/src/Lucene.Net/Support/LinkedHashMap.cs
index e3d9c94..6019f6e 100644
--- a/src/Lucene.Net/Support/LinkedHashMap.cs
+++ b/src/Lucene.Net/Support/LinkedHashMap.cs
@@ -69,25 +69,27 @@ namespace Lucene.Net.Support
#region Constructors
public LinkedHashMap()
+ : this(0, EqualityComparer<TKey>.Default)
{
- dict = new HashMap<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>();
- list = new LinkedList<KeyValuePair<TKey, TValue>>();
}
public LinkedHashMap(IEqualityComparer<TKey> comparer)
+ : this(0, comparer)
{
- dict = new HashMap<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>(comparer);
- list = new LinkedList<KeyValuePair<TKey, TValue>>();
}
public LinkedHashMap(int capacity)
+ : this(capacity, EqualityComparer<TKey>.Default)
{
- dict = new HashMap<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>(capacity);
- list = new LinkedList<KeyValuePair<TKey, TValue>>();
}
public LinkedHashMap(int capacity, IEqualityComparer<TKey> comparer)
{
+ if (capacity < 0)
+ throw new ArgumentOutOfRangeException($"{nameof(capacity)} must be 0 or greater.");
+ if (comparer == null)
+ throw new ArgumentNullException(nameof(comparer));
+
dict = new HashMap<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>(capacity, comparer);
list = new LinkedList<KeyValuePair<TKey, TValue>>();
}
@@ -95,9 +97,8 @@ namespace Lucene.Net.Support
public LinkedHashMap(IEnumerable<KeyValuePair<TKey, TValue>> source)
{
if (source == null)
- {
- throw new ArgumentNullException("source");
- }
+ throw new ArgumentNullException(nameof(source));
+
var countable = source as ICollection;
if (countable != null)
{
@@ -117,9 +118,10 @@ namespace Lucene.Net.Support
public LinkedHashMap(IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
{
if (source == null)
- {
- throw new ArgumentNullException("source");
- }
+ throw new ArgumentNullException(nameof(source));
+ if (comparer == null)
+ throw new ArgumentNullException(nameof(comparer));
+
var countable = source as ICollection;
if (countable != null)
{