Skip to content

Commit

Permalink
Use primary constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
nurhafiz committed May 2, 2024
1 parent f00f1cf commit 034e10a
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 88 deletions.
27 changes: 7 additions & 20 deletions src/Toimik.SitemapsProtocol/Sitemap/Sitemap.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 [email protected]
* Copyright 2021-2024 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,15 +24,10 @@ namespace Toimik.SitemapsProtocol;
using System.Xml;
using System.Xml.Schema;

public class Sitemap
public class Sitemap(SitemapParser parser)
{
private readonly ISet<SitemapEntry> entries = new HashSet<SitemapEntry>(new EntryComparer());

public Sitemap(SitemapParser parser)
{
Parser = parser;
}

public Sitemap(Uri location, int entryMaxCount = SitemapParser.DefaultEntryMaxCount)
: this(new SitemapParser(location, entryMaxCount))
{
Expand All @@ -42,7 +37,7 @@ public Sitemap(Uri location, int entryMaxCount = SitemapParser.DefaultEntryMaxCo

public int EntryCount => entries.Count;

public SitemapParser Parser { get; }
public SitemapParser Parser { get; } = parser;

public bool AddEntry(SitemapEntry entry)
{
Expand All @@ -55,16 +50,12 @@ public bool AddEntry(SitemapEntry entry)
/// <summary>
/// Loads, to this instance, the data of a sitemap from a <see cref="string"/>.
/// </summary>
/// <param name="data">
/// Data of a sitemap.
/// </param>
/// <param name="data">Data of a sitemap.</param>
/// <param name="schemaStream">
/// <see cref="Stream"/> of schema, which is used to validate the sitemap index against. If
/// <c>null</c>, the default one is used.
/// </param>
/// <remarks>
/// All existing entries, if any, are cleared when this method is called.
/// </remarks>
/// <remarks>All existing entries, if any, are cleared when this method is called.</remarks>
public void Load(string data, Stream? schemaStream = null)
{
var byteArray = Encoding.UTF8.GetBytes(data);
Expand All @@ -89,17 +80,13 @@ public void Load(string data, Stream? schemaStream = null)
/// <see cref="Stream"/> of schema, which is used to validate the sitemap index against. If
/// <c>null</c>, the default one is used.
/// </param>
/// <returns>
/// A <see cref="Task"/>.
/// </returns>
/// <returns>A <see cref="Task"/>.</returns>
/// <exception cref="ObjectDisposedException">
/// Thrown when <paramref name="dataStream"/> is manually closed.
/// </exception>
/// <remarks>
/// All existing entries, if any, are cleared when this method is called.
/// <para>
/// Call <see cref="Stream.Close()"/> on <paramref name="dataStream"/> to cancel loading.
/// </para>
/// <para>Call <see cref="Stream.Close()"/> on <paramref name="dataStream"/> to cancel loading.</para>
/// </remarks>
public async Task Load(Stream dataStream, Stream? schemaStream = null)
{
Expand Down
11 changes: 3 additions & 8 deletions src/Toimik.SitemapsProtocol/Sitemap/SitemapEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 [email protected]
* Copyright 2021-2024 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,7 @@ namespace Toimik.SitemapsProtocol;

using System;

public class SitemapEntry : IEntry
public class SitemapEntry(string baseLocation) : IEntry
{
private const string TagForChangeFrequency = "changefreq";

Expand All @@ -28,12 +28,7 @@ public class SitemapEntry : IEntry

private const string TagForPriority = "priority";

public SitemapEntry(string baseLocation)
{
BaseLocation = baseLocation;
}

public string BaseLocation { get; }
public string BaseLocation { get; } = baseLocation;

public ChangeFrequency? ChangeFrequency { get; private set; }

Expand Down
14 changes: 4 additions & 10 deletions src/Toimik.SitemapsProtocol/Sitemap/SitemapParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 [email protected]
* Copyright 2021-2024 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@ namespace Toimik.SitemapsProtocol;
using System.Xml;
using System.Xml.Schema;

public class SitemapParser
public class SitemapParser(Uri location, int entryMaxCount = SitemapParser.DefaultEntryMaxCount)
{
// As per standard
internal const int DefaultEntryMaxCount = 50000;
Expand All @@ -38,15 +38,9 @@ static SitemapParser()
SchemaSet = Utils.CreateSchemaSet($"{typeof(Sitemap).Namespace}.Resources.sitemap.xsd");
}

public SitemapParser(Uri location, int entryMaxCount = DefaultEntryMaxCount)
{
Location = Utils.NormalizeLocation(location) ?? throw new ArgumentException($"{nameof(location)} is not in a valid format.");
EntryMaxCount = entryMaxCount;
}

public int EntryMaxCount { get; }
public int EntryMaxCount { get; } = entryMaxCount;

public string Location { get; }
public string Location { get; } = Utils.NormalizeLocation(location) ?? throw new ArgumentException($"{nameof(location)} is not in a valid format.");

public async IAsyncEnumerable<SitemapEntry> Parse(Stream dataStream, Stream? schemaStream = null)
{
Expand Down
27 changes: 7 additions & 20 deletions src/Toimik.SitemapsProtocol/SitemapIndex/SitemapIndex.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 [email protected]
* Copyright 2021-2024 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,15 +24,10 @@ namespace Toimik.SitemapsProtocol;
using System.Xml;
using System.Xml.Schema;

public class SitemapIndex
public class SitemapIndex(SitemapIndexParser parser)
{
private readonly ISet<SitemapIndexEntry> entries = new HashSet<SitemapIndexEntry>(new EntryComparer());

public SitemapIndex(SitemapIndexParser parser)
{
Parser = parser;
}

public SitemapIndex(Uri location, int entryMaxCount = SitemapIndexParser.DefaultEntryMaxCount)
: this(new SitemapIndexParser(location, entryMaxCount))
{
Expand All @@ -42,7 +37,7 @@ public SitemapIndex(Uri location, int entryMaxCount = SitemapIndexParser.Default

public int EntryCount => entries.Count;

public SitemapIndexParser Parser { get; }
public SitemapIndexParser Parser { get; } = parser;

public bool AddEntry(SitemapIndexEntry entry)
{
Expand All @@ -55,16 +50,12 @@ public bool AddEntry(SitemapIndexEntry entry)
/// <summary>
/// Loads, to this instance, the data of a sitemap index from a <see cref="string"/>.
/// </summary>
/// <param name="data">
/// Data of a sitemap index.
/// </param>
/// <param name="data">Data of a sitemap index.</param>
/// <param name="schemaStream">
/// <see cref="Stream"/> of schema, which is used to validate the sitemap index against. If
/// <c>null</c>, the default one is used.
/// </param>
/// <remarks>
/// All existing entries, if any, are cleared when this method is called.
/// </remarks>
/// <remarks>All existing entries, if any, are cleared when this method is called.</remarks>
public void Load(string data, Stream? schemaStream = null)
{
var byteArray = Encoding.UTF8.GetBytes(data);
Expand All @@ -89,12 +80,8 @@ public void Load(string data, Stream? schemaStream = null)
/// <see cref="Stream"/> of schema, which is used to validate the sitemap index against. If
/// <c>null</c>, the default one is used.
/// </param>
/// <returns>
/// A <see cref="Task"/>.
/// </returns>
/// <remarks>
/// All existing entries, if any, are cleared when this method is called.
/// </remarks>
/// <returns>A <see cref="Task"/>.</returns>
/// <remarks>All existing entries, if any, are cleared when this method is called.</remarks>
public async Task Load(Stream dataStream, Stream? schemaStream = null)
{
entries.Clear();
Expand Down
11 changes: 3 additions & 8 deletions src/Toimik.SitemapsProtocol/SitemapIndex/SitemapIndexEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 [email protected]
* Copyright 2021-2024 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,18 +18,13 @@ namespace Toimik.SitemapsProtocol;

using System;

public class SitemapIndexEntry : IEntry
public class SitemapIndexEntry(string baseLocation) : IEntry
{
private const string TagForLastModified = "lastmod";

private const string TagForLocation = "loc";

public SitemapIndexEntry(string baseLocation)
{
BaseLocation = baseLocation;
}

public string BaseLocation { get; }
public string BaseLocation { get; } = baseLocation;

public DateTime? LastModified { get; internal set; }

Expand Down
14 changes: 4 additions & 10 deletions src/Toimik.SitemapsProtocol/SitemapIndex/SitemapIndexParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2022 [email protected]
* Copyright 2021-2024 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,7 +22,7 @@ namespace Toimik.SitemapsProtocol;
using System.Xml;
using System.Xml.Schema;

public class SitemapIndexParser
public class SitemapIndexParser(Uri location, int entryMaxCount = SitemapIndexParser.DefaultEntryMaxCount)
{
// As per standard
internal const int DefaultEntryMaxCount = 50000;
Expand All @@ -38,15 +38,9 @@ static SitemapIndexParser()
SchemaSet = Utils.CreateSchemaSet($"{typeof(SitemapIndex).Namespace}.Resources.siteindex.xsd");
}

public SitemapIndexParser(Uri location, int entryMaxCount = DefaultEntryMaxCount)
{
Location = Utils.NormalizeLocation(location) ?? throw new ArgumentException($"{nameof(location)} is not in a valid format.");
EntryMaxCount = entryMaxCount;
}

public int EntryMaxCount { get; }
public int EntryMaxCount { get; } = entryMaxCount;

public string Location { get; }
public string Location { get; } = Utils.NormalizeLocation(location) ?? throw new ArgumentException($"{nameof(location)} is not in a valid format.");

public async IAsyncEnumerable<SitemapIndexEntry> Parse(Stream dataStream, Stream? schemaStream = null)
{
Expand Down
14 changes: 2 additions & 12 deletions tests/Toimik.SitemapsProtocol.Tests/SitemapTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,8 @@ private static SitemapEntry GetOnlyEntry(IEnumerator<SitemapEntry> entries)
return entry;
}

private class ExtendedSitemapEntry : SitemapEntry
private class ExtendedSitemapEntry(string locationPrefix) : SitemapEntry(locationPrefix)
{
public ExtendedSitemapEntry(string locationPrefix)
: base(locationPrefix)
{
}

public string? Title { get; internal set; }

internal override void Set(string name, string value)
Expand All @@ -379,13 +374,8 @@ internal override void Set(string name, string value)
}
}

private class ExtendedSitemapParser : SitemapParser
private class ExtendedSitemapParser(Uri location) : SitemapParser(location)
{
public ExtendedSitemapParser(Uri location)
: base(location)
{
}

protected override SitemapEntry CreateEntry()
{
return new ExtendedSitemapEntry(Location);
Expand Down

0 comments on commit 034e10a

Please sign in to comment.