File: Language\DirectiveDescriptorComparer.cs
Web Access
Project: src\src\roslyn\src\Razor\src\Compiler\Microsoft.CodeAnalysis.Razor.Compiler\src\Microsoft.CodeAnalysis.Razor.Compiler.csproj (Microsoft.CodeAnalysis.Razor.Compiler)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Internal;

namespace Microsoft.AspNetCore.Razor.Language;

internal class DirectiveDescriptorComparer : IEqualityComparer<DirectiveDescriptor>
{
    public static readonly DirectiveDescriptorComparer Default = new DirectiveDescriptorComparer();

    protected DirectiveDescriptorComparer()
    {
    }

    public bool Equals(DirectiveDescriptor descriptorX, DirectiveDescriptor descriptorY)
    {
        if (descriptorX == descriptorY)
        {
            return true;
        }

        return descriptorX != null &&
            string.Equals(descriptorX.Directive, descriptorY.Directive, StringComparison.Ordinal) &&
            descriptorX.Kind == descriptorY.Kind &&
            Enumerable.SequenceEqual(
                descriptorX.Tokens,
                descriptorY.Tokens,
                DirectiveTokenDescriptorComparer.Default);
    }

    public int GetHashCode(DirectiveDescriptor descriptor)
    {
        if (descriptor == null)
        {
            throw new ArgumentNullException(nameof(descriptor));
        }

        var hash = HashCodeCombiner.Start();
        hash.Add(descriptor.Directive, StringComparer.Ordinal);
        hash.Add(descriptor.Kind);

        return hash.CombinedHash;
    }
}