Skip to content

Commit 1cb11b0

Browse files
Matthias Gessingercommonsensesoftware
authored andcommitted
Extract base class for LinkList
1 parent a679121 commit 1cb11b0

2 files changed

Lines changed: 40 additions & 22 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace Asp.Versioning;
4+
5+
using System.Collections.ObjectModel;
6+
7+
internal abstract class LinkList : Collection<LinkHeaderValue>
8+
{
9+
public LinkList() { }
10+
11+
protected override void InsertItem( int index, LinkHeaderValue item )
12+
{
13+
EnsureRelationType( item );
14+
base.InsertItem( index, item );
15+
}
16+
17+
protected override void SetItem( int index, LinkHeaderValue item )
18+
{
19+
EnsureRelationType( item );
20+
base.SetItem( index, item );
21+
}
22+
23+
protected abstract void EnsureRelationType( LinkHeaderValue item );
24+
}

src/Abstractions/src/Asp.Versioning.Abstractions/SunsetPolicy.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,46 @@
22

33
namespace Asp.Versioning;
44

5-
using System.Collections.ObjectModel;
6-
75
/// <summary>
86
/// Represents an API version sunset policy.
97
/// </summary>
108
public class SunsetPolicy
119
{
12-
private SunsetLinkList? links;
10+
private readonly LinkList links;
1311

1412
/// <summary>
1513
/// Initializes a new instance of the <see cref="SunsetPolicy"/> class.
1614
/// </summary>
17-
public SunsetPolicy() { }
15+
public SunsetPolicy()
16+
{
17+
links = new SunsetLinkList();
18+
}
1819

1920
/// <summary>
2021
/// Initializes a new instance of the <see cref="SunsetPolicy"/> class.
2122
/// </summary>
2223
/// <param name="date">The date and time when the API version will be sunset.</param>
2324
/// <param name="link">The optional link which provides information about the sunset policy.</param>
2425
public SunsetPolicy( DateTimeOffset date, LinkHeaderValue? link = default )
26+
: this()
2527
{
2628
Date = date;
2729

2830
if ( link is not null )
2931
{
30-
links = new() { link };
32+
links.Add( link );
3133
}
3234
}
3335

3436
/// <summary>
3537
/// Initializes a new instance of the <see cref="SunsetPolicy"/> class.
3638
/// </summary>
3739
/// <param name="link">The link which provides information about the sunset policy.</param>
38-
public SunsetPolicy( LinkHeaderValue link ) => links = new() { link };
40+
public SunsetPolicy( LinkHeaderValue link )
41+
: this()
42+
{
43+
links.Add( link );
44+
}
3945

4046
/// <summary>
4147
/// Gets the date and time when the API version will be sunset.
@@ -47,7 +53,7 @@ public SunsetPolicy() { }
4753
/// Gets a value indicating whether the sunset policy has any associated links.
4854
/// </summary>
4955
/// <value>True if the sunset policy has associated links; otherwise, false.</value>
50-
public bool HasLinks => links is not null && links.Count > 0;
56+
public bool HasLinks => links.Count > 0;
5157

5258
/// <summary>
5359
/// Gets a read-only list of links that provide information about the sunset policy.
@@ -56,23 +62,11 @@ public SunsetPolicy() { }
5662
/// <remarks>If a link is provided, generally only one link is necessary; however, additional
5763
/// links might be provided for different languages or different formats such as a HTML page
5864
/// or a JSON file.</remarks>
59-
public IList<LinkHeaderValue> Links => links ??= new();
65+
public IList<LinkHeaderValue> Links => links;
6066

61-
private sealed class SunsetLinkList : Collection<LinkHeaderValue>
67+
internal sealed class SunsetLinkList : LinkList
6268
{
63-
protected override void InsertItem( int index, LinkHeaderValue item )
64-
{
65-
base.InsertItem( index, item );
66-
EnsureRelationType( item );
67-
}
68-
69-
protected override void SetItem( int index, LinkHeaderValue item )
70-
{
71-
base.SetItem( index, item );
72-
EnsureRelationType( item );
73-
}
74-
75-
private static void EnsureRelationType( LinkHeaderValue item )
69+
protected override void EnsureRelationType( LinkHeaderValue item )
7670
{
7771
if ( !item.RelationType.Equals( "sunset", StringComparison.OrdinalIgnoreCase ) )
7872
{

0 commit comments

Comments
 (0)