Skip to content

Commit

Permalink
SWEEP: Use pattern matching, #667 (#1082)
Browse files Browse the repository at this point in the history
* SWEEP: Replace type-check-and-cast with pattern matching, #667

* SWEEP: Use negated pattern matching, #667
  • Loading branch information
paulirwin authored Jan 1, 2025
1 parent 47adc07 commit d95a40b
Show file tree
Hide file tree
Showing 185 changed files with 517 additions and 546 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30868,7 +30868,7 @@ public HTMLStripCharFilter(TextReader source, ICollection<string> escapedTags)
/// <returns></returns>
private static BufferedCharFilter GetBufferedReader(TextReader reader)
{
return (reader is BufferedCharFilter) ? (BufferedCharFilter)reader : new BufferedCharFilter(reader);
return reader as BufferedCharFilter ?? new BufferedCharFilter(reader);
}

public override int Read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class MappingCharFilter : BaseCharFilter

/// <summary>
/// Default constructor that takes a <see cref="TextReader"/>. </summary>
public MappingCharFilter(NormalizeCharMap normMap, TextReader @in)
public MappingCharFilter(NormalizeCharMap normMap, TextReader @in)
: base(@in)
{
//LUCENENET support to reset the reader.
Expand All @@ -79,20 +79,20 @@ public MappingCharFilter(NormalizeCharMap normMap, TextReader @in)

/// <summary>
/// LUCENENET: Copied this method from the <see cref="WordlistLoader"/> class - this class requires readers
/// with a Reset() method (which .NET readers don't support). So, we use the <see cref="BufferedCharFilter"/>
/// (which is similar to Java BufferedReader) as a wrapper for whatever reader the user passes
/// with a Reset() method (which .NET readers don't support). So, we use the <see cref="BufferedCharFilter"/>
/// (which is similar to Java BufferedReader) as a wrapper for whatever reader the user passes
/// (unless it is already a <see cref="BufferedCharFilter"/>).
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
private static BufferedCharFilter GetBufferedReader(TextReader reader)
{
return (reader is BufferedCharFilter) ? (BufferedCharFilter)reader : new BufferedCharFilter(reader);
return reader as BufferedCharFilter ?? new BufferedCharFilter(reader);
}

public override void Reset()
{
// LUCENENET: reset the BufferedCharFilter.
// LUCENENET: reset the BufferedCharFilter.
_input.Reset();
buffer.Reset(_input);
replacement = null;
Expand Down Expand Up @@ -240,4 +240,4 @@ public override int Read(char[] cbuf, int off, int len)
return numRead == 0 ? -1 : numRead;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ public virtual Hyphenation Hyphenate(char[] w, int offset, int len, int remainCh
object o = hw[i];
// j = index(sw) = letterindex(word)?
// result[k] = corresponding index(w)
if (o is string)
if (o is string os)
{
j += ((string)o).Length;
j += os.Length;
if (j >= remainCharCount && j < (len - pushCharCount))
{
result[k++] = j + iIgnoreAtBeginning;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace Lucene.Net.Analysis.Compound.Hyphenation
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -62,7 +62,7 @@ public PatternParser()
hyphenChar = '-'; // default
}

public PatternParser(IPatternConsumer consumer)
public PatternParser(IPatternConsumer consumer)
: this()
{
this.consumer = consumer;
Expand Down Expand Up @@ -127,8 +127,8 @@ public virtual void Parse(FileInfo file, Encoding encoding)
/// <param name="xmlStream">
/// The stream containing the XML data.
/// <para/>
/// The <see cref="PatternParser"/> scans the first bytes of the stream looking for a byte order mark
/// or other sign of encoding. When encoding is determined, the encoding is used to continue reading
/// The <see cref="PatternParser"/> scans the first bytes of the stream looking for a byte order mark
/// or other sign of encoding. When encoding is determined, the encoding is used to continue reading
/// the stream, and processing continues parsing the input as a stream of (Unicode) characters.
/// </param>
/// <exception cref="IOException"> In case of an exception while parsing </exception>
Expand Down Expand Up @@ -294,9 +294,8 @@ protected virtual IList<object> NormalizeException<T1>(IList<T1> ex)
for (int i = 0; i < ex.Count; i++)
{
object item = ex[i];
if (item is string)
if (item is string str)
{
string str = (string)item;
StringBuilder buf = new StringBuilder();
for (int j = 0; j < str.Length; j++)
{
Expand Down Expand Up @@ -335,9 +334,9 @@ protected virtual string GetExceptionWord<T1>(IList<T1> ex)
for (int i = 0; i < ex.Count; i++)
{
object item = ex[i];
if (item is string)
if (item is string str)
{
res.Append((string)item);
res.Append(str);
}
else
{
Expand Down Expand Up @@ -396,9 +395,9 @@ public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToRe
/// <summary>
/// Receive notification of the beginning of an element.
/// <para/>
/// The Parser will invoke this method at the beginning of every element in the XML document;
/// there will be a corresponding <see cref="EndElement"/> event for every <see cref="StartElement"/> event
/// (even when the element is empty). All of the element's content will be reported,
/// The Parser will invoke this method at the beginning of every element in the XML document;
/// there will be a corresponding <see cref="EndElement"/> event for every <see cref="StartElement"/> event
/// (even when the element is empty). All of the element's content will be reported,
/// in order, before the corresponding endElement event.
/// </summary>
/// <param name="uri">the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed</param>
Expand Down Expand Up @@ -442,8 +441,8 @@ public virtual void StartElement(string uri, string local, string raw, IDictiona
/// <summary>
/// Receive notification of the end of an element.
/// <para/>
/// The parser will invoke this method at the end of every element in the XML document;
/// there will be a corresponding <see cref="StartElement"/> event for every
/// The parser will invoke this method at the end of every element in the XML document;
/// there will be a corresponding <see cref="StartElement"/> event for every
/// <see cref="EndElement"/> event (even when the element is empty).
/// </summary>
/// <param name="uri">the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed</param>
Expand Down Expand Up @@ -489,9 +488,9 @@ public virtual void EndElement(string uri, string local, string raw)
/// <summary>
/// Receive notification of character data.
/// <para/>
/// The Parser will call this method to report each chunk of character data. Parsers may
/// return all contiguous character data in a single chunk, or they may split it into
/// several chunks; however, all of the characters in any single event must come from
/// The Parser will call this method to report each chunk of character data. Parsers may
/// return all contiguous character data in a single chunk, or they may split it into
/// several chunks; however, all of the characters in any single event must come from
/// the same external entity so that the Locator provides useful information.
/// <para/>
/// The application must not attempt to read from the array outside of the specified range.
Expand Down Expand Up @@ -526,4 +525,4 @@ public virtual void Characters(char[] ch, int start, int length)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2713,7 +2713,7 @@ void ICollection.CopyTo(Array array, int index)
}
else
{
if (!(array is object[] objects))
if (array is not object[] objects)
{
throw new ArgumentException(SR.Argument_InvalidArrayType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public FilesystemResourceLoader(DirectoryInfo baseDirectory, IResourceLoader @de
{
// LUCENENET NOTE: If you call DirectoryInfo.Create() it doesn't set the DirectoryInfo.Exists
// flag to true, so we use the Directory object to check the path explicitly.
if (!(baseDirectory is null) && !Directory.Exists(baseDirectory.FullName))
if (baseDirectory is not null && !Directory.Exists(baseDirectory.FullName))
{
throw new ArgumentException("baseDirectory is not a directory or is null");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public override bool Equals(object other)
{
return true;
}
if (!(other is Branch))
if (other is not Branch branch)
{
return false;
}

return ToString().Equals(((Branch)other).ToString(), StringComparison.Ordinal);
return ToString().Equals(branch.ToString(), StringComparison.Ordinal);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Benchmark/Support/TagSoup/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ private TextReader GetReader(InputSource s)
{
i = GetInputStream(publicId, systemId);
}
if (!(i is BufferedStream))
if (i is not BufferedStream)
{
i = new BufferedStream(i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public override TermsEnum GetEnumerator()

public override TermsEnum GetEnumerator(TermsEnum reuse)
{
if (!(reuse is null) && reuse is BloomFilteredTermsEnum bfte && bfte.filter == _filter)
if (reuse is not null && reuse is BloomFilteredTermsEnum bfte && bfte.filter == _filter)
{
// recycle the existing BloomFilteredTermsEnum by asking the delegate
// to recycle its contained TermsEnum
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Codecs/Memory/DirectPostingsFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ public override TermsEnum GetEnumerator()

public override TermsEnum GetEnumerator(TermsEnum reuse)
{
if (!(reuse is DirectTermsEnum termsEnum) || !termsEnum.CanReuse(terms))
if (reuse is not DirectTermsEnum termsEnum || !termsEnum.CanReuse(terms))
termsEnum = new DirectTermsEnum(this);

termsEnum.Reset();
Expand Down
3 changes: 1 addition & 2 deletions src/Lucene.Net.Codecs/Memory/FSTTermOutputs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ public override bool Equals(object other)
if (other == this)
return true;

if (!(other is TermData))
if (other is not TermData _other)
return false;

var _other = (TermData) other;
return StatsEqual(this, _other) && Int64sEqual(this, _other) && BytesEqual(this, _other);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Codecs/Memory/MemoryPostingsFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public override DocsEnum Docs(IBits liveDocs, DocsEnum reuse, DocsFlags flags)
{
DecodeMetaData();

if (reuse is null || !(reuse is FSTDocsEnum docsEnum) || !docsEnum.CanReuse(field.IndexOptions, field.HasPayloads))
if (reuse is null || reuse is not FSTDocsEnum docsEnum || !docsEnum.CanReuse(field.IndexOptions, field.HasPayloads))
docsEnum = new FSTDocsEnum(field.IndexOptions, field.HasPayloads);

return docsEnum.Reset(this.postingsSpare, liveDocs, docFreq);
Expand All @@ -823,7 +823,7 @@ public override DocsAndPositionsEnum DocsAndPositions(IBits liveDocs, DocsAndPos
return null;
}
DecodeMetaData();
if (reuse is null || !(reuse is FSTDocsAndPositionsEnum docsAndPositionsEnum) || !docsAndPositionsEnum.CanReuse(field.HasPayloads, hasOffsets))
if (reuse is null || reuse is not FSTDocsAndPositionsEnum docsAndPositionsEnum || !docsAndPositionsEnum.CanReuse(field.HasPayloads, hasOffsets))
docsAndPositionsEnum = new FSTDocsAndPositionsEnum(field.HasPayloads, hasOffsets);

//System.out.println("D&P reset this=" + this);
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Codecs/Pulsing/PulsingPostingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public override DocsEnum Docs(FieldInfo field, BlockTermState termState, IBits l
return postings.Reset(liveDocs, termState2);
}

if (!(reuse is PulsingDocsEnum))
if (reuse is not PulsingDocsEnum)
return _wrappedPostingsReader.Docs(field, termState2.WrappedTermState, liveDocs, reuse, flags);

var wrapped = _wrappedPostingsReader.Docs(field, termState2.WrappedTermState, liveDocs,
Expand Down Expand Up @@ -299,7 +299,7 @@ public override DocsAndPositionsEnum DocsAndPositions(FieldInfo field, BlockTerm
return postings.Reset(liveDocs, termState2);
}

if (!(reuse is PulsingDocsAndPositionsEnum))
if (reuse is not PulsingDocsAndPositionsEnum)
return _wrappedPostingsReader.DocsAndPositions(field, termState2.WrappedTermState, liveDocs, reuse,
flags);

Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Codecs/Sep/SepPostingsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public override DocsEnum Docs(FieldInfo fieldInfo, BlockTermState termState, IBi
// If you are using ParellelReader, and pass in a
// reused DocsAndPositionsEnum, it could have come
// from another reader also using sep codec
if (reuse is null || !(reuse is SepDocsEnum docsEnum) || docsEnum.startDocIn != docIn)
if (reuse is null || reuse is not SepDocsEnum docsEnum || docsEnum.startDocIn != docIn)
docsEnum = new SepDocsEnum(this);

return docsEnum.Init(fieldInfo, termState_, liveDocs);
Expand All @@ -260,7 +260,7 @@ public override DocsAndPositionsEnum DocsAndPositions(FieldInfo fieldInfo, Block
// If you are using ParellelReader, and pass in a
// reused DocsAndPositionsEnum, it could have come
// from another reader also using sep codec
if (reuse is null || !(reuse is SepDocsAndPositionsEnum postingsEnum) || postingsEnum.startDocIn != docIn)
if (reuse is null || reuse is not SepDocsAndPositionsEnum postingsEnum || postingsEnum.startDocIn != docIn)
postingsEnum = new SepDocsAndPositionsEnum(this);

return postingsEnum.Init(fieldInfo, termState_, liveDocs);
Expand Down
4 changes: 2 additions & 2 deletions src/Lucene.Net.Codecs/SimpleText/SimpleTextFieldsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public override BytesRef Next()

public override DocsEnum Docs(IBits liveDocs, DocsEnum reuse, DocsFlags flags)
{
if (reuse is null || !(reuse is SimpleTextDocsEnum docsEnum) || !docsEnum.CanReuse(outerInstance.input))
if (reuse is null || reuse is not SimpleTextDocsEnum docsEnum || !docsEnum.CanReuse(outerInstance.input))
docsEnum = new SimpleTextDocsEnum(outerInstance);

return docsEnum.Reset(docsStart, liveDocs, indexOptions == IndexOptions.DOCS_ONLY, docFreq);
Expand All @@ -223,7 +223,7 @@ public override DocsAndPositionsEnum DocsAndPositions(IBits liveDocs, DocsAndPos
return null;
}

if (reuse is null || !(reuse is SimpleTextDocsAndPositionsEnum docsAndPositionsEnum) || !docsAndPositionsEnum.CanReuse(outerInstance.input))
if (reuse is null || reuse is not SimpleTextDocsAndPositionsEnum docsAndPositionsEnum || !docsAndPositionsEnum.CanReuse(outerInstance.input))
docsAndPositionsEnum = new SimpleTextDocsAndPositionsEnum(outerInstance);

return docsAndPositionsEnum.Reset(docsStart, liveDocs, indexOptions, docFreq);
Expand Down
3 changes: 1 addition & 2 deletions src/Lucene.Net.Facet/DrillDownQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,11 @@ public override int GetHashCode()

public override bool Equals(object obj)
{
if (!(obj is DrillDownQuery))
if (obj is not DrillDownQuery other)
{
return false;
}

DrillDownQuery other = (DrillDownQuery)obj;
return query.Equals(other.query) && base.Equals(other);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Facet/FacetsCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private static TopDocs DoSearch(IndexSearcher searcher, ScoreDoc after, Query q,

if (sort != null)
{
if (after != null && !(after is FieldDoc))
if (after != null && after is not FieldDoc)
{
// TODO: if we fix type safety of TopFieldDocs we can
// remove this
Expand Down
7 changes: 3 additions & 4 deletions src/Lucene.Net.Facet/Taxonomy/CategoryPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,11 @@ public virtual int CopyFullPath(char[] buf, int start, char delimiter)

public override bool Equals(object obj)
{
if (!(obj is CategoryPath))
if (obj is not CategoryPath other)
{
return false;
}

CategoryPath other = (CategoryPath)obj;
if (Length != other.Length)
{
return false; // not same length, cannot be equal
Expand Down Expand Up @@ -350,7 +349,7 @@ public virtual string ToString(char delimiter)

public static bool operator <(CategoryPath left, CategoryPath right)
{
return left is null ? !(right is null) : left.CompareTo(right) < 0;
return left is null ? right is not null : left.CompareTo(right) < 0;
}

public static bool operator <=(CategoryPath left, CategoryPath right)
Expand All @@ -360,7 +359,7 @@ public virtual string ToString(char delimiter)

public static bool operator >(CategoryPath left, CategoryPath right)
{
return !(left is null) && left.CompareTo(right) > 0;
return left is not null && left.CompareTo(right) > 0;
}

public static bool operator >=(CategoryPath left, CategoryPath right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public DirectoryTaxonomyWriter(DirectoryTaxonomyIndexWriterFactory indexWriterFa
// verify (to some extent) that merge policy in effect would preserve category docids
if (indexWriter != null)
{
if (Debugging.AssertsEnabled) Debugging.Assert(!(indexWriter.Config.MergePolicy is TieredMergePolicy), "for preserving category docids, merging none-adjacent segments is not allowed");
if (Debugging.AssertsEnabled) Debugging.Assert(indexWriter.Config.MergePolicy is not TieredMergePolicy, "for preserving category docids, merging none-adjacent segments is not allowed");
}

// after we opened the writer, and the index is locked, it's safe to check
Expand Down
Loading

0 comments on commit d95a40b

Please sign in to comment.