Skip to content

Commit 3273acf

Browse files
authored
Implement Equals correctly for IdPartPair (#871)
1 parent 58af49a commit 3273acf

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
- Added nullability attributes (#840, #849)
1111
- Added overload for `OpenXmlPartReader` and `OpenXmlReader.Create(...)` to ignore whitespace (#857)
1212
- Added `HexBinaryValue.TryGetBytes(...)` and `HexBinaryValue.Create(byte[])` to manage the encoding and decoding of bytes (#867)
13+
- Implemented `IEquatable<IdPartPair>` on `IdPartPair` to fix equality implementation there and obsoleted setters (#871)
1314

1415
## Version 2.12.1 - 2021-01-11
1516

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<!-- Because of the size of the project, to facilitate quick development, by default only single
99
frameworks will be build. This is customizable with the following possible values:
10-
- net35: .NET Framework 3.5
10+
- net35: .NET Framework 3.5
1111
- net40: .NET Framework 4.0
1212
- net46: .NET Framework 4.6
1313
- netstandard1_3: .NET Core 2.1 (but running .NET Standard 1.3 build)
Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,37 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
#nullable disable
5-
64
using System;
75

6+
#if !NET5_0
7+
using DocumentFormat.OpenXml.Framework;
8+
#endif
9+
810
namespace DocumentFormat.OpenXml.Packaging
911
{
1012
/// <summary>
1113
/// Represents a (RelationshipId, OpenXmlPart) pair.
1214
/// </summary>
13-
public class IdPartPair
15+
public class IdPartPair : IEquatable<IdPartPair>
1416
{
15-
private string _id;
16-
private OpenXmlPart _part;
17-
1817
/// <summary>
1918
/// Gets or sets the relationship ID in the pair.
2019
/// </summary>
2120
public string RelationshipId
2221
{
23-
get { return _id; }
24-
set { _id = value; }
22+
get;
23+
[Obsolete("This object will be made immutable in a future release. Please use a new instance.")]
24+
set;
2525
}
2626

2727
/// <summary>
2828
/// Gets or sets the OpenXmlPart in the pair.
2929
/// </summary>
3030
public OpenXmlPart OpenXmlPart
3131
{
32-
get { return _part; }
33-
set { _part = value; }
32+
get;
33+
[Obsolete("This object will be made immutable in a future release. Please use a new instance.")]
34+
set;
3435
}
3536

3637
/// <summary>
@@ -40,24 +41,40 @@ public OpenXmlPart OpenXmlPart
4041
/// <param name="part">The OpenXmlPart.</param>
4142
public IdPartPair(string id, OpenXmlPart part)
4243
{
44+
#pragma warning disable CS0618 // Type or member is obsolete
4345
RelationshipId = id;
4446
OpenXmlPart = part;
47+
#pragma warning restore CS0618 // Type or member is obsolete
4548
}
4649

47-
/// <summary>
48-
/// Determines whether this instance and another specified IdPartPair object have the same value.
49-
/// </summary>
50-
/// <param name="value">An IdPartPair.</param>
51-
/// <returns>True if the value of the value parameter is the same as this instance; otherwise, false.</returns>
52-
public bool Equals(IdPartPair value)
50+
/// <inheritdoc/>
51+
public override bool Equals(object? obj) => obj is IdPartPair other && Equals(other);
52+
53+
/// <inheritdoc/>
54+
public override int GetHashCode()
55+
{
56+
var code = new HashCode();
57+
58+
code.Add(RelationshipId, StringComparer.Ordinal);
59+
60+
return code.ToHashCode();
61+
}
62+
63+
/// <inheritdoc/>
64+
public bool Equals(IdPartPair? value)
5365
{
5466
//Check for null
5567
if (value is null)
5668
{
5769
return false;
5870
}
5971

60-
return string.Equals(_id, value._id, StringComparison.Ordinal) && (_part == value._part);
72+
if (ReferenceEquals(this, value))
73+
{
74+
return true;
75+
}
76+
77+
return string.Equals(RelationshipId, value.RelationshipId, StringComparison.Ordinal) && (OpenXmlPart == value.OpenXmlPart);
6178
}
6279
}
6380
}

0 commit comments

Comments
 (0)