Skip to content

Commit

Permalink
Lucene.Net.Analysis.Util (CharArraySet + CharArrayMap): Don't call ov…
Browse files Browse the repository at this point in the history
…erridable members in constructor (See apache#670). Added some missing guard clauses.
  • Loading branch information
NightOwl888 committed Oct 23, 2022
1 parent 5843aa1 commit 555e280
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
49 changes: 47 additions & 2 deletions src/Lucene.Net.Analysis.Common/Analysis/Util/CharArrayMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ public MapValue(TValue value)
/// otherwise <c>true</c>. </param>
public CharArrayMap(LuceneVersion matchVersion, int startSize, bool ignoreCase)
{
// LUCENENET: Added guard clause
if (startSize < 0)
throw new ArgumentOutOfRangeException(nameof(startSize), $"nameof(startSize) may not be negative.");

this.ignoreCase = ignoreCase;
var size = INIT_SIZE;
while (startSize + (startSize >> 2) > size)
Expand All @@ -139,11 +143,20 @@ public CharArrayMap(LuceneVersion matchVersion, int startSize, bool ignoreCase)
/// <c>false</c> if and only if the set should be case sensitive;
/// otherwise <c>true</c>. </param>
public CharArrayMap(LuceneVersion matchVersion, IDictionary<string, TValue> c, bool ignoreCase)
: this(matchVersion, c.Count, ignoreCase)
: this(matchVersion, c?.Count ?? 0, ignoreCase)
{
// LUCENENET: Added guard clause
if (c is null)
throw new ArgumentNullException(nameof(c));

foreach (var v in c)
{
Add(v);
// LUCENENET: S1699: Don't call call protected members in the constructor
if (ContainsKey(v.Key))
{
throw new ArgumentException("The key " + v.Key + " already exists in the dictionary");
}
PutImpl(v.Key, new MapValue(v.Value));
}
}

Expand Down Expand Up @@ -426,6 +439,10 @@ private int GetSlot(string text)
/// </summary>
public virtual TValue Put(ICharSequence text, TValue value)
{
// LUCENENET: Added guard clause
if (text is null)
throw new ArgumentNullException(nameof(text));

MapValue oldValue = PutImpl(text, new MapValue(value)); // could be more efficient
return (oldValue != null) ? oldValue.Value : default;
}
Expand All @@ -436,6 +453,10 @@ public virtual TValue Put(ICharSequence text, TValue value)
/// </summary>
public virtual TValue Put(object o, TValue value)
{
// LUCENENET: Added guard clause
if (o is null)
throw new ArgumentNullException(nameof(o));

MapValue oldValue = PutImpl(o, new MapValue(value));
return (oldValue != null) ? oldValue.Value : default;
}
Expand All @@ -445,6 +466,10 @@ public virtual TValue Put(object o, TValue value)
/// </summary>
public virtual TValue Put(string text, TValue value)
{
// LUCENENET: Added guard clause
if (text is null)
throw new ArgumentNullException(nameof(text));

MapValue oldValue = PutImpl(text, new MapValue(value));
return (oldValue != null) ? oldValue.Value : default;
}
Expand All @@ -456,6 +481,10 @@ public virtual TValue Put(string text, TValue value)
/// </summary>
public virtual TValue Put(char[] text, TValue value)
{
// LUCENENET: Added guard clause
if (text is null)
throw new ArgumentNullException(nameof(text));

MapValue oldValue = PutImpl(text, new MapValue(value));
return (oldValue != null) ? oldValue.Value : default;
}
Expand All @@ -465,11 +494,19 @@ public virtual TValue Put(char[] text, TValue value)
/// </summary>
private MapValue PutImpl(ICharSequence text, MapValue value)
{
// LUCENENET: Added guard clause
if (text is null)
throw new ArgumentNullException(nameof(text));

return PutImpl(text.ToString(), value); // could be more efficient
}

private MapValue PutImpl(object o, MapValue value)
{
// LUCENENET: Added guard clause
if (o is null)
throw new ArgumentNullException(nameof(o));

// LUCENENET NOTE: Testing for *is* is at least 10x faster
// than casting using *as* and then checking for null.
// http://stackoverflow.com/q/1583050/181087
Expand Down Expand Up @@ -498,6 +535,10 @@ private MapValue PutImpl(object o, MapValue value)
/// </summary>
private MapValue PutImpl(string text, MapValue value)
{
// LUCENENET: Added guard clause
if (text is null)
throw new ArgumentNullException(nameof(text));

return PutImpl(text.ToCharArray(), value);
}

Expand All @@ -509,6 +550,10 @@ private MapValue PutImpl(string text, MapValue value)
/// </summary>
private MapValue PutImpl(char[] text, MapValue value)
{
// LUCENENET: Added guard clause
if (text is null)
throw new ArgumentNullException(nameof(text));

if (ignoreCase)
{
charUtils.ToLower(text, 0, text.Length);
Expand Down
12 changes: 10 additions & 2 deletions src/Lucene.Net.Analysis.Common/Analysis/Util/CharArraySet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,17 @@ public CharArraySet(LuceneVersion matchVersion, int startSize, bool ignoreCase)
/// <c>false</c> if and only if the set should be case sensitive
/// otherwise <c>true</c>. </param>
public CharArraySet(LuceneVersion matchVersion, ICollection<string> c, bool ignoreCase)
: this(matchVersion, c.Count, ignoreCase)
: this(matchVersion, c?.Count ?? 0, ignoreCase)
{
this.UnionWith(c);
// LUCENENET: Added guard clause
if (c is null)
throw new ArgumentNullException(nameof(c));

foreach (string text in c)
{
// LUCENENET: S1699: Don't call call protected members in the constructor
map.Put(text);
}
}

/// <summary>
Expand Down

0 comments on commit 555e280

Please sign in to comment.