Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce equality comparers for OpenXmlElement #1476

Merged
merged 18 commits into from
Jul 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace DocumentFormat.OpenXml.Equality
PhDuck marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Equality comparer for determining value equality for <see cref="OpenXmlElement"/>.
/// </summary>
public sealed class OpenXmlElementEqualityComparer : IEqualityComparer<OpenXmlElement>
PhDuck marked this conversation as resolved.
Show resolved Hide resolved
{
private static readonly OpenXmlElementEqualityComparer[] Comparers = InitializeComparers();

private static OpenXmlElementEqualityComparer[] InitializeComparers()
{
// Calculate how many comparer there can maximum be.
var hs = new HashSet<int>(Enumerable.Range(0, Enum.GetValues<OpenXmlElementEqualityOptions>().Length).Select(f => (int)Math.Pow(2, f)));
int comparerMaxCombinations = (int)Math.Pow(2, Enum.GetValues<OpenXmlElementEqualityOptions>().Where(f => hs.Contains((int)f)).Count());

var comparers = new OpenXmlElementEqualityComparer[comparerMaxCombinations];
for (int i = 0; i < comparerMaxCombinations; i++)
{
comparers[i] = new OpenXmlElementEqualityComparer((OpenXmlElementEqualityOptions)i);
}

return comparers;
}

private OpenXmlElementEqualityComparer(OpenXmlElementEqualityOptions options)
{
this.Options = options;
}

/// <summary>
/// Gets the options regulating how equality is defined.
/// </summary>
internal OpenXmlElementEqualityOptions Options { get; }

/// <summary>
/// Gets the <see cref="IEqualityComparer{OpenXmlElement}"/> based on the given options./>
/// </summary>
/// <param name="openXmlElementEqualityOptions">The options defining equality.</param>
/// <returns></returns>
public static IEqualityComparer<OpenXmlElement> GetEqualityComparer(OpenXmlElementEqualityOptions openXmlElementEqualityOptions) => Comparers[(int)openXmlElementEqualityOptions];

/// <summary>
/// Gets the default equality comparer.
/// </summary>
public static IEqualityComparer<OpenXmlElement> Default => GetEqualityComparer(OpenXmlElementEqualityOptions.Default);

/// <summary>
/// Determines equality for two given <see cref="OpenXmlElement"/>.
/// </summary>
/// <param name="x">First object.</param>
/// <param name="y">Second object.</param>
/// <returns></returns>
public bool Equals(OpenXmlElement? x, OpenXmlElement? y)
{
if (ReferenceEquals(x, y))
{
return true;
}

if (x == null || y == null)
{
return false;
}

return x.ValueEquality(y, this);
}

/// <summary>
/// Calculates a hashcode based on the given <see cref="OpenXmlElement"/> object.
/// </summary>
/// <param name="obj">The object to get a hashcode for.</param>
/// <returns></returns>
public int GetHashCode([DisallowNull] OpenXmlElement obj)
{
if (obj == null)
{
return 0;
}

return obj.GetValueHashCode(this.Options);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace DocumentFormat.OpenXml.Equality
{
/// <summary>
/// Options defining the behaviour of equality for <see cref="OpenXmlElement"/>.
/// </summary>
[Flags]
public enum OpenXmlElementEqualityOptions
PhDuck marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Only consider elements and their nested elements.
/// </summary>
None = 0,

/// <summary>
/// Specifies if extended attributes should be considered when determining equality.
/// </summary>
IncludeExtendedAttributes = 1,

/// <summary>
/// Specifies if MC attributes should be considered when determining equality.
/// </summary>
IncludeMCAttributes = 2,

/// <summary>
/// Specifies if namespace should alone be used when comparing idenity of elements, skipping prefix lookup.
/// </summary>
CompareNamespaceInsteadOfPrefix = 4,

/// <summary>
/// Specifies that elements must be parsed which ensures order of schema is used instead of input ordering.
/// </summary>
RequireParsed = 8,

/// <summary>
/// The default.
/// </summary>
Default = IncludeExtendedAttributes | IncludeMCAttributes,
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;

namespace DocumentFormat.OpenXml
{
/// <summary>
/// Defines the Markup Compatibility Attributes.
/// </summary>
public class MarkupCompatibilityAttributes
public class MarkupCompatibilityAttributes : IEquatable<MarkupCompatibilityAttributes>
{
/// <summary>
/// Gets or sets a whitespace-delimited list of prefixes, where each
Expand Down Expand Up @@ -42,5 +44,47 @@ public class MarkupCompatibilityAttributes
/// a set of namespace names.
/// </summary>
public StringValue? MustUnderstand { get; set; }

/// <inheritdoc/>
public override bool Equals(object? obj)
{
return this.Equals(obj as MarkupCompatibilityAttributes);
}

/// <inheritdoc/>
public bool Equals(MarkupCompatibilityAttributes? other)
{
if (other == null)
{
return false;
}

if (object.ReferenceEquals(this, other))
{
return true;
}

return (this.Ignorable == null == (other.Ignorable == null)) &&
PhDuck marked this conversation as resolved.
Show resolved Hide resolved
(this.Ignorable == null || this.Ignorable.Equals(other.Ignorable)) &&
(this.ProcessContent == null == (other.ProcessContent == null)) &&
(this.ProcessContent == null || this.ProcessContent.Equals(other.ProcessContent)) &&
(this.PreserveElements == null == (other.PreserveElements == null)) &&
(this.PreserveElements == null || this.PreserveElements.Equals(other.PreserveElements)) &&
(this.PreserveAttributes == null == (other.PreserveAttributes == null)) &&
(this.PreserveAttributes == null || this.PreserveAttributes.Equals(other.PreserveAttributes)) &&
(this.MustUnderstand == null == (other.MustUnderstand == null)) &&
(this.MustUnderstand == null || this.MustUnderstand.Equals(other.MustUnderstand));
PhDuck marked this conversation as resolved.
Show resolved Hide resolved
}

/// <inheritdoc/>
public override int GetHashCode()
{
return HashCode.Combine(
this.Ignorable?.GetHashCode() ?? 0,
this.ProcessContent?.GetHashCode() ?? 0,
this.PreserveElements?.GetHashCode() ?? 0,
this.PreserveAttributes?.GetHashCode() ?? 0,
this.MustUnderstand?.GetHashCode() ?? 0);
}
}
}
Loading